Javascript 组合嵌套的对象数组
我拥有以下阵列:Javascript 组合嵌套的对象数组,javascript,typescript,Javascript,Typescript,我拥有以下阵列: const myArray=[ { id:1, 选项:{ a:[ {name:“test-a”,值:10}, {name:“test-b”,值:10}, ], b:[ {name:“test-a”,值:10}, {name:“test-b”,值:10}, ], }, }, { id:2, 选项:{ a:[ {name:“test-a”,值:10}, {name:“test-b”,值:10}, ], b:[ {name:“test-a”,值:10}, {name:“test-b”
const myArray=[
{
id:1,
选项:{
a:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
b:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
},
},
{
id:2,
选项:{
a:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
b:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
},
},
];
我想减少数组以仅获取选项
属性内容并汇总项目。看起来是这样的:
const myNewArray={
a:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
b:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
};
我试过了,但我知道这根本不是正确的方法:
const myArray=[
{
id:1,
选项:{
a:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
b:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
},
},
{
id:2,
选项:{
a:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
b:[
{name:“test-a”,值:10},
{name:“test-b”,值:10},
],
},
},
];
常量myOptions=myArray.map((点)=>point.options);
const combinedOptions=myOptions.reduce((option)=>(option={…option}));
console.log(组合选项)代码>向我们展示您的尝试。因此,这不是一个免费的代码编写服务。这里的目的是让你发布你解决自己问题的尝试,当这些尝试没有达到预期效果时,其他人会提供帮助。请参阅和array。您要查找的是reduce
。您的代码更新似乎执行了要求“否”的操作?如果不提供更多details@charlietfl不,它没有,因为我想得到财产a的所有值,即使是双倍的。但有了这个,我只得到了第一个选项。