简体   繁体   中英

Modify array structure in javascript

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!

Use nested loops.

 var arr = { 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" }] }; var newarr = []; for (var key in arr) { arr[key].forEach(obj => newarr.push([obj.prop_2, obj.prop_3, key])); } console.log(newarr); 

Note that the value you push should be an array, according to your desired output. A string with values concatenated with commas is not the same thing.

arr[key].forEach(function)

loops over the array in arr[key] , calling the function on each element, which are the nested objects in your data. The function above creates an array containing the prop_2 property, prop_3 property, and key , and pushes that onto the new_arr array.

Fot the modified scenario, just add another level of nested for loop:

for (var key1 in obj) {
    for (var key2 in obj[key1]) {
        obj[[key1][key2].forEach(o => newarr.push([o.prop_2, o.prop_3, key1, key2]));
    }
}

Make an inner loop for the index

for (var key in arr) {
for (var innerKey in arr[key]) {
   new_arr.push( [
   arr[key][innerKey].prop_2,  
   arr[key][innerKey].prop_3, 
   key ])
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM