在 javascript 中的对象数组中合并具有相同键的对象
Merge objects with same key in array of objects in javascript
我有一个对象数组,如图所示below.If该数组包含具有相同键值的对象,那么生成的数组应该包含 ES5 版本中两个对象的组合
var arr = [
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
}
]
},
{
"def": [
{
"first_name": "test1",
"last_name": "test1"
}
]
}
]
预期输出应该是
var arr =[
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
},
{
"first_name": "test1",
"last_name": "test1"
}
]
}
]
谁能帮我实现这个?提前致谢
var arr = [
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
}
]
},
{
"def": [
{
"first_name": "test1",
"last_name": "test1"
}
]
}]
const result = arr.reduce((acc, curr) => {
const key = Object.keys(curr)[0]
const found = acc.find(i => i[key])
if (!found) {
acc.push(curr)
} else {
found[key] = [ ...found[key], ...curr[key] ]
}
return acc;
}, [])
console.log(result)
下面的代码应该会给出想要的结果。
ES6
arr.reduce((acc, curr) => {
const key = Object.keys(curr)[0]
const found = acc.find(i => i[key])
if (!found) {
acc.push(curr)
} else {
found[key] = [ ...found[key], ...curr[key] ]
}
return acc;
}, [])
ES5
arr.reduce(function (acc, curr) {
var key = Object.keys(curr)[0];
var found = acc.find(function (i) {
return i[key];
});
if (!found) {
acc.push(curr);
} else {
found[key] = [].concat(found[key], curr[key]);
}
return acc;
}, []);
使用 filter
而不是 find
arr.reduce(function (acc, curr) {
var key = Object.keys(curr)[0];
var found = acc.filter(function (i) {
return i[key];
})[0];
if (!found) {
acc.push(curr);
} else {
found[key] = [].concat(found[key], curr[key]);
}
return acc;
}, []);
使用 object
和 array
var arr = [{
"abc": [{
"name": "test",
"addr": "123",
}]
},
{
"def": [{
"first_name": "test",
"last_name": "test"
}]
},
{
"def": [{
"first_name": "test1",
"last_name": "test1"
}]
}
]
var b = {}
arr.forEach(c => {
var key = Object.keys(c)[0]
var value = Object.values(c)[0]
if (!b[key]) b[key] = []
b[key].push(...value)
})
// b is resultant object
var c = []
Object.keys(b).forEach(v => {
let a = {};
a[v] = b[v];
c.push(a)
})
// c is converting object to array
console.log(c)
我有一个对象数组,如图所示below.If该数组包含具有相同键值的对象,那么生成的数组应该包含 ES5 版本中两个对象的组合
var arr = [
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
}
]
},
{
"def": [
{
"first_name": "test1",
"last_name": "test1"
}
]
}
]
预期输出应该是
var arr =[
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
},
{
"first_name": "test1",
"last_name": "test1"
}
]
}
]
谁能帮我实现这个?提前致谢
var arr = [
{
"abc": [
{
"name": "test",
"addr": "123",
}
]
},
{
"def": [
{
"first_name": "test",
"last_name": "test"
}
]
},
{
"def": [
{
"first_name": "test1",
"last_name": "test1"
}
]
}]
const result = arr.reduce((acc, curr) => {
const key = Object.keys(curr)[0]
const found = acc.find(i => i[key])
if (!found) {
acc.push(curr)
} else {
found[key] = [ ...found[key], ...curr[key] ]
}
return acc;
}, [])
console.log(result)
下面的代码应该会给出想要的结果。
ES6
arr.reduce((acc, curr) => {
const key = Object.keys(curr)[0]
const found = acc.find(i => i[key])
if (!found) {
acc.push(curr)
} else {
found[key] = [ ...found[key], ...curr[key] ]
}
return acc;
}, [])
ES5
arr.reduce(function (acc, curr) {
var key = Object.keys(curr)[0];
var found = acc.find(function (i) {
return i[key];
});
if (!found) {
acc.push(curr);
} else {
found[key] = [].concat(found[key], curr[key]);
}
return acc;
}, []);
使用 filter
而不是 find
arr.reduce(function (acc, curr) {
var key = Object.keys(curr)[0];
var found = acc.filter(function (i) {
return i[key];
})[0];
if (!found) {
acc.push(curr);
} else {
found[key] = [].concat(found[key], curr[key]);
}
return acc;
}, []);
使用 object
和 array
var arr = [{
"abc": [{
"name": "test",
"addr": "123",
}]
},
{
"def": [{
"first_name": "test",
"last_name": "test"
}]
},
{
"def": [{
"first_name": "test1",
"last_name": "test1"
}]
}
]
var b = {}
arr.forEach(c => {
var key = Object.keys(c)[0]
var value = Object.values(c)[0]
if (!b[key]) b[key] = []
b[key].push(...value)
})
// b is resultant object
var c = []
Object.keys(b).forEach(v => {
let a = {};
a[v] = b[v];
c.push(a)
})
// c is converting object to array
console.log(c)