Javascript 手柄-但是将一个数组中的所有元素作为一个元素的值推入另一个数组
我的记录json文件中有一个元素,如下所示:Javascript 手柄-但是将一个数组中的所有元素作为一个元素的值推入另一个数组,javascript,json,handlebars.js,Javascript,Json,Handlebars.js,我的记录json文件中有一个元素,如下所示: "custitem_list": [ { "internalid": "1", "name": "FLAT AND DULL" }, { "internalid": "2", "name": "F
"custitem_list": [
{
"internalid": "1",
"name": "FLAT AND DULL"
},
{
"internalid": "2",
"name": "FRIZZY"
}
]
现在我需要将列表名从记录中推送到另一个json记录中,作为属性元素中的选项值
"attributes": [
{
"id": 0,
"name": "Custlist",
"position": 0,
"visible": true,
"variation": false,
"options": [
{
"FLAT AND DULL",
"FRIZZY"
}
]
}
]
我尝试过以下方法,但都没有达到预期效果:
{
"id": 0,
"name": "Custlist",
"position": 0,
"visible": true,
"variation": false,
"options": [
{
{{#each record.custitem_list}}
{{#if @index}}
"{{{name}}}"
,{{/if}}
{{/each}}
}
]
}
{
"id": 0,
"name": "Custlist",
"position": 0,
"visible": true,
"variation": false,
"options": [
{
{{#each record.custitem_list}}
{{#if @index}},{{/if}}
{"name": "{{{name}}}"}
{{/each}}
]
}
{
"id": 0,
"name": "Custlist",
"position": 0,
"visible": true,
"variation": false,
"options": [
{
{{#each record.custitem_list }}
{{#if @index}},
{{/if}}
"{{{name}}}"
{{/each}}
}
]
}
谁能给我点启示吗
非常感谢试试这个:
const names = obj.custitem_list.map(item => item.name);
obj2.attributes.options = names
下次请尽量给出没有错误的有效代码,并将其格式化以便于阅读。这是否回答了您的问题?