简体   繁体   English

如何从对象 object 和数组的字段创建一个新数组

[英]How create a new array from the fields of the objects object and the array

I have an array and an object object, I want to formulate a new array that will contain specialization from the team array and salary from the salaries object我有一个数组和一个 object object,我想制定一个新数组,其中包含团队数组的专业化和薪水 object 的薪水

const salaries = {
TeamLead: { salary: 1000, tax: "99%" },
Architect: { salary: 9000, tax: "34%" },
Design: { salary: 3000, tax: "34%" },
}
const team = [
{ name: "Alexander", specialization: "TeamLead" },
{ name: "Gaudi", specialization: "Architect" },
{ name: "Koolhas", specialization: "Architect" },
{ name: "Foster", specialization: "Architect" },
{ name: "Inna", specialization: "Design" },
{ name: "John", specialization: "Design" },
{ name: "Anna", specialization: "BackEnd" },]

  let proffesionSalaries = Object.keys(salaries);
  let teamProffesion = team.map((worker) => worker.specialization);
  let arrProfesionSallary = [];
  let arrSalaries = Object.values(salaries);
  let salariesWithTax = arrSalaries.map((sallary) => sallary.salary + (sallary.salary * (parseInt(sallary.tax)) / 100));
  for (let i = 0; i < teamProffesion.length; i++) {
    for (let j = 0; j < proffesionSalaries.length; j++) {
      if ( teamProffesion[i] == proffesionSalaries[j]) {
         name = teamProffesion[i];
         workerSalary = salariesWithTax[j];
        }}
  arrProfesionSallary.push({profession:name,sallary:workerSalary})
  };
 console.log(arrProfesionSallary);

When comparing, if the profession is not in the salary object, then the profession is renamed to the last one that is in the salary object. How to make it so that if there is no identical profession, then it is not added to the new array?.比较的时候,如果这个职业不在salary object中,那么这个职业就重命名为在salary object中的最后一个。如何做到,如果没有相同的职业,则不添加到新的大批?。

In your solution, when the if condition is never matched, name and workerSalary are not updated but still added into the array.在您的解决方案中,当if条件永远不匹配时, nameworkerSalary不会更新,但仍会添加到数组中。

I think this would work, too (build the output objects from salaries and then add them as many times as they are matched by team members):我认为这也行得通(根据salaries构建 output 对象,然后根据团队成员的匹配次数添加它们):

 const salaries = { TeamLead: { salary: 1000, tax: "99%" }, Architect: { salary: 9000, tax: "34%" }, Design: { salary: 3000, tax: "34%" }, } const team = [ { name: "Alexander", specialization: "TeamLead" }, { name: "Gaudi", specialization: "Architect" }, { name: "Koolhas", specialization: "Architect" }, { name: "Foster", specialization: "Architect" }, { name: "Inna", specialization: "Design" }, { name: "John", specialization: "Design" }, { name: "Anna", specialization: "BackEnd" }, ] const calculateTotalSalary = ({salary, tax}) => salary + (salary * parseInt(tax) / 100) const totalSalaries = Object.keys(salaries).map(profession => ({profession, salary: calculateTotalSalary(salaries[profession])})).reduce((total, salary) => total.concat(team.filter(t => salary.profession === t.specialization).map(_ => ({...salary}))), []) console.log(totalSalaries)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM