试图映射对象内部的对象
Trying to map objects inside an object
我是 webdev 的初学者,我在使用 reactjs 时遇到了一些问题。我有一系列关于大学的数据,例如:
const data = [
{
name: "UFSM",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{name: "engineering", value: 10}, {name:"biology", value: 20}, ...]
},
{...
disciplines: [{name: "engineering", value: 7}, ....]
},...]
我正在使用 map 函数将他们所有的名字映射到一个数组中,例如:
let data_name = props.data.map((data) => data.name);
是否可以使用map将不同大学的同名学科的值分组到一个数组中?输出如下:
data_engineering = ["10", "7", ...]
这样我就可以将来自不同大学的所有工程相关学科放在同一个数组中。
如果你想要像engineering
这样的单一学科的数组,你可以做下面的映射:
data_engineering =
props.data.map((uni) => uni.disciplines.engineering);
对于未定义工程值的大学,此数组将具有 undefined
值。但这可能就是您想要的,因为这意味着索引与 data_name
.
的索引对齐
如果你想为所有学科建立这样的阵列,你可以这样做:
const dataDiscipline = {};
// 1. Collect all disciplines from all universities
// by making them a key of dataDiscipline.
props.data.forEach((uni) =>
uni.disciplines.forEach((discipline) =>
dataDiscipline[discipline] = null
)
);
// 2. Now build the discipline array for each discipline.
Object.keys(dataDiscipline).forEach((discipline) =>
dataDiscipline[discipline] =
props.data.map((uni) => uni.disciplines[discipline])
);
// You can now use e.g. dataDiscipline.engineering
const data = [{
name: "UFSM",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 10
}, {
name: "biology",
value: 20
}]
}, {
name: "ABCD",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 4
}, {
name: "biology",
value: 5
}]
}, {
name: "EFGH",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 5
}, {
name: "biology",
value: 9
}]
}]
let result = {};
data.map((data) => data.disciplines).forEach((item, index) => {
item.forEach((item, index) => {
if (!result[item.name]) {
result[item.name] = []
}
result[item.name].push(item.value);
});
});
console.log(JSON.stringify(result));
我是 webdev 的初学者,我在使用 reactjs 时遇到了一些问题。我有一系列关于大学的数据,例如:
const data = [
{
name: "UFSM",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{name: "engineering", value: 10}, {name:"biology", value: 20}, ...]
},
{...
disciplines: [{name: "engineering", value: 7}, ....]
},...]
我正在使用 map 函数将他们所有的名字映射到一个数组中,例如:
let data_name = props.data.map((data) => data.name);
是否可以使用map将不同大学的同名学科的值分组到一个数组中?输出如下:
data_engineering = ["10", "7", ...]
这样我就可以将来自不同大学的所有工程相关学科放在同一个数组中。
如果你想要像engineering
这样的单一学科的数组,你可以做下面的映射:
data_engineering =
props.data.map((uni) => uni.disciplines.engineering);
对于未定义工程值的大学,此数组将具有 undefined
值。但这可能就是您想要的,因为这意味着索引与 data_name
.
如果你想为所有学科建立这样的阵列,你可以这样做:
const dataDiscipline = {};
// 1. Collect all disciplines from all universities
// by making them a key of dataDiscipline.
props.data.forEach((uni) =>
uni.disciplines.forEach((discipline) =>
dataDiscipline[discipline] = null
)
);
// 2. Now build the discipline array for each discipline.
Object.keys(dataDiscipline).forEach((discipline) =>
dataDiscipline[discipline] =
props.data.map((uni) => uni.disciplines[discipline])
);
// You can now use e.g. dataDiscipline.engineering
const data = [{
name: "UFSM",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 10
}, {
name: "biology",
value: 20
}]
}, {
name: "ABCD",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 4
}, {
name: "biology",
value: 5
}]
}, {
name: "EFGH",
total: 17,
filled: 12,
approved: 10,
reproved: 1,
dropouts: 1,
requests: 170,
approved_requests: 120,
disciplines: [{
name: "engineering",
value: 5
}, {
name: "biology",
value: 9
}]
}]
let result = {};
data.map((data) => data.disciplines).forEach((item, index) => {
item.forEach((item, index) => {
if (!result[item.name]) {
result[item.name] = []
}
result[item.name].push(item.value);
});
});
console.log(JSON.stringify(result));