简体   繁体   English

如何将嵌套对象数组动态转换为单个数组

[英]How to convert nested object array to single array dynamically

// I have this kind of multiple object // 我有这种多对象

_id: "5e5d00337c5e6a0444d00304"
    orderID: 10355
    orderDate: "2020-03-02"
    user:
       _id: "5e2e9699a648c53154f41025"
       name: "xyz1"
       email: "abcde@gmail.com"
       mNumber: 12336

// and I want this in a single array // 我想把它放在一个数组中

orderID: 10354, 10355
orderDate:"2020-03-02","2020-03-02"
name: "xyz", "xyz1"
email: "abcd@gmail.com", "abcde@gmail.com"
mNumber: 1234536, 12336

My main objective is to get the excel sheet where under these headers id name email and so on i get those data.我的主要目标是获取 Excel 表,在这些标题下,ID 名称电子邮件等我得到这些数据。 like喜欢

orderID    orderDate    name   email           mNumber
10354      2020-03-02   xyz   abcd@gmail.com   1234536
10355      2020-03-02   xyz1   abcde@gmail.com   12336

How should this be done in Angular?这应该如何在 Angular 中完成?

If I understand you correctly, you can do something like this,如果我理解正确,你可以做这样的事情,

objs = [
  {
    _id: "5e5d00337c5e6a0444d00304",
    orderID: 10354,
    orderDate: "2020-03-02",
    user:
    {
      _id: "5e2e9699a648c53154f41025",
      name: "xyz",
      email: "abcd@gmail.com",
      mNumber: 1234536
    }
  },
  {
    _id: "5e5d00337c5e6a0444d00304",
    orderID: 10355,
    orderDate: "2020-03-02",
    user:
    {
      _id: "5e2e9699a648c53154f41025",
      name: "xyz1",
      email: "abcde@gmail.com",
      mNumber: 12336
    }
  }
];
const arr = [];
for (const o of objs) {
  arr.push([
    o.orderID,
    o.orderDate,
    o.user.name,
    o.user.email,
    o.user.mNumber
  ]);
}
console.log(arr);

This is the output,这是输出,

[ [ 10354, '2020-03-02', 'xyz', 'abcd@gmail.com', 1234536 ],
  [ 10355, '2020-03-02', 'xyz1', 'abcde@gmail.com', 12336 ] ]

Basically it is just an iteration.基本上它只是一个迭代。

I am going to add another solution.我将添加另一个解决方案。 In this case, you don't want to describe all the values that goes in the row, but instead you want to describe the values you don't want in the row.在这种情况下,您不想描述行中的所有值,而是想描述行中不需要的值。 You can use a recursive function to traverse the objects in this situation.在这种情况下,您可以使用递归函数来遍历对象。 fields is the object where you describe wich values goes and witch don't. fields是您描述的值的对象,而女巫则没有。 Something like this.像这样的东西。

const fields = { _id: false, user: { _id: false} }
function toRowRec(obj, fields) {
  let row = [];
  const keys = Object.keys(obj);
  for (const k of keys) {
    if (!fields.hasOwnProperty(k)) {
      row.push(obj[k]);
    } else if (typeof obj[k] === 'object' && obj[k] !== null) {
      row = row.concat(toRowRec(obj[k], fields[k])); 
    }
  }
  return row;
}
let arr = []
for (const o of objs) {
  arr.push(toRowRec(o, fields));
}
console.log(arr);

I omit the input objs (same as previous), and you will get the exact output than before.我省略了输入objs (与之前相同),您将获得比以前更准确的输出。

You will see that I am also omitting the keys that I want to put in the row, to do this I am just checking if the key is in the fields object.你会看到我也省略了我想放在行中的键,为此我只是检查键是否在fields对象中。

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

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