比较数组并删除对象

时间:2018-06-20 02:30:44

标签: javascript

我在下面有这段代码,我试图通过将{"id": "Apple", "group": 1}graph进行比较以删除testArray的每个对象来删除整行graphtestArray相同,只是删除整行。但是我不确定如何完全删除它,对您的帮助将不胜感激

var graph = {   "nodes": [
    {"id": "Apple", "group": 1},
    {"id": "Cherry", "group": 2},
    {"id": "Tomato", "group": 3}
    ],
    "links": [
    {"source": "Apple", "target": "Cherry", "value": 1},
    {"source": "Cherry", "target": "Tomato", "value": 1},
    {"source": "Tomato", "target": "Apple", "value": 1}
    ]
    };

    var testArray = ['Apple'];

    var nodes = graph["nodes"];
    let removeNodes = nodes;
    removeNodes.forEach((obj) => {
        if (testArray.includes(obj.id.toString())) 
    });

4 个答案:

答案 0 :(得分:1)

如何循环testArray并在nodes数组中找到每个项目并将其删除?

编辑:如果要通过给定的键删除,则可以创建一个接受对象键和列表的函数。

var graph = {
  nodes: [
    {
      id: "Apple",
      group: 3
    },
    {
      id: "Cherry",
      group: 3
    },
    {
      id: "Tomato",
      group: 3
    },
    {
      id: "Lemon",
      group: 4
    },
    {
      id: "Grape",
      group: 5
    }
  ],
  links: [
    {
      source: "Apple",
      target: "Cherry",
      value: 1
    },
    {
      source: "Cherry",
      target: "Tomato",
      value: 1
    },
    {
      source: "Tomato",
      target: "Apple",
      value: 1
    },
    {
      source: "Lemon",
      target: "Grape",
      value: 1
    },
    {
      source: "Grape",
      target: "Lemon",
      value: 1
    }
  ]
};

function removeObject(key, arr) {
  arr.forEach((item) => {
    // get all nodes that has the value of item
    const foundNodes = graph.nodes.filter(node => arr.includes(node[key]));
    // get index of each found item
    foundNodes.forEach((node) => {
      const nodeIndex = graph.nodes.indexOf(node);
      // remove item by index
      graph.nodes.splice(nodeIndex, 1);
    })
  });
}


// find object by group
removeObject('group', [3]);
console.log(graph);

答案 1 :(得分:1)

只需在graph["nodes"]上使用Array.prototype.filter(),在testArray上使用Array.prototype.includes()并变异图形对象。

var graph = {   "nodes": [
    {"id": "Apple", "group": 1},
    {"id": "Cherry", "group": 2},
    {"id": "Tomato", "group": 3}
    ],
    "links": [
    {"source": "Apple", "target": "Cherry", "value": 1},
    {"source": "Cherry", "target": "Tomato", "value": 1},
    {"source": "Tomato", "target": "Apple", "value": 1}
    ]
    };

let testArray = ['Apple'];

graph["nodes"] = graph["nodes"].filter(x => !testArray.includes(x.id));

console.log(graph);

来自评论的临时请求:

如果组= 1则删除元素

graph["nodes"] = graph["nodes"].filter(x=> x.group !== 1)

答案 2 :(得分:0)

尝试一下。

使用索引,我们可以使用splice()删除元素。

我已经使用slice制作了节点数组的副本。

var graph = {
      "nodes": [
        { "id": "Apple", "group": 1 },
        { "id": "Cherry", "group": 2 },
        { "id": "Tomato", "group": 3 }
      ],
      "links": [
        { "source": "Apple", "target": "Cherry", "value": 1 },
        { "source": "Cherry", "target": "Tomato", "value": 1 },
        { "source": "Tomato", "target": "Apple", "value": 1 }
      ]
    };
    
    var testArray = ['Apple'];
    
    // var nodes = graph["nodes"];
    let removeNodes = graph["nodes"].slice();
    removeNodes.forEach((obj, index) => {
      if (testArray.includes(obj.id)) {   //Why toString()? obj.id is already a string.
        graph.nodes.splice(index, 1);
      }
    });

JSBin:https://jsbin.com/kokumipixi/edit?js,console

答案 3 :(得分:0)

我使用过滤器方法来解决这个问题。如果有条件,那就没有了

var graph = {   "nodes": [
        {"id": "Apple", "group": 1},
        {"id": "Cherry", "group": 2},
        {"id": "Tomato", "group": 3}
        ],
        "links": [
        {"source": "Apple", "target": "Cherry", "value": 1},
        {"source": "Cherry", "target": "Tomato", "value": 1},
        {"source": "Tomato", "target": "Apple", "value": 1}
        ]
        };
    
        var testArray = ['Apple'];
    
        var nodes = graph["nodes"];
        let removeNodes =  	nodes.filter((node)=>{
      		return testArray.includes(node.id);
      });
      console.log(removeNodes);

简单是最好的! 在这里拨弄:http://jsfiddle.net/zsr0ugfq/3/

好的,在这里避免混乱。这将显示已删除的项目。 刚放一个!在返回中,它将满足OP的需求。 我认为有时可以展示如何解决问题。