简体   繁体   English

如何将值推送到节点 js 中的对象数组

[英]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 .您可以迭代数组并将属性添加到result.selected_values的属性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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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