我需要用Javascript转换这些对象:
const tables = [
{
table_name: "Table",
columns: [
{
column_name: 'column_1',
values: [{value: 'data1_c1', other:25}, {value: 'data2_c1', other:25}]
},
{
column_name: 'column_2',
values: [{value: 'data1_c2', other:30}, {value: 'data2_c2', other:30}]
},
{
column_name: 'column_3',
values: [{value: 'data1_c3', other:40}, {value: 'data2_c3', other:40}]
}
]
}
]
为此数组
result = [['data1_c1, 'data2_c1'], ['data1_c2, 'data2_c2'], ['data1_c3, 'data2_c3']]
请,我需要此练习的帮助。
我正在尝试使用此
let result = []
tables.map(table =>(
table.columns.map(column => (
column.values.map(value => (
result.push(value.value)
))
))
))
答案 0 :(得分:0)
这可能是一个简单的答案
var result = []
table[0].columns.forEach(colum => {
var cols = []
colum.values.forEach(valueObj => cols.push(valueObj.value))
result.push(cols)
})
答案 1 :(得分:-1)
如果tables
数组中只有一项,则可以使用
const result = tables[0].columns.map((column) => {
return column.values.map((item) => item.value)
})
如果您有多个项目,则要复杂一些:
const result = tables.reduce((acc, table) => {
return acc.concat(
table.columns.map(column => {
return column.values.map(item => item.value);
})
);
}, []);
const tables = [
{
table_name: "Table",
columns: [
{
column_name: "column_1",
values: [
{ value: "data1_c1", other: 25 },
{ value: "data2_c1", other: 25 }
]
},
{
column_name: "column_2",
values: [
{ value: "data1_c2", other: 30 },
{ value: "data2_c2", other:30 }
]
},
{
column_name: "column_3",
values: [
{ value: "data1_c3", other: 40 },
{ value: "data2_c3", other: 40 }
]
}
]
}
];
const result1 = tables[0].columns.map((column) => {
return column.values.map((item) => item.value)
})
console.log(result1);
const result2 = tables.reduce((acc, table) => {
return acc.concat(
table.columns.map(column => {
return column.values.map(item => item.value);
})
);
}, []);
console.log(result2);