我不确定这是否是编写此内容的最佳方法。基本上,我试图将员工分组为公司对象。在这种情况下,应该有一家公司和许多员工。下面的示例创建重复的公司。
所需的输出
var companies = [
{
companyName: "Company1",
employees: [
{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
]
},
{
companyName: "Company1",
employees: [
{
currentCompany: "Company2",
firstName: "Sam"
}
]
}
]
var users = [{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company2",
firstName: "Sam"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
];
var companies = [];
for (var i = 0; i <= users.length - 1; i++) {
if (companies.length === 0) {
var company = {
companyName: users[i].currentCompany,
employees: [users[i]]
};
console.log(i, users[i].firstName);
companies.push(company);
} else {
companies.forEach((item, index) => {
if (item.companyName === users[i].currentCompany) {
console.log(
i,
users[i].firstName,
users[i].currentCompany,
item.companyName
);
companies[index].employees.push(users[i]);
return;
} else {
var company = {
companyName: users[i].currentCompany,
employees: [users[i]]
};
console.log(
i,
users[i].firstName,
users[i].currentCompany,
item.companyName
);
companies.push(company);
return;
}
});
}
}
console.log(companies);
答案 0 :(得分:1)
使用公司名称作为对象的对象作为对象,其值是一个员工数组,而不是公司作为数组,
currentCompany
名称作为键op
对象上,使用该值,否则将其初始化为默认值,
var users = [{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company2",
firstName: "Sam"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
];
let final = users.reduce((op,inp)=>{
let {currentCompany} = inp
op[currentCompany] = op[currentCompany] || {currentCompany,employee:[]}
op[currentCompany].employee.push(inp)
return op
},{})
console.log(Object.values(final))
答案 1 :(得分:0)
我认为这是解决问题的方法。
var users = [{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company2",
firstName: "Sam"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
];
var companies = {};
users.forEach(user => {
if (!companies[user.currentCompany]) {
companies[user.currentCompany] = [];
}
});
users.forEach(element => {
companies[element.currentCompany].push(element.firstName);
});
console.log(companies);
答案 2 :(得分:0)
这将起作用
var users = [{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company2",
firstName: "Sam"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
];
let companies = [];
for(user of users) {
let flag = 0;
for(company of companies){
if(user.currentCompany == company.companyName) {
if(company.employees) {
company.employees.push(user);
}
else {
company.employees = [];
company.employees.push(user);
}
flag = 1;
break;
}
}
if(!flag) {
companies.push({
companyName : user.currentCompany,
employees : [
user
]
});
}
}
console.log(companies);
答案 3 :(得分:-1)
var users = [
{
currentCompany: "Company1",
firstName: "James"
},
{
currentCompany: "Company2",
firstName: "Sam"
},
{
currentCompany: "Company1",
firstName: "Greg"
}
];
var companies = [];
var tmpObj = {};
for (var u of users) {
if (typeof tmpObj[u.currentCompany] == 'undefined') {
tmpObj[u.currentCompany] = [];
}
tmpObj[u.currentCompany].push(u.firstName);
}
for (var c in tmpObj) {
companies.push({
"companyName": c,
"employees" : tmpObj[c]
});
}
console.log(companies);