简体   繁体   English

将 JavaScript 中的嵌套 arrays 转换为包含父 object 成员的平面对象

[英]Converting nested arrays in JavaScript into flat objects containing members of the parent object

I have an array of objects with nested subarray (empoloyees), and the names of the attributes on the parent level can be voluntary (for example, company):我有一个带有嵌套子数组(员工)的对象数组,父级别的属性名称可以是自愿的(例如,公司):

values = [{
  company: 'DotCom',
  employees: [{
    name: 'John Doe',
    position: 'Salesrep'
  }, {
    name: 'Mike Pen',
    position: 'Driver'
  }]
}, {
  company: 'Green Tree',
  employees: [{
    name: 'Suzanne Fox',
    position: 'Secretary'
  }, {
    name: 'Kelly Fun',
    position: 'CEO Assistant'
  }, {
    name: 'Brian Bush',
    position: 'CEO'
  }]
}]

I need to transform it in a way that the resulting objects will not contain nested arrays:我需要以生成的对象不包含嵌套 arrays 的方式对其进行转换:

[{
    company: "DotCom",
    name: "John Doe",
    position: "Salesrep"
  },
  {
    company: "DotCom",
    name: "Mike Pen",
    position: "Driver"
  },
  {
    company: "Green Tree",
    name: "Suzanne Fox",
    position: "Secretary"
  },
  {
    company: "Green Tree",
    name: "Kelly Fun",
    position: "CEO Assistant"
  },
  {
    company: "Green Tree",
    name: "Brian Bush",
    position: "CEO"
  }
]

This is my code:这是我的代码:

 values = [{company: 'DotCom', employees: [{name: 'John Doe', position: 'Salesrep'}, {name: 'Mike Pen', position: 'Driver'}]},{company: 'Green Tree', employees: [{name: 'Suzanne Fox', position: 'Secretary'}, {name: 'Kelly Fun', position: 'CEO Assistant'}, {name: 'Brian Bush', position: 'CEO'}]}] const getEntries = (o) => Object.entries(o).flatMap(([k, v]) => Object(v) === v? getEntries(v, k): [ [k, v] ] ) var result = [] values.map(item => [item].flatMap(x => result.push(Object.fromEntries(getEntries(x))))) console.log(result)

And the result is (console.log(result)) giving me only the one (last) member of the nested array of employees, and not all of the employees:结果是 (console.log(result)) 只给我嵌套员工数组的一个(最后一个)成员,而不是所有员工:

[{
    company: "DotCom",
    name: "Mike Pen",
    position: "Driver"
  },
  {
    company: "Green Tree",
    name: "Brian Bush",
    position: "CEO"
  }
]

What would be the best approach to modify the code so that I would get all of the employees into the result?修改代码的最佳方法是什么,以便我将所有员工都纳入结果?

Here is another example of the values array:这是值数组的另一个示例:

values = [{
  city: 'New York', 
  division: 'Main Office', 
  employees: [{
    name: 'John Doe', 
    position: 'Salesrep'
  }, {
    name: 'Mike Pen', 
    position: 'Driver'
  }]
},{
  city: 'Tampa', 
  division: 'Regional Office', 
  employees: [{
    name: 'Suzanne Fox', 
    position: 'Secretary'
  }, {
    name: 'Kelly Fun', 
    position: 'CEO Assistant'
  }, {
    name: 'Brian Bush', 
    position: 'CEO'
  }]
}]

The expected result:预期结果:

[{
  city: 'New York', 
  division: 'Main Office', 
  name: 'John Doe', 
  position: 'Salesrep'
 }, 
 {...}]

Please check if this would work for you.请检查这是否适合您。 I am spreading the employee object and flattening the resultant array:我正在传播员工 object 并展平结果数组:

 let values = [{ company: 'DotCom', employees: [{ name: 'John Doe', position: 'Salesrep' }, { name: 'Mike Pen', position: 'Driver' }] }, { company: 'Green Tree', employees: [{ name: 'Suzanne Fox', position: 'Secretary' }, { name: 'Kelly Fun', position: 'CEO Assistant' }, { name: 'Brian Bush', position: 'CEO' }] }]; let newValues = values.map(c => c.employees.map(e => { return { company: c.company, ...e } })).flatMap(x => x); console.log(newValues);

Do NOT use map or flatMap if you do not use the resulting array.如果不使用结果数组,请不要使用 map 或 flatMap。

Here I use flatMap and spread to return a flattened map这里我使用 flatMap 和 spread 返回一个扁平化的 map

Without names - assuming only ONE array and no other nesting没有名字——假设只有一个数组,没有其他嵌套

 const values = [{company: 'DotCom', employees: [{name: 'John Doe', position: 'Salesrep'}, {name: 'Mike Pen', position: 'Driver'}]},{company: 'Green Tree', employees: [{name: 'Suzanne Fox', position: 'Secretary'}, {name: 'Kelly Fun', position: 'CEO Assistant'}, {name: 'Brian Bush', position: 'CEO'}]}] const companyArr = values.flatMap(item => { const obj = {}; let arr = []; Object.entries(item).forEach(([key,val]) => { if (Array.isArray(val)) arr = val; else obj[key] = val; }) return arr.map(item => ({...obj,...item})) }); console.log(companyArr)

Using the names使用名称

 const values = [{company: 'DotCom', employees: [{name: 'John Doe', position: 'Salesrep'}, {name: 'Mike Pen', position: 'Driver'}]},{company: 'Green Tree', employees: [{name: 'Suzanne Fox', position: 'Secretary'}, {name: 'Kelly Fun', position: 'CEO Assistant'}, {name: 'Brian Bush', position: 'CEO'}]}] const empArr = values.flatMap(entry => entry.employees.map(emp => ({"company": entry.company, ...emp})) ); console.log(empArr)

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

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