0

I've an array of objects and I'm trying to get the values of it, although the variables inside are not predefined. The problem is that I don't know how to do that.

Array:

Array[3]: [
    {
        id: "1",
        name: "name1",
        lastname: "lastname1"
    },
    {
        id: "2",
        name: "name2",
        lastname: "lastname2"
    },
    {
        id: "3",
        name: "name3",
        lastname: "lastname3"
    }
]

Javascript:

this.myFunction = function(response) {
    var iLength = response.data.length;

    for(var i=0;i<iLength;i++) {
        var jLength = Object.keys(response.data[i]).length;

        for(var j=0;j<jLength;j++) {
            // ???
        }
    }
}

So instead of accessing the values this way,

response.data[i].id
response.data[i].name
response.data[i].lastname

I would rather access it in this way:

response.data[i]."somethingSomething[j]";

Is that possible? I'm fairly new to AngularJS, so I might be approaching this matter not in right way. Any help would be appreciated.

Edit: I have formulated my question better.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Artur D.
  • 29
  • 4
  • may i know what is your final required output,do u want to store them in something or do u want to display them some where/? – Sa E Chowdary Dec 15 '16 at 10:04
  • 1
    Try `response.data[i][somethingSomething[j]]` instead of `response.data[i].somethingSomething[j]`. Is that what you wanted to do? – Cleared Dec 15 '16 at 10:04
  • See also: http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable – T.J. Crowder Dec 15 '16 at 10:06
  • I want to store the results inside another object. I actually don't know how to call the values inside response.data. I'm looking for something like "response.data[i].value[j]" or "response.data[i].text[j]". – Artur D. Dec 15 '16 at 10:09
  • what do you mean by 'array accessed dynamically' ? When using square brackets and indices it is accessed (dynamically). You can use a name on an object keys... It's not for arrays, which are lists basically... Be more precise. – mguijarr Dec 15 '16 at 10:12
  • I needed a way to iterate through my array without knowing the keys inside my objects in advance. @Nina Scholz answer helped me to solve the problem. I should have been more precisely with my questions. Thanks guys. – Artur D. Dec 15 '16 at 10:21

2 Answers2

3

You could iterate over the keys.

for (var i = 0; i < iLength; i++) {
    var keys = Object.keys(response.data[i]);

    for (var j = 0; j < keys.length; j++) {
        console.log(response.data[i][keys[j]]);
    }
}

But if you know the keys in advance, you could use an array with the keys and maintain a stable order while iterating.

var keys = ['id', 'name', 'lastname'];
for (var i = 0; i < iLength; i++) {
    for (var j = 0; j < keys.length; j++) {
        console.log(response.data[i][keys[j]]);
    }
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can use for ... in ... like this :

var obj = {
  id: "1",
  name: "name1",
  lastname: "lastname1"
};

for (key in obj) {
  if (obj.hasOwnProperty(key))
    document.body.innerHTML += key + "<br>";
}
user2226755
  • 12,494
  • 5
  • 50
  • 73