You have a complex data structure that you want to filter the ID's out of.
So you have a nested array (the complex structure) like this:
( NOTE: This is an example structure )
uniq_nds = [
{
data: {
id: 1,
other: '...'
}
},
{
data: {
id: 2,
other: '...'
}
},
{
data: {
id: 1,
other: '...'
}
},
]
With some duplicate id
fields deeply nested.
These get filtered by your fast
method which will only return data that hasn't already been identified through the id
field.
Therefore your array will only contain unique ID's:
uniq_nds = [
{
data: {
id: 1,
other: '...'
}
},
{
data: {
id: 2,
other: '...'
}
}
]
Then comes the part where you essentially map
the ID's to an array using a for
loop.
Your loop however looks a bit different from the usual loop you'd see as you're using the length
of the id
array that you're continuously filling with elements, that means that if the length
is 0
, your first element will be placed in id[0]
. When assigning an element to id[0]
the length
property changes and increments by 1
(you're adding 1 item to the array) so the next time you loop the length
property on the id
array will return 1
therefore setting the next item in the array.
In the end this will cause you to end up with an array filled with ID's.
Now for the assignment part of your operation, e.g.
id[id.length] = uniq_nds[x]['data']['id']
Here you're looping with the variable x
, which happens to also automatically get incremented by the for
loop every time it runs.
This means that the first time your loop runs, it will fetch index 0
of array uniq_nds
. The result of that is an object:
console.log(uniq_nds[0]);
=> {
data: {
id: 1
}
}
You can then use the bracket notation to access the data
property of uniq_nds[0]
which will return another object e.g.
console.log(uniq_nds[0]['data']);
=> {
id: 1
}
And when you've done that you can now finally access that id
that you want to have in your array e.g.
console.log(uniq_nds[0]['data']['id']);
=> 1
Now the entire explanation here might be a bit tough to chew on as a beginner but I hope I explained it well enough for you.
All that you're doing is assigning an array index to a value so that you can call / loop through them later with more for
loops or using functions like map
.
You're doing this in a bit of a complex way too, you don't need to do:
id[id.length] = uniq_nds[x]['data']['id'];
In your loop at all, instead you can use push
which will dynamically append the item at the end of the array:
id.push(uniq_nds[x]['data']['id']);
This will get rid of the id.length
usage which doesn't really explain what you're doing like push
does (pushing an element into an array).
Also, the map
function I keep mentioning can be used to do this in one line.
id = uniq_nds.map(function(obj) { return obj.data.id; });
Since uniq_nds
returns an array, it can be looped over using map
, which runs the provided callback function (function(obj) { return obj.data.id; }
) for every item in that array, returning an array of values returned by the callback.
This allows you to easily transform your complex data structure to a simple array of ID's.