nodesData=[
{name:'name1'},
{name:'name2'},
{name:'name1'},
{name:'name3'},
]
uniqData=[
{name:'name1'}
{name:'name2'}
{name:'name3'}
]
for (let i = 0; i < nodesData.length; i += 2) {
const currentSource = nodesData[i];
const currentTarget = nodesData[i + 1];
const currentData = {
source: uniqData.indexOf(currentSource),
target: uniqData.indexOf(currentTarget),
};
}
So I've got two lists with objects. First list is list with objects that contains names, and other list is list generated with function that I've made for removing duplicates.The problem is when I use for loop and loop through nodesData list only the first time I get only for source index of -1...why? and other indexOf work expected.
So the console.log would be:
source:-1, target:1
source:0, target:2
,inuniqData. Second, your code logsource: -1, target: -1twice-1forcurrentDatain all iteration. You want to generateuniqData. So,uniqDatais empty. andindexOfuniqDatawill always give you-1indexOfwill always return-1because for us the values seem similar but actually they aren't