JavaScript remove equivalent object in array

Peter

I'm writing a d3 force graph to make the update look fliud I implemented it like the Modifying a Force Layout example. But unline in the example I want a dynamic updating behaviour. Given the situation I already called initializeGraphData(json); once, my nodes- array contains a set of node objects. On 2nd call I push every json.nodes object to the nodes- array. Then I get the difference between the nodes- array and the json.nodes- array. For each different object I want to remove it from the nodes array by giving in it's index and the parameter 1 to remove only one element.

function initializeGraphData(json){
    json.nodes.forEach(function(node){
        nodes.push(node);
    });
    json.links.forEach(function(link){
        links.push(link);
    });
    var difInNodes = nodes.diff(json.nodes);
    difInNodes.forEach(function(node){
        nodes.slice(nodes.indexOf(node), 1);
    });
    var difInLinks = links.diff(json.links);
    difInLinks.forEach(function(link){
        links.slice(links.indexOf(link), 1);
    });
    nodes.forEach(function(node){
        java.alert("In nodes array: " + node.name);
    });

    draw();
    checkForLinked(json);
    checkForOptions(json);
}

One node element might look like this

{"img":"ron.png","concept":null,"name":"Ron Weasley","id":1,"shortname":"Ron","properties":[{"property":"Animal","value":"Rat"}],"group":"#867b69"}

I do know that these are not the same objects when I try to remove them, so I thought I could use the node.id property, which didn't work as well.

var difInNodes = nodes.diff(json.nodes);
difInNodes.forEach(function(node){
   nodes.slice(node.id, 1);
}); 

How can I remove an object in an array that is equivalent by content to one in another array 1:1?

T.J. Crowder

slice doesn't remove entries from the array, it creates a new array with a subset of the entries from the original.

Given where and how you're using it, I believe you meant to use splice (with a p).

But you're also using node.id as though it were the index of the entry, which isn't likely to be correct (and certainly won't be correct after the first time you change the array). So first you need to find the right index, then use splice to remove the entry. ES2015 adds findIndex to the Array.prototype which is useful for finding the index of an entry based on a callback function; it can be shimmed/polyfilled on ES5 and earlier engines.

So:

var difInNodes = nodes.diff(json.nodes);
difInNodes.forEach(function(node){
   var index = nodes.findIndex(function(entry) { return entry.id == node.id; });
   if (index !== -1) {
     nodes.splice(node.id, 1);
     //     ^
   }
});

Alternately, you could use Array#filter to build a new array each time with only the entries you want:

var difInNodes = nodes.diff(json.nodes);
difInNodes.forEach(function(node){
   nodes = nodes.filter(function(entry) { return entry.id != node.id; });
});

Which you use is up to you, largely depending on what else will have a reference to the array (since the second method creates a new array).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove object in array by id javascript

From Javascript

Remove duplicates in an object array Javascript

From Dev

Remove array from javascript object

From Dev

Javascript Object to array then remove element

From Dev

remove an object from an object array in javascript

From Dev

Javascript - remove object out of nested Object array

From Dev

Remove entire object from a json array javascript

From Dev

Javascript array object need to remove element

From Dev

Javascript fastest way to remove Object from Array

From Dev

Remove empty object from an array javascript

From Dev

Remove multiple object from array Javascript

From Javascript

Remove Object from Array using JavaScript

From Dev

Remove an object in an array of arrays in JavaScript or Typescript

From Dev

remove and replace item from object in array javascript

From Dev

In Javascript remove keys from object not in an Array

From Dev

Javascript Remove object from array not removing the selected

From Dev

Javascript : filter array of object remove all objects

From Dev

Remove and update overlapping indexes in array of object in javascript

From Dev

How to remove "integer" array from object in JavaScript

From Dev

Remove the extra " " in "lng " in array object javascript

From

golang javascript object equivalent

From Dev

JavaScript- Remove object from parent array with an object's method

From Dev

Javascript remove object from array using object variable

From Dev

Javascript remove object from array if object contains string

From Dev

How to remove duplicate object based on condition from an array of object in javascript

From Dev

Javascript: Remove object from array on function call to object

From Dev

Javascript to remove an object from an array if one element in the object contains a string

From Dev

JavaScript remove object from array if value exists in other array

From Dev

Remove some array object comparing with another array JavaScript

Related Related

  1. 1

    Remove object in array by id javascript

  2. 2

    Remove duplicates in an object array Javascript

  3. 3

    Remove array from javascript object

  4. 4

    Javascript Object to array then remove element

  5. 5

    remove an object from an object array in javascript

  6. 6

    Javascript - remove object out of nested Object array

  7. 7

    Remove entire object from a json array javascript

  8. 8

    Javascript array object need to remove element

  9. 9

    Javascript fastest way to remove Object from Array

  10. 10

    Remove empty object from an array javascript

  11. 11

    Remove multiple object from array Javascript

  12. 12

    Remove Object from Array using JavaScript

  13. 13

    Remove an object in an array of arrays in JavaScript or Typescript

  14. 14

    remove and replace item from object in array javascript

  15. 15

    In Javascript remove keys from object not in an Array

  16. 16

    Javascript Remove object from array not removing the selected

  17. 17

    Javascript : filter array of object remove all objects

  18. 18

    Remove and update overlapping indexes in array of object in javascript

  19. 19

    How to remove "integer" array from object in JavaScript

  20. 20

    Remove the extra " " in "lng " in array object javascript

  21. 21

    golang javascript object equivalent

  22. 22

    JavaScript- Remove object from parent array with an object's method

  23. 23

    Javascript remove object from array using object variable

  24. 24

    Javascript remove object from array if object contains string

  25. 25

    How to remove duplicate object based on condition from an array of object in javascript

  26. 26

    Javascript: Remove object from array on function call to object

  27. 27

    Javascript to remove an object from an array if one element in the object contains a string

  28. 28

    JavaScript remove object from array if value exists in other array

  29. 29

    Remove some array object comparing with another array JavaScript

HotTag

Archive