使用纯 javascript 将对象数组转换为具有 属性 类型数组的映射数组
Turn an array of objects into a map array with a property of type array using pure javascript
我需要显示一个公司列表,其中包含与每个公司相关的所有用户(关注者):
这是当前的数组数据结构:
[
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U001"
},
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U002"
},
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U003"
},
{
company:"CompanyName2",
type:"Type2",
info: {}
userId: "U001"
}
]
我需要这种格式的数据结构,使用纯 javascript(映射、缩减等)
[
{
company:"CompanyName1",
type:"Type1",
info:{},
userIds: ["U001", "U002", "U003"]
},
{
company:"CompanyName2",
type:"Type2",
info:{},
userIds: ["U001"]
}
]
这应该有效,您的结果存储在 companyList
数组中 - 如果您需要了解这里发生的事情,请告诉我。基本上它只是一个简单的 forEach
.
let origionalData = [{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U001"
},
{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U002"
},
{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U003"
},
{
"company": "CompanyName2",
"type": "Type2",
"info": "",
"userId": "U001"
}
];
然后:
let companyList = []
origionalData.forEach((data, index) => {
if (companyList[data.company]) {
companyList[data.company].userIds.push(data.userId)
} else {
companyList[data.company] = {
"type": data.type,
"info": "",
"userIds": [data.userIds]
}
}
})
console.log(companyList); // Here are the results!
您可以使用散列 table 对相同的公司进行分组。
var array = [{ company: "CompanyName1", type: "Type1", info: {}, userId: "U001" }, { company: "CompanyName1", type: "Type1", info: {}, userId: "U002" }, { company: "CompanyName1", type: "Type1", info: {}, userId: "U003" }, { company: "CompanyName2", type: "Type2", info: {}, userId: "U001" }],
hash = Object.create(null),
result = [];
array.forEach(function (o) {
if (!hash[o.company]) {
hash[o.company] = { company: o.company, type: o.type, info: o.info, userIds: [] }
result.push(hash[o.company]);
}
hash[o.company].userIds.push(o.userId);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
因此,我对您需要的数据做了一些假设。我不确定你是想按给定的方式在每个对象中使用公司字段,还是增加它,所以我按给定的方式使用它。我还返回了一个有效对象,因为您要求的结果无效。不管怎样,这是你想要的吗?
const original = [
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U001"
},
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U002"
},
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U003"
},
{
"company":"CompanyName2",
"type":"Type2",
"info": {},
"userId": "U001"
}
];
const result = original.reduce((acc, item) => {
acc.push({
[item.company]: {
type: item.type,
info: item.info,
userId: item.userId
}
});
return acc;
}, []);
我需要显示一个公司列表,其中包含与每个公司相关的所有用户(关注者): 这是当前的数组数据结构:
[
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U001"
},
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U002"
},
{
company:"CompanyName1",
type:"Type1",
info: {}
userId: "U003"
},
{
company:"CompanyName2",
type:"Type2",
info: {}
userId: "U001"
}
]
我需要这种格式的数据结构,使用纯 javascript(映射、缩减等)
[
{
company:"CompanyName1",
type:"Type1",
info:{},
userIds: ["U001", "U002", "U003"]
},
{
company:"CompanyName2",
type:"Type2",
info:{},
userIds: ["U001"]
}
]
这应该有效,您的结果存储在 companyList
数组中 - 如果您需要了解这里发生的事情,请告诉我。基本上它只是一个简单的 forEach
.
let origionalData = [{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U001"
},
{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U002"
},
{
"company": "CompanyName1",
"type": "Type1",
"info": "",
"userId": "U003"
},
{
"company": "CompanyName2",
"type": "Type2",
"info": "",
"userId": "U001"
}
];
然后:
let companyList = []
origionalData.forEach((data, index) => {
if (companyList[data.company]) {
companyList[data.company].userIds.push(data.userId)
} else {
companyList[data.company] = {
"type": data.type,
"info": "",
"userIds": [data.userIds]
}
}
})
console.log(companyList); // Here are the results!
您可以使用散列 table 对相同的公司进行分组。
var array = [{ company: "CompanyName1", type: "Type1", info: {}, userId: "U001" }, { company: "CompanyName1", type: "Type1", info: {}, userId: "U002" }, { company: "CompanyName1", type: "Type1", info: {}, userId: "U003" }, { company: "CompanyName2", type: "Type2", info: {}, userId: "U001" }],
hash = Object.create(null),
result = [];
array.forEach(function (o) {
if (!hash[o.company]) {
hash[o.company] = { company: o.company, type: o.type, info: o.info, userIds: [] }
result.push(hash[o.company]);
}
hash[o.company].userIds.push(o.userId);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
因此,我对您需要的数据做了一些假设。我不确定你是想按给定的方式在每个对象中使用公司字段,还是增加它,所以我按给定的方式使用它。我还返回了一个有效对象,因为您要求的结果无效。不管怎样,这是你想要的吗?
const original = [
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U001"
},
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U002"
},
{
"company":"CompanyName1",
"type":"Type1",
"info": {},
"userId": "U003"
},
{
"company":"CompanyName2",
"type":"Type2",
"info": {},
"userId": "U001"
}
];
const result = original.reduce((acc, item) => {
acc.push({
[item.company]: {
type: item.type,
info: item.info,
userId: item.userId
}
});
return acc;
}, []);