我有一个像这样的对象数组。如果年份和DName是smae,我想合并这个。这是我的数组
var film = [{
Year:2017
DName:"Rohit"
Released:"Golmal"
id:0
},
{
Year:2017
DName:"Rohit"
Upcoming:"Singham"
id:1
},
{
Year:2016
DName:"Anil"
Released:"xyz"
id:2
},
{
Year:2017
DName:"David"
Released:"Judva"
id:3
},
{
Year:2018
DName:"Karan"
InProgress:"Brahmastra"
id:4
},
{
Year:2017
DName:"David"
InProgress:"Something"
id:5
}]
现在在这个json我希望如果year and DName
相同,那么对象喊出合并。输出应该像
[{
Year:2017
DName:"Rohit"
Released:"Golmal",
Upcoming:"Singham"
id:0
},
{
Year:2016
DName:"Anil"
Released:"xyz"
id:2
},
{
Year:2017
DName:"David"
Released:"Judva",
InProgress:"Something"
id:3
},
{
Year:2018
DName:"Karan"
InProgress:"Brahmastra"
id:4
}]
我正在尝试合并对象,但我在循环中无法完成。
有没有人有这个或任何内置方法的脚本来实现这个?
我在这里尝试的是
ww function(destination,source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
现在在这种情况下,我可以传递两个对象,但对于一个数组,我不知道如何传递。
答案 0 :(得分:2)
您可以使用哈希表来保持对具有相同Year
和DName
的对象的引用。然后更新所有属性并返回一个新数组。
var film = [{ Year: 2017, DName: "Rohit", Released: "Golmal", id: 0 }, { Year: 2017, DName: "Rohit", Upcoming: "Singham", id: 1 }, { Year: 2016, DName: "Anil", Released: "xyz", id: 2 }, { Year: 2017, DName: "David", Released: "Judva", id: 3 }, { Year: 2018, DName: "Karan", InProgress: "Brahmastra", id: 4 }, { Year: 2017, DName: "David", InProgress: "Something", id: 5 }],
hash = {},
result = [];
film.forEach(function (o) {
function update(source, target) {
Object.keys(source).forEach(function (k) { target[k] = source[k]; });
}
var key = ['Year', 'DName'].map(function (k) { return o[k] }).join('|');
if (!(key in hash)) {
hash[key] = {};
result.push(hash[key]);
}
update(o, hash[key]);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ES6与Object.assign
。
var film = [{ Year: 2017, DName: "Rohit", Released: "Golmal", id: 0 }, { Year: 2017, DName: "Rohit", Upcoming: "Singham", id: 1 }, { Year: 2016, DName: "Anil", Released: "xyz", id: 2 }, { Year: 2017, DName: "David", Released: "Judva", id: 3 }, { Year: 2018, DName: "Karan", InProgress: "Brahmastra", id: 4 }, { Year: 2017, DName: "David", InProgress: "Something", id: 5 }],
hash = new Map,
result = [];
film.forEach(function (o) {
var key = ['Year', 'DName'].map(k => o[k]).join('|');
if (!hash.has(key)) {
hash.set(key, {});
result.push(hash.get(key));
}
Object.assign(hash.get(key), o);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
您可以使用 array.reduce , array.find 和 Object.assign :
var film = [{
Year:2017,
DName:"Rohit",
Released:"Golmal",
id:0
},
{
Year:2017,
DName:"Rohit",
Upcoming:"Singham",
id:1
},
{
Year:2016,
DName:"Anil",
Released:"xyz",
id:2
},
{
Year:2017,
DName:"David",
Released:"Judva",
id:3
},
{
Year:2018,
DName:"Karan",
InProgress:"Brahmastra",
id:4
},
{
Year:2017,
DName:"David",
InProgress:"Something",
id:5
}];
var film = film.reduce((m, o) => {
var found = m.find(p => p.Year === o.Year && p.DName === o.DName);
if (found) {
var foundId = found.id; // Save id, it will be overwritten by Object.assign
Object.assign(found, o);
found.id = foundId; // restore id
} else {
m.push(o);
}
return m;
}, []);
console.log(film);