我有一个JavaScript对象
我正在尝试按id对这些对象进行分组,并生成一个名称列表的映射,其中键是id,值是名称。例如:
{1 : [John, Jane, Mike], 3: [Bob, Will]}
使用下面的代码,我得到:
{1: [John, Jane, Mike, Bob, Will], 3: [John, Jane, Mike, Bob, Will]}
如何实现所需的输出?任何帮助将不胜感激。
var myObj = [{
id: 1,
name: "John",
major: "accounting"
},
{
id: 1,
name: "Jane",
major: "accounting"
}, {
id: 1,
name: "Mike",
major: "accounting"
}, {
id: 3,
name: "Bob",
major: "history"
}, {
id: 3,
name: "Will",
major: "history"
}]
for (i in myObj) {
var obj = myObj[i];
var id = obj.id;
var name = obj.name;
var newMap = {};
var namesArray = [];
if (!(id in newMap)) {
namesArray.push(name);
newMap[id] = namesArray;
} else {
for (key in newMap) {
if (key == id) {
namesArray.push(name);
newMap[key] = namesArray;
}
}
}
console.log(newMap);
}
答案 0 :(得分:1)
编辑自己的程序:
var myObj = [{
id: 1,
name: "John",
major: "accounting"
},
{
id: 1,
name: "Jane",
major: "accounting"
}, {
id: 1,
name: "Mike",
major: "accounting"
}, {
id: 3,
name: "Bob",
major: "history"
}, {
id: 3,
name: "Will",
major: "history"
}]
var newMap = {};
var namesArray = [];
for (i in myObj) {
var obj = myObj[i];
var id = obj.id;
var name = obj.name;
if (!(id in newMap)) {
namesArray = [];
namesArray.push(name);
newMap[id] = namesArray;
} else {
namesArray = newMap[id];
namesArray.push(name);
}
}
console.log(newMap);
答案 1 :(得分:0)
var myObj = [{id: 1, name: "John", major:"accounting"},
{id: 1, name: "Jane", major:"accounting"},
{id: 1, name: "Mike", major:"accounting"},
{id: 3, name: "Bob", major:"history" },
{id: 3, name: "Will", major:"history" }];
var result = myObj.reduce(function(acc, item) {
acc[item.id] = acc[item.id] || [];
acc[item.id].push(item.name);
return acc;
}, {});
console.log(result);

答案 2 :(得分:0)
var myObj = [{id: 1, name: "John", major:"accounting"},
{id: 1, name: "Jane", major:"accounting"},
{id: 1, name: "Mike", major:"accounting"},
{id: 3, name: "Bob", major:"history"},
{id: 3, name: "Will", major:"history"}];
var group = {};
for(var i = 0; i < myObj.length; i++){
if(myObj[i].id in group){
group[myObj[i].id].push(myObj[i].name);
} else {
group[myObj[i].id] = [];
group[myObj[i].id].push(myObj[i].name);
}
}
console.log(group);
答案 3 :(得分:0)
使用map
函数的另一种解决方案。
var objs = [{id: 1, name: "John", major:"accounting"},
{id: 1, name: "Jane", major:"accounting"},
{id: 1, name: "Mike", major:"accounting"},
{id: 3, name: "Bob", major:"history"},
{id: 3, name: "Will", major:"history"}];
var namesForId = [], currentId = null, theObj = {};
objs.map((item) => {
if (item.id === currentId) {
namesForId.push(item.name);
} else {
currentId = item.id;
namesForId = [item.name];
theObj[item.id] = namesForId;
}
})
console.log(theObj);
答案 4 :(得分:0)
另一种使用Array#reduce
但更紧凑的方法。
const input = [{ id: 1, name: "John", major: "accounting" }, { id: 1, name: "Jane", major: "accounting" }, { id: 1, name: "Mike", major: "accounting" }, { id: 3, name: "Bob", major: "history" }, { id: 3, name: "Will", major: "history" } ];
console.log(input.reduce((m, e) => ((m[e.id] = m[e.id] || []).push(e.name), m), {}))
&#13;