简体   繁体   中英

How to push values to an array of object in node js

"default_tabs" : [ 
    {
        "value" : "Ice"
    }, 
    {
        "value" : "Gold"
    } 
 ]

I want to assign this values into another array in such a way that it should look like below

selected_values :  [{"values" : { "Ice" : "Edit","Gold" : "Edit" },"role" : "Admin"}]

for this i prepared the below,

 default_tabs.forEach(function(i,v){
       selected_values.push('values':v)
    }) 

I know i am wrong can anyone help me please.Thanks.

You could iterate the array and add the properties to the first item of result.selected_values 's property values .

 var object = { default_tabs: [{ value: "Ice" }, { value: "Gold" }] }, result = { selected_values: [{ values: {}, role: "Admin" }] }; object.default_tabs.forEach(function (a) { result.selected_values[0].values[a.value] = 'Edit'; }); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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