Little update (Just for clarify the "duplicate question flag")
This question is slightly different from this one, because in there we are talking about some ways to loop an array using javascript. In my case we are talking about 'how to modify' an existing associative array of arrays using javascript. That's not exactly the same concept, cause for my specific case we could try to apply also something different from a loop.
I have this data structure obj
reproduced with a console.log
in chrome browser:
var obj = {
key_1: [{
prop_1: "some_value",
prop_2: "some_value_2",
prop_3: "some_value_3",
prop_4: "some_value",
prop_5: "some_value"
}, {
prop_1: "some_value",
prop_2: "some_value_2",
prop_3: "some_value_3",
prop_4: "some_value",
prop_5: "some_value"
}],
key_2: [{
prop_1: "some_value",
prop_2: "some_value_2",
prop_3: "some_value_3",
prop_4: "some_value",
prop_5: "some_value"
}, {
prop_1: "some_value",
prop_2: "some_value_2",
prop_3: "some_value_3",
prop_4: "some_value",
prop_5: "some_value"
}]
};
Loop and push inside a new array
I'm trying to get as output result from obj
a new array called new_arr
filled with only the prop_2
and prop_3
and the key value
. Like in the example result below:
new_arr = [
["some_value_2","some_value_3","key_1"],
["some_value_2","some_value_3","key_1"],
["some_value_2","some_value_3","key_2"],
["some_value_2","some_value_3","key_2"]
];
So I have tried to perform a for loop and inside it I have push only the required properties, like the example code below:
new_arr = [];
for (var key in obj) {
new_arr.push(
obj[key][0].prop_2 + ', ' +
obj[key][0].prop_3 + ', ' +
key);
}
console.log(new_arr);
Output problem
The problem that stuck me is related to the fact that the new_arr
contains just the [0] index array. So the array at index 1 is not pushed inside. Is there a way to access to all the index properties and perform the loop for all the index (not only for the case index[0])?
Any suggestions? Thanks in advice!