H5W3
当前位置:H5W3 > 其他技术问题 > 正文

【前端问题精选】过滤出相同ID的数据(基础问题)

一个基础数据处理问题.打扰各位了.

描述:
data2 nodes 的id 对应 data1 tableModels 中的 id
data2 edges 的id 对应 data1 tableRelations 中的 id

目标:
仅保留 data1 中 含有 data2 中 id 的 tableModels 和 tableRelations 数据.
(实际情况是tableModels 和 tableRelations 会有多条)

如何实现 ?

data1:

    "tableModels": [
      {
        "schema": "public",
        "aliasName": "tableName1",
        "tableName": "car_cross_info1",
        "id": "44ae872a"
      },
      {
        "schema": "public",
        "aliasName": "tableName2",
        "tableName": "result16",
        "id": "d81e0c6d"
      }
    ],
    "tableRelations": [
      {
        "tbid": "18jmv7by04b7so6ps",
        "main": {
          "schema": "public",
          "aliasName": "tableName1",
          "tableName": "car_cross_info1",
          "id": "44ae872a"
        },
        "sub": {
          "schema": "public",
          "aliasName": "tableName2",
          "tableName": "result16",
          "id": "d81e0c6d"
        },
        "relationShip": "RIGHT_JOIN",
        "condition": "tableName1.car_type = tableName2.value"
      }
    ],

参照ID值,data2:

{
    "nodes":[
        {
            "type":"node",
            "size":"72*72",
            "shape":"flow-circle",
            "color":"#00BFFF",
            "label":"result16",
            "parentId":"85925db4-5bfa-4224-81c6-0df513cae72d",
            "desDatabase":"cardb",
            "desSchema":"public",
            "desTableName":"result16",
            "x":321.91668701171875,
            "y":192.4666748046875,
            "id":"d81e0c6d",
            "index":0
        },
        {
            "type":"node",
            "size":"72*72",
            "shape":"flow-circle",
            "color":"#00BFFF",
            "label":"car_cross_info1",
            "parentId":"85925db4-5bfa-4224-81c6-0df513cae72d",
            "desDatabase":"cardb",
            "desSchema":"public",
            "desTableName":"car_cross_info1",
            "x":127.91668701171875,
            "y":83.46667480468749,
            "id":"44ae872a",
            "index":1
        }
    ],
    "edges":[
        {
            "source":"44ae872a",
            "sourceAnchor":1,
            "target":"d81e0c6d",
            "targetAnchor":3,
            "id":"1f3d7a69",
            "label":"eee",
            "shape":"flow-smooth",
            "index":2
        }
    ]
}

改写取 edges 部分:

描述:图形是如下这样的.source对应节点起始id,target对应节点目标id.前一个节点的 target id 是下一个节点的 source id.

我这样写,不对.本来是4个节点(nodes),3条线(edges),输出结果是 4 条线.

const relationsSource = data2.edges.map(item => item.source)
const relationsTarget = data2.edges.map(item => item.target)
const rl = data1.filter(item => {
      return relationsSource.includes(item.main.id) && relationsTarget.includes(item.sub.id)
    })
    console.log(rl) 

回答:

只拿其中一个举例,另一个你自己应该会写了:

const idList = data2.nodes.map(item => item.id)
rs = data1.tableModels.filter(item => {
    return idList.includes(item.id)
})

rs 就是你要的 tableModels 筛选完的结果

本文地址:H5W3 » 【前端问题精选】过滤出相同ID的数据(基础问题)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址