Best Jeanist
Best Jeanist

Reputation: 1129

Compare Array and remove object

I have this code below i'm trying to remove the entire row {"id": "Apple", "group": 1} by comparing the graph with the testArray to see for each object that the graph have in common with the testArray just remove the entire row. But i'm not really sure on how to complete remove it any help would be greatly appreciated

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())) 
    });

Upvotes: 0

Views: 65

Answers (4)

wobsoriano
wobsoriano

Reputation: 13434

How about looping the testArray and find each item in the nodes array and remove it?

Edit: If you want to remove by given key, you can make a function that accepts the object key and the list.

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);

Upvotes: 1

Isaac
Isaac

Reputation: 12874

Just use Array.prototype.filter() on graph["nodes"] and Array.prototype.includes() on testArray and mutate graph object.

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);

Ad-hoc request from comments:

To remove element if group = 1

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

Upvotes: 1

Faheem
Faheem

Reputation: 1166

Try this.

Using the index, we can remove the element using splice().

I've used slice to make a copy of the nodes array.

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

Upvotes: 0

keysl
keysl

Reputation: 2167

My take on this using the filter approach. No more if conditions

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);

Simple is the best! Fiddle here: http://jsfiddle.net/zsr0ugfq/3/

Okay to avoid confusion here. this will show the removed Items. Just put a ! in the return and it will suit the OP's need. I think sometimes it's okay to show how things can be solve.

Upvotes: 0

Related Questions