根据子数组映射父数组

时间:2020-01-09 20:16:09

标签: javascript arrays

我有一个数组,其中有一个名称和详细信息数组,其中包含电话号码和放置对象。我试图通过遍历内部数组来分离父数组。例如我有这个数组-

import sys
theargis = ''

theargis +=str(sys.argv[1])
print ("The argument is: " + theargis)

我想将数组转换成下面的结果数组,所以我可以通过升序对电话号码进行排序,显示和列出。我现在只需要将数组转换为下面的数组结果-

    const arr = [
        {
            id: 1,
            name: 'Person 1',
            details: [
                {
                    phone: 9999999999,
                    place: 'Mumbai',
                },
                {
                    phone: 8888888888,
                    place: 'Pune'
                }
            ]
        },
        {
            id: 2,
            name: 'Person 2',
            details: [
                {
                    phone: 7777777777,
                    place: 'Mumbai',
                },
                {
                    phone: 6666666666,
                    place: 'Pune'
                }
            ]
        }
    ]

请帮助。

5 个答案:

答案 0 :(得分:4)

您可以减少初始数组以构造一个新数组。对于每次迭代,您都可以将一个人的details属性映射到一个新数组,该数组将包含phoneplace以及id和{{1} }的那个人。

name
const result = data.reduce((res, {id, name, details}) => {
  return res.concat(details.map(detail => ({
    id, 
    name, 
    details: [detail]
  })));
}, []);

console.log(result)

上面,我没有循环遍历<script>const data=[{id:1,name:"Person 1",details:[{phone:9999999999,place:"Mumbai"},{phone:8888888888,place:"Pune"}]},{id:2,name:"Person 2",details:[{phone:7777777777,place:"Mumbai"},{phone:6666666666,place:"Pune"}]}];</script>并追加到details,而是使用了一种快捷方式,直接返回与地图结果串联的res

此外,借助传播算子,您可以执行以下操作:

res
const result = data.reduce((res, {details, ...person}) => {
  return res.concat(details.map(detail => ({
    details: [detail],
    ...person,
  })));
}, []);

console.log(result)

答案 1 :(得分:0)

const result = []
for(let i = 0; i < arr.length; i++){
    for(let j = 0; j < arr[i].details.length; j++){
        result.push({
            ...arr[i],
            details: [arr[i].details[j]]
        })
    }
}

答案 2 :(得分:0)

使用for循环

const arr = [
    {
        id: 1,
        name: 'Person 1',
        details: [
            {
                phone: 9999999999,
                place: 'Mumbai',
            },
            {
                phone: 8888888888,
                place: 'Pune'
            }
        ]
    },
    {
        id: 2,
        name: 'Person 2',
        details: [
            {
                phone: 7777777777,
                place: 'Mumbai',
            },
            {
                phone: 6666666666,
                place: 'Pune'
            }
        ]
    }
];
let result = [];
for(let i=0;i < arr.length;i++){
  for(let j=0; j < arr[i].details.length;j++){ 
    result.push({
      id: arr[i].id,
      name: arr[i].name,
      details: arr[i].details[j]
    });
  }
}
console.log(result);

答案 3 :(得分:0)

简单的forEach应该做。

const arr = [
  {
    id: 1,
    name: "Person 1",
    details: [
      {
        phone: 9999999999,
        place: "Mumbai"
      },
      {
        phone: 8888888888,
        place: "Pune"
      }
    ]
  },
  {
    id: 2,
    name: "Person 2",
    details: [
      {
        phone: 7777777777,
        place: "Mumbai"
      },
      {
        phone: 6666666666,
        place: "Pune"
      }
    ]
  }
];

const result = [];
arr.forEach(row => {
  row.details.forEach(detail => {
    result.push({
      "id": row.id,
      "name": row.name,
      "details": [detail]
    });
  })
});

console.log(JSON.stringify(result, null, 2));

答案 4 :(得分:0)

这是我使用reduce的简单方法:

const result = arr.reduce((acc, entry) => {
        const obj = []

        entry.details.forEach(detail => {
          obj.push({
            ...entry,
            details: [
              detail
            ]
          })
        });

        return [...acc, ...obj];
    }, []);