有没有办法从这些对象中提取数据值
is there any way to extract the data values from these objects
我有如下图所示的数据,我正在尝试从下图中的 json 中提取 Data
数组。
数据具有 condition_type
等键值和其他值。
我可以使用下面的代码获得每个单独的值
const submitcode = (data) => {
const tasks = Object.values(data);
console.log(tasks[0].Data[0]);
}
console.log(tasks[0].Data[0]);
任何人都可以建议是否有任何方法可以使用 React JS 从这 6 个对象中获取 Data
数组。
非常感谢
这是 returns 个 Data
数组
的简单片段
const array = [
{Cols: [], Data: [{condition_type: "1"}], title: "Environmental Condition"},
{Cols: [], Data: [{condition_type: "2"}], title: "Ventilation"},
{Cols: [], Data: [{condition_type: "3"}], title: "Thermal Comfort"},
{Cols: [], Data: [{condition_type: "4"}], title: "Internal Loads"},
{Cols: [], Data: [{condition_type: "5"}], title: "Exhaust"},
{Cols: [], Data: [{condition_type: "6"}], title: "Misc"},
]
console.log(array.map(({Data: [val]})=>val))
或者如果您想直接访问特定键的值
const array = [
{Cols: [], Data: [{condition_type: "1"}], title: "Environmental Condition"},
{Cols: [], Data: [{condition_type: "2"}], title: "Ventilation"},
{Cols: [], Data: [{condition_type: "3"}], title: "Thermal Comfort"},
{Cols: [], Data: [{condition_type: "4"}], title: "Internal Loads"},
{Cols: [], Data: [{condition_type: "5"}], title: "Exhaust"},
{Cols: [], Data: [{condition_type: "6"}], title: "Misc"},
]
console.log(array.map(({Data: [{condition_type}]})=>condition_type))
考虑到您在屏幕截图中的数组称为 tasks
,您可以使用具有对象和数组解构的映射:
tasks.map(({ Data: [el] }) => el);
const tasks = [{ Data: [{ a: 1, b: 2 }] }, { Data: [{ a: 3, b: 4 }] }];
console.log(tasks.map(({ Data: [el] }) => el));
或者也有点 hacky,对数组也使用对象析构两次,使用数组索引:
tasks.map(({ Data: { 0: el } }) => el);
const tasks = [{ Data: [{ a: 1, b: 2 }] }, { Data: [{ a: 3, b: 4 }] }];
console.log(tasks.map(({ Data: { 0: el } }) => el));
您可以映射父项(任务数组)并获取子项(数据数组)
const result = tasks.map(s => s.Data[0]);
//result contains array of Data objects
我有如下图所示的数据,我正在尝试从下图中的 json 中提取 Data
数组。
数据具有 condition_type
等键值和其他值。
我可以使用下面的代码获得每个单独的值
const submitcode = (data) => {
const tasks = Object.values(data);
console.log(tasks[0].Data[0]);
}
console.log(tasks[0].Data[0]);
任何人都可以建议是否有任何方法可以使用 React JS 从这 6 个对象中获取 Data
数组。
非常感谢
这是 returns 个 Data
数组
const array = [
{Cols: [], Data: [{condition_type: "1"}], title: "Environmental Condition"},
{Cols: [], Data: [{condition_type: "2"}], title: "Ventilation"},
{Cols: [], Data: [{condition_type: "3"}], title: "Thermal Comfort"},
{Cols: [], Data: [{condition_type: "4"}], title: "Internal Loads"},
{Cols: [], Data: [{condition_type: "5"}], title: "Exhaust"},
{Cols: [], Data: [{condition_type: "6"}], title: "Misc"},
]
console.log(array.map(({Data: [val]})=>val))
或者如果您想直接访问特定键的值
const array = [
{Cols: [], Data: [{condition_type: "1"}], title: "Environmental Condition"},
{Cols: [], Data: [{condition_type: "2"}], title: "Ventilation"},
{Cols: [], Data: [{condition_type: "3"}], title: "Thermal Comfort"},
{Cols: [], Data: [{condition_type: "4"}], title: "Internal Loads"},
{Cols: [], Data: [{condition_type: "5"}], title: "Exhaust"},
{Cols: [], Data: [{condition_type: "6"}], title: "Misc"},
]
console.log(array.map(({Data: [{condition_type}]})=>condition_type))
考虑到您在屏幕截图中的数组称为 tasks
,您可以使用具有对象和数组解构的映射:
tasks.map(({ Data: [el] }) => el);
const tasks = [{ Data: [{ a: 1, b: 2 }] }, { Data: [{ a: 3, b: 4 }] }];
console.log(tasks.map(({ Data: [el] }) => el));
或者也有点 hacky,对数组也使用对象析构两次,使用数组索引:
tasks.map(({ Data: { 0: el } }) => el);
const tasks = [{ Data: [{ a: 1, b: 2 }] }, { Data: [{ a: 3, b: 4 }] }];
console.log(tasks.map(({ Data: { 0: el } }) => el));
您可以映射父项(任务数组)并获取子项(数据数组)
const result = tasks.map(s => s.Data[0]);
//result contains array of Data objects