Chr*_*row 4 javascript arrays json ecmascript-6 angular
我有一个类似于这样的JSON结构:
[
{
cells: [
{ id: "1", cellType: 3, widget: { id: 1, description: "myDesc"} },
{ id: "2", cellType: 4, widget: { id: 2, description: "myDesc2"} }
]
},
{
cells: [
{ id: "3", cellType: 5, widget: { id: 3, description: "myDesc3"} }
]
},
...
]
Run Code Online (Sandbox Code Playgroud)
如何widget使用EcmaScript(或Angular 2+中可用的任何内容)将每个值的值转换为单独的数组,而不使用库(包括JQuery)?我需要一个像这样的最终数组:
[
{
id: 1,
description: "myDesc"
},
{
id: 2,
description: "myDesc2"
},
...
]
Run Code Online (Sandbox Code Playgroud)
更新
(感谢@Felix Kling的第一部分) - 我发现我可以用这个来获取所有小部件:
JSON.parse(json)[0].forEach( c => c.cells.forEach( c2 => console.log(c2.widget)));
Run Code Online (Sandbox Code Playgroud)
let input = [
{
cells: [
{ id: "1", cellType: 3, widget: { id: 1, description: "myDesc"} },
{ id: "1", cellType: 4, widget: { id: 2, description: "myDesc2"} }
]
},
{
cells: [
{ id: "3", cellType: 5, widget: { id: 3, description: "myDesc3"} }
]
},
];
let result = input.reduce((result, current) => {
return result.concat(current.cells.map(x => x.widget));
}, [])
console.log(result);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |