合并两个数据源以创建一个对象数组

时间:2019-09-24 04:59:32

标签: javascript

我不确定这是否是编写此内容的最佳方法。基本上,我试图将员工分组为公司对象。在这种情况下,应该有一家公司和许多员工。下面的示例创建重复的公司。

所需的输出

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);

4 个答案:

答案 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);