admin管理员组文章数量:1392054
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 T.J.Crowder
if (!selectedNodesArray.some(function(entry) { return entry == d.coreId; })) {
selectedNodesArray.push(d.coreId);
}
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 T.J.Crowder
if (!selectedNodesArray.some(function(entry) { return entry == d.coreId; })) {
selectedNodesArray.push(d.coreId);
}
Share
Improve this question
edited Nov 20, 2014 at 13:55
rekoDolph
asked Nov 20, 2014 at 13:47
rekoDolphrekoDolph
8231 gold badge12 silver badges32 bronze badges
3 Answers
Reset to default 4Why 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.
本文标签: How to check an array to see if a value is in thereJavaScriptStack Overflow
版权声明:本文标题:How to check an array to see if a value is in there, javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744782949a2624823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论