Javascript 将公共对象追加到一个数组中并删除重复项
我试图从Javascript数组中删除重复的对象 我有一个这样的物体Javascript 将公共对象追加到一个数组中并删除重复项,javascript,lodash,Javascript,Lodash,我试图从Javascript数组中删除重复的对象 我有一个这样的物体 var actualObj = [ { "name": "Sam", "id": "1", "dept": "Inventory", "info": { "key1": "test1", "key2": "test2", "key3": "test3" } }, { "name": "Paul", "id": "2",
var actualObj = [
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Paul",
"id": "2",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key4": "test4",
"key5": "test5",
"key6": "test6"
}
}
]
var expectedObj = [
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": [
{
"key1": "test1",
"key2": "test2",
"key3": "test3"
},
{
"key4": "test4",
"key5": "test5",
"key6": "test6"
}
]
},
{
"name": "Paul",
"id": "2",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
}
]
我试图删除重复项并将“信息”合并到对象数组中,类似这样
var actualObj = [
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Paul",
"id": "2",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
},
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": {
"key4": "test4",
"key5": "test5",
"key6": "test6"
}
}
]
var expectedObj = [
{
"name": "Sam",
"id": "1",
"dept": "Inventory",
"info": [
{
"key1": "test1",
"key2": "test2",
"key3": "test3"
},
{
"key4": "test4",
"key5": "test5",
"key6": "test6"
}
]
},
{
"name": "Paul",
"id": "2",
"dept": "Inventory",
"info": {
"key1": "test1",
"key2": "test2",
"key3": "test3"
}
}
]
对于“info”对象中的相同值,我尝试使用Lodash,效果很好
有谁能帮助我从实际的目标中实现预期的目标。我正在尝试通过组合为一个具有相似id值的对象来创建所需对象。您可以尝试此方法,希望它对您有用
for(let i = 0; i < actualObj.length; i++) {
let o = actualObj[i];
for(let j = i+1; j < actualObj.length; j++) {
let b = actualObj[j];
// dublicate object identified by id
if (o.id === b.id) {
const info = [];
info.push(o.info);
info.push(b.info);
o.info = info;
actualObj.splice(j, 1);
}
}
}
你可以试试这个,希望它对你有用
for(let i = 0; i < actualObj.length; i++) {
let o = actualObj[i];
for(let j = i+1; j < actualObj.length; j++) {
let b = actualObj[j];
// dublicate object identified by id
if (o.id === b.id) {
const info = [];
info.push(o.info);
info.push(b.info);
o.info = info;
actualObj.splice(j, 1);
}
}
}
使用lodash,通过id
将项目,然后将组转换为请求的格式,使用获取不带信息的基本对象,并使用\uuu.map()
获取信息的数组。使用以下命令组合到单个对象:
var actualObj=[{“name”:“Sam”,“id”:“1”,“dept”:“Inventory”,“info”:{“key1”:“test1”,“key2”:“test2”,“key3”:“test3”},{“name”:“Paul”,“id”:“2”,“dept”:“Inventory”,“info”:{“key1”:“test1”,“key2”:“test2”,“key3”:“test3”},{“name”:“Sam;
变量结果=uj(实际值)
.groupBy('id'))
.map(功能(组){
返回u.assign(u.omit(组[0],'info'){
信息:u.map(组“信息”)
});
})
.value();
控制台日志(结果);
使用lodash,通过id
将项目,然后将组转换为请求的格式,使用以获取不带信息的基本对象,.map()
以获取信息的数组。使用以下命令组合到单个对象:
var actualObj=[{“name”:“Sam”,“id”:“1”,“dept”:“Inventory”,“info”:{“key1”:“test1”,“key2”:“test2”,“key3”:“test3”},{“name”:“Paul”,“id”:“2”,“dept”:“Inventory”,“info”:{“key1”:“test1”,“key2”:“test2”,“key3”:“test3”},{“name”:“Sam;
变量结果=uj(实际值)
.groupBy('id'))
.map(功能(组){
返回u.assign(u.omit(组[0],'info'){
信息:u.map(组“信息”)
});
})
.value();
控制台日志(结果);
的可能重复