通过匹配另一个数组中的多个值来过滤数组

时间:2017-05-29 10:44:12

标签: javascript arrays

选择对象后,对象会添加到nodeArray[],某些节点连接在一起,这些链接存储在linkArray[]中,每个对象都包含源ID和节点连接的目标ID

我需要过滤linkArray[],以便它只返回sourcetarget都位于nodeArray[]的对象。

至于浏览类似的问题,我有以下几点:



var linkArray = [{
  "conID": "100",
  "source": "10",
  "target": "11"
}, {
  "conID": "101",
  "source": "11",
  "target": "12"
}, {
  "conID": "102",
  "source": "12",
  "target": "13"
}, {
  "conID": "103",
  "source": "13",
  "target": "14"
}, {
  "conID": "386",
  "source": "55",
  "target": "32"
}];
var nodeArray = [{"id": "10"}, {"id": "11"}, {"id": "12"}];

function filterArray(array, filter) {
  var myArrayFiltered = [];
  for (var i = 0; i < array.length; i++) {
    for (var j = 0; j < filter.length; j++) {
      if (array[i].source === filter[j].id) {
        myArrayFiltered.push(array[i]);
      }
    }
  }
  return myArrayFiltered;
}

myArrayFiltered = filterArray(linkArray, nodeArray);

document.body.innerHTML = '<pre>'+ JSON.stringify(myArrayFiltered, null, 4) +'</pre>';
&#13;
&#13;
&#13;

我已尝试添加if语句以包含目标ID,但我不确定我是否正确理解它。

结果是返回source中与id匹配nodeArray[]的所有链接。

[
    {
        "conID": "100",
        "source": "10",
        "target": "11"
    },
    {
        "conID": "101",
        "source": "11",
        "target": "12"
    },
    {
        "conID": "102",
        "source": "12",
        "target": "13"
    }
]

我需要帮助的是过滤数组,以便它只返回sourcetarget ID都在nodeArray[]中的对象。

选择节点10,11和12后所需的结果将只是2个对象,因为节点13不在选择范围内。

[
    {
        "conID": "100",
        "source": "10",
        "target": "11"
    },
    {
        "conID": "101",
        "source": "11",
        "target": "12"
    }
]

希望这很清楚,谢谢!

5 个答案:

答案 0 :(得分:4)

你可以试试这个

&#13;
&#13;
var linkArray = [{
  "conID": "100",
  "source": "10",
  "target": "11"
}, {
  "conID": "101",
  "source": "11",
  "target": "12"
}, {
  "conID": "102",
  "source": "12",
  "target": "13"
}, {
  "conID": "103",
  "source": "13",
  "target": "14"
}, {
  "conID": "386",
  "source": "55",
  "target": "32"
}];
var nodeArray = [{
  "id": "10"
}, {
  "id": "11"
}, {
  "id": "12"
}];

function filterArray(array, filter) {
  // Get all the required ids
  var ids = filter.map(function(f) {
    return f.id;
  });
  return array.filter(function(a) {
    // Check if both source and target are present in list of ids
    return ids.indexOf(a.source) !== -1 && ids.indexOf(a.target) !== -1;
  });
}

var myArrayFiltered = filterArray(linkArray, nodeArray);
console.log(myArrayFiltered)
&#13;
&#13;
&#13;

<强>更新

已移除forEach + push并已使用filter

答案 1 :(得分:1)

您可以使用哈希表ids并测试值。

&#13;
&#13;
var linkArray = [{ conID: "100", source: "10", target: "11" }, { conID: "101", source: "11", target: "12" }, { conID: "102", source: "12", target: "13" }, { conID: "103", source: "13", target: "14" }, { conID: "386", source: "55", target: "32" }],
    nodeArray = [{ id: "10" }, { id: "11" }, { id: "12" }],
    ids = nodeArray.reduce(function (o, a) {
        o[a.id] = true;
        return o;
    }, Object.create(null)),
    result = linkArray.filter(function (a) {
        return ids[a.source] && ids[a.target];
    });

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

ES6 with

  • Set代表想要的id
  • Array#filter仅获取匹配的项目,
  • 一个keys数组,其中包含匹配['source', 'target']
  • 的所需属性
  • Array#every用于检查所有密钥,
  • Set#has,用于检查所需的密钥是否在集合中。

&#13;
&#13;
var links = [{ conID: "100", source: "10", target: "11" }, { conID: "101", source: "11", target: "12" }, { conID: "102", source: "12", target: "13" }, { conID: "103", source: "13", target: "14" }, { conID: "386", source: "55", target: "32" }],
    nodes = [{ id: "10" }, { id: "11" }, { id: "12" }],
    keys = ['source', 'target'],
    result = links.filter(
        (s => a => keys.every(k => s.has(a[k])))(new Set(nodes.map(n => n.id)))
    );

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以将Array#filter用于以下条件

nodeArray.find(n=>n.id === e.source ) && nodeArray.find(n=>n.id === e.target)

条件使用Array#find - 如果找不到具有源ID或目标ID的节点,它将返回undefined

var linkArray = [{"conID": "100","source": "10","target": "11"}, {"conID": "101","source": "11","target": "12"}, {"conID": "102",   "source": "12",   "target": "13"}, {"conID": "103","source": "13","target": "14"}, {"conID": "386","source": "55","target": "32" }]; 
var nodeArray = [{"id": "10"}, {"id": "11"}, {"id": "12"}];

var filteredArray = linkArray.filter(e=> nodeArray.find(n=>n.id === e.source ) && nodeArray.find(n=>n.id === e.target));

console.log(filteredArray);

答案 3 :(得分:0)

您可以将nodeArray转换为键值,并使用它来检查source

中每个对象的targetlinkArray

&#13;
&#13;
var linkArray = [{"conID": "100","source": "10","target": "11"}, {"conID": "101","source": "11","target": "12"}, {"conID": "102",   "source": "12",   "target": "13"}, {"conID": "103","source": "13","target": "14"}, {"conID": "386","source": "55","target": "32" }];

var nodeArray = [{"id": "10"}, {"id": "11"}, {"id": "12"}];

var nodes ={};
var myArrayFiltered = [];

for(var i =0; i<nodeArray.length;i++){
  nodes[nodeArray[i].id] = true;
}

for(var i =0; i<linkArray.length;i++){
  if(nodes[linkArray[i].source] && nodes[linkArray[i].target])
    myArrayFiltered.push(linkArray[i])
}

console.log(myArrayFiltered)
&#13;
&#13;
&#13;

答案 4 :(得分:0)

var linkArray = [{
  "conID": "100",
  "source": "10",
  "target": "11"
}, {
  "conID": "101",
  "source": "11",
  "target": "12"
}, {
  "conID": "102",
  "source": "12",
  "target": "13"
}, {
  "conID": "103",
  "source": "13",
  "target": "14"
}, {
  "conID": "386",
  "source": "55",
  "target": "32"
}];
var nodeArray = [{"id": "10"}, {"id": "11"}, {"id": "12"}];

function filterArray(array, filter) {
  // map the nodeArray to an array of ids: ["10", "11", "12"]
  // could be done outside the function if needed
  var ids = nodeArray.map(function(item) {return item.id});

  // filter the array only returning itemes that have 'target' and 'source' in ids
  return array.filter(function(item) {
    return ids.includes(item.target) && ids.includes(item.source)
  })
}

myArrayFiltered = filterArray(linkArray, nodeArray);

document.body.innerHTML = '<pre>' + JSON.stringify(myArrayFiltered, null, 4) + '</pre>';