对象的反应、排序和数组(reduce 和 map?)
React, ordering and array of objects (reduce and map?)
问题如下:(https://codesandbox.io/s/8p21n6p09l)
我有一组对象(称为模块),如下所示:
const modules = [
{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb" ]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
},
并且此数组从 App.js 传递到 thmaticArea.jsx 组件。问题是我改变了显示数据的想法并希望按 thematicArea 对它们进行分组,所以它看起来像:
Topic 1:
- Name 1
- Name 2
- Name 6
- ...
Topic 2:
- Name 3
- Name 5
Topic 3:
- Name 4
and so on.
每个名称都是唯一的,只能有一个thematicArea(主题),但一个thematicArea(主题)可以有多个名称(名称1,名称2...)。
我认为我应该先使用 reduce() 方法,然后再使用 .map() 方法,但我已经尝试了多种方法,但它对我不起作用。 Reduce() 方法正在创建一个没有长度的数组...这就是我尝试在 App.js 中使用 reduce 实现它并将其作为 'modules':[=14 传递给 thematicArea.jsx 组件的方式=]
modules={this.state.modules.reduce(function(groups, item) {
const val = item["thematicArea"];
groups[val] = groups[val] || [];
groups[val].push(item);
return groups;
}, [])}
达到预期结果的最佳方法是什么?
我有一个例子,最后是一个对象或数组。
这是你想要的吗?
const modules = [{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb"
]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
},
];
// If you want the values in an object
const retObject = modules.reduce((tmp, x) => {
if (!tmp[x.thematicArea]) {
tmp[x.thematicArea] = [x.name];
} else {
tmp[x.thematicArea].push(x.name);
}
return tmp;
}, {});
console.log('Value as object -----');
console.log(retObject);
// If you want the values in array
const retArray = Object.values(retObject);
console.log('Value as array -----');
console.log(retArray);
如果这不是理论编程问题,我认为最好的方法是使用来自 lodash 或类似程序的 _.groupBy。有时不需要重新发明轮子。
你可以这样使用,最终输出应该是object
而不是array
。
const modules = [{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb"
]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
}
];
let result = modules.reduce((obj, val) => {
obj[val.thematicArea] = obj[val.thematicArea] || [];
if (obj[val.thematicArea].indexOf(val.name) == -1)
obj[val.thematicArea].push(val.name);
return obj;
}, {});
console.log(result);
问题如下:(https://codesandbox.io/s/8p21n6p09l)
我有一组对象(称为模块),如下所示:
const modules = [
{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb" ]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
},
并且此数组从 App.js 传递到 thmaticArea.jsx 组件。问题是我改变了显示数据的想法并希望按 thematicArea 对它们进行分组,所以它看起来像:
Topic 1:
- Name 1
- Name 2
- Name 6
- ...
Topic 2:
- Name 3
- Name 5
Topic 3:
- Name 4
and so on.
每个名称都是唯一的,只能有一个thematicArea(主题),但一个thematicArea(主题)可以有多个名称(名称1,名称2...)。
我认为我应该先使用 reduce() 方法,然后再使用 .map() 方法,但我已经尝试了多种方法,但它对我不起作用。 Reduce() 方法正在创建一个没有长度的数组...这就是我尝试在 App.js 中使用 reduce 实现它并将其作为 'modules':[=14 传递给 thematicArea.jsx 组件的方式=]
modules={this.state.modules.reduce(function(groups, item) {
const val = item["thematicArea"];
groups[val] = groups[val] || [];
groups[val].push(item);
return groups;
}, [])}
达到预期结果的最佳方法是什么?
我有一个例子,最后是一个对象或数组。
这是你想要的吗?
const modules = [{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb"
]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
},
];
// If you want the values in an object
const retObject = modules.reduce((tmp, x) => {
if (!tmp[x.thematicArea]) {
tmp[x.thematicArea] = [x.name];
} else {
tmp[x.thematicArea].push(x.name);
}
return tmp;
}, {});
console.log('Value as object -----');
console.log(retObject);
// If you want the values in array
const retArray = Object.values(retObject);
console.log('Value as array -----');
console.log(retArray);
如果这不是理论编程问题,我认为最好的方法是使用来自 lodash 或类似程序的 _.groupBy。有时不需要重新发明轮子。
你可以这样使用,最终输出应该是object
而不是array
。
const modules = [{
thematicArea: "Topic 1",
id: 1,
name: "Budowanie postawy asertywnej",
details: [
"Czym jest asertywność?",
"Asertywny Asertywny menedżer – charakterystyczne zachowania"
]
},
{
thematicArea: "Topic 2",
id: 2,
name: "Asertywne narzędzia",
details: [
"Asertywna odmowa – zastosowanie trzyetapowej procedury",
"Technika „Zdartej płyty”",
"Wyrażanie pochwał i próśb"
]
},
{
thematicArea: "Topic 3",
id: 3,
name: "Lorem ipsum 1",
details: ["Coś się wymyśli", "Żeby nie było tak łyso"]
},
{
thematicArea: "Topic 3",
id: 4,
name: "Lorem ipsum 2",
details: [
"Wyczesane szkolenia",
"Naprawdępowinieneś wrzucić to do koszyka",
"Na co czekasz - dodaj!"
]
},
{
thematicArea: "Topic 3",
id: 5,
name: "Ipsum Lorem",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 6,
name: "Ipsum Lorem 1",
details: ["Och", "Ach"]
},
{
thematicArea: "Topic 3",
id: 7,
name: "Ipsum Lorem 2",
details: ["Och", "Ach"]
}
];
let result = modules.reduce((obj, val) => {
obj[val.thematicArea] = obj[val.thematicArea] || [];
if (obj[val.thematicArea].indexOf(val.name) == -1)
obj[val.thematicArea].push(val.name);
return obj;
}, {});
console.log(result);