Badshah01
Badshah01

Reputation: 19

JavaScript loops and Arrays Issue

I have the following codes which I am unable to understand. I have just started to learn JavaScript. Please can someone describe this code snippet.

function cyst(data, options) {  
    var uniq_nds = fast(sumer),
              id = [];

            for(var x = 0 ; x < uniq_nds.length; x++) {
                id[id.length] = uniq_nds[x]['data']['id'];          
            }
}

The code for the fast method:

function fast(a) {
    var seen = {};
    var out = [];
    var len = a.length;

    for(var i = 0; i < len; i++) {
        var item = a[i]['data']['id'];
        if (seen[item] !== 1) {
            seen[item] = 1;
            out[out.length] = a[i];
        }
    }

    return out;
}

Especially I want to know about this line. id[id.length] = uniq_ndes[x]['data']['id'];

Upvotes: -1

Views: 72

Answers (3)

SidOfc
SidOfc

Reputation: 4594

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.

Upvotes: 0

cralfaro
cralfaro

Reputation: 5948

the object uniq_nds is an array of objects, and he is trying to extract all id's from this initial array structure to an array with only the id's not the full object. Is a little bit confusing because is using different index in the array/matrix. Would be the same if use this:

for(var x = 0 ; x < uniq_nds.length; x++) {
            id[x] = uniq_nds[x]['data']['id'];          
}

An example to accomplish this situation would be something like this:

function test () {
    var id = {id:'12'}
    var object = {name:"Fiat", data:{id:'12', name:'another detail'}}; 
    var array=[];
    array[0]=object;
    array[1]=object;

    for (var i = 0; i < array.length; i++){
        var id = array[i]['data']['id']
        var name = array[i]['data']['name']
        console.log(id + " " + name);
    }
}

test();

Upvotes: 1

Will Sheppard
Will Sheppard

Reputation: 3479

uniq_ndes looks like a multi-dimensional array. This line:

id[id.length] = uniq_ndes[x]['data']['id'];

appears to be assigning the contents of a location in uniq_ndes to a location in the id array.

See similar question here: JavaScript multidimensional array, or just google "javascript multi-dimensioal array".

Upvotes: 0

Related Questions