简体   繁体   中英

How to check an array to see if a value is in there, javascript

I am using javascript together with D3. I am creating a set of nodes and i want to be able to click them and an ID of that node is added into an array so that i can print that array to console to view the selected nodes and do whatever i wish to those selected. (basically select and deselect the node)

I have done a click event so the ID of the node goes to the selected array. But i want to check this array before it goes into the list to see if its already there, so no duplicate information.

selectedNodesArray=[];

for(var i = 0; i< selectedNodesArray.length; i++)
  {
  if(selectedNodesArray[i] === d.coreId)
  {
    console.log("that node is already selected");       
  } else 
  {  
    selectedNodesArray.push(d.coreId);
  }  

}
console.log(selectedNodesArray);
}

Above is my for loop, i dont understand why it doesnt work. any ideas ?


Answer by TJCrowder

if (!selectedNodesArray.some(function(entry) { return entry == d.coreId; })) {
    selectedNodesArray.push(d.coreId);
}

Why don't you use the "indexOf()" method of array to find out if the item is already present in the array? If it is present then the position of the item will be returned else -1 will be returned. This is much faster than going through For loop.

if(selectedNodesArray.indexOf(d.coreId)===-1){
   selectedNodesArray.push(d.coreId);
}else{
   console.log("that node is already selected"); 
}

Now in order to remove the already present item you can make use of splice(). First find the item using indexOf() then use splice() to remove that item from the list.

If I think I understand what you're asking, you just want to find out if an element is already in an array, you could use indexOf() .

if(selectedNodesArray.indexOf(d.coreId) === -1){
       // push here because a value of -1 means it's not in the array
}

Above is my for loop, i dont understand why it doesnt work. any ideas ?

You're doing a push for every element that doesn't match. So if your array has [1, 2, 3] and you're looking for 2, you'll end up with [1, 2, 3, 2, 2] because you've added 2 every time you see an entry that isn't 2.

You could fix that by using a flag and pushing after the loop if the flag isn't set, or just by breaking the loop when you find the entry and then pushing if i < selectedNodesArray.length .

But for me, this is a case for Array#indexOf (see Barry's answer ) or Array#some :

if (!selectedNodesArray.some(function(entry) { return entry == d.coreId; })) {
    selectedNodesArray.push(d.coreId);
}

Array#some calls your callback function once for each entry in the array, in order, until your callback returns true or it reaches the end of the array. It returns true if your callback ever returned true , or false if not. It exists on all modern browsers. If you need to supported IE8 or other similarly out-of-date browsers, it can be polyfilled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM