0

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

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that works great but may i ask if i want to do it another way and not by comparing the testArray for example i want to remove it based on the group number so like if 'group:1' remove it
Sorry but it only removes one of the object may i ask if you can check my code here is my JSfiddle jsfiddle.net/tbj82hef/1
1

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)

Comments

0

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.

2 Comments

Why are you returning the removed object? Did you understand the OPs post?
I understand it, but decided to show how the incuded in testArray can be remove. Just put ! in the return and it will be suffice the OP's need.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.