如何使用类型脚本循环遍历对象数组并创建仅具有某些属性的新对象数组并做出反应?

时间:2020-08-18 18:04:56

标签: javascript reactjs typescript

我的数据如下,

const items = [
    {
       id: '1',
       color: 'green',
       name: 'item1',
       polygons: [
           {
               id: '1', 
               coordinates: [
                   {
                       latitude: '25.00',
                       longitude: '-25.99',
                   }
                   {
                       latitude: '15.00',
                       longitude: '-25.99',
                   }
                   {
                       latitude: '25.00',
                       longitude: '-35.99',
                   }
              ],
          }
      ]
      subItems: [
          {
              id: '1', 
              name: 'subitem-1',
              color: 'green',
              polygons: [
                  {
                      id: '2', 
                      coordinates: [
                          {
                              latitude: '25.00',
                              longitude: '-25.99',
                          }
                          {
                              latitude: '15.00',
                              longitude: '-25.99',
                          }
                          {
                              latitude: '25.00',
                              longitude: '-35.99',
                          }
                      ],
                  }
              ]
          }
      ],
  },
  {
      id: '2',
      color: 'red',
      name: 'item2',
      polygons: [
          {
              id: '3', 
              coordinates: [
                  {
                      latitude: '25.00',
                      longitude: '-25.99',
                  }
                  {
                      latitude: '15.00',
                      longitude: '-25.99',
                  }
                  {
                      latitude: '25.00',
                      longitude: '-35.99',
                  }
              ],
          }
      ]
      subItems: [
          {
              id: '2', 
              name: 'subitem-1',
              color: 'red',
              polygons: [
                 {
                     id: '5', 
                     coordinates: [
                         {
                             latitude: '25.00',
                             longitude: '-25.99',
                         } 
                         {
                             latitude: '15.00',
                             longitude: '-25.99',
                         } 
                         {
                             latitude: '25.00',
                             longitude: '-35.99',
                         }
                     ],
                 }
             ]
         }
     ],
 }

]

现在我想将每个项目和子项目中的所有多边形放入对象数组中,以便预期的输出如下所示,

const polygons = [
{
     id: '1',
     color: 'green', //this comes from item.color
     coordinates: [
         {
             latitude: '25.00',
             longitude: '-25.99',
         }
         {
             latitude: '15.00',
             longitude: '-25.99',
         }
         {
             latitude: '25.00',
             longitude: '-35.99',
         }
     ],
 },
 {
     id: '2',
     color: 'green', //this comes from subItems color
     coordinates: [
         {
             latitude: '25.00',
             longitude: '-25.99',
         }
         {
             latitude: '15.00',
             longitude: '-25.99',
         }
         {
             latitude: '25.00',
             longitude: '-35.99',
         }
     ],
 },
 {
     id: '3',
     color: 'red', //this comes from Item color
     coordinates: [
         {
             latitude: '25.00',
             longitude: '-25.99',
         }
         {
             latitude: '15.00',
             longitude: '-25.99',
         }
         {
             latitude: '25.00',
             longitude: '-35.99',
         }
     ],
 },
 {
     id: '4',
     color: 'red', //this comes from subItems color
     coordinates: [
         {
             latitude: '25.00',
             longitude: '-25.99',
         }
         {
             latitude: '15.00',
             longitude: '-25.99',
         }
         {
             latitude: '25.00',
             longitude: '-35.99',
         }
     ],
 },

]

现在是一个问题,我如何使用reduce方法使用JavaScript或打字稿从Items数组中获取如上所述的数组多边形。谢谢。

编辑:

我尝试做类似的事情,但这只能从Item对象中检索多边形,但是我也希望subItems多边形。

export interface Polygon {
    id: string;
    coordinate: Coordinate[];
    item: Item;
}

export interface DrawablePolygon extends Polygon {
    color: string;
}

const polygons = React.useMemo(() => {
    return Items.reduce((acc: DrawablePolygon[], Item) => {
       (Item.polygons || []).forEach(polygon => {
           acc.push({ ...polygon, color: Item.color });
       });
       return acc;
   }, []);
}

我该如何解决。谢谢。

2 个答案:

答案 0 :(得分:1)

请在下面尝试

const items = [
  {
     id: '1',
     color: 'green',
     name: 'item1',
     polygons: [
         {
             id: '1', 
             coordinates: [
                 {
                     latitude: '25.00',
                     longitude: '-25.99',
                 },
                 {
                     latitude: '15.00',
                     longitude: '-25.99',
                 },
                 {
                     latitude: '25.00',
                     longitude: '-35.99',
                 }
            ],
        }
    ],
    subItems: [
        {
            id: '1', 
            name: 'subitem-1',
            color: 'green',
            polygons: [
                {
                    id: '2', 
                    coordinates: [
                        {
                            latitude: '25.00',
                            longitude: '-25.99',
                        },
                        {
                            latitude: '15.00',
                            longitude: '-25.99',
                        },
                        {
                            latitude: '25.00',
                            longitude: '-35.99',
                        }
                    ],
                }
            ]
        }
    ],
},
{
    id: '2',
    color: 'red',
    name: 'item2',
    polygons: [
        {
            id: '3', 
            coordinates: [
                {
                    latitude: '25.00',
                    longitude: '-25.99',
                },
                {
                    latitude: '15.00',
                    longitude: '-25.99',
                },
                {
                    latitude: '25.00',
                    longitude: '-35.99',
                }
            ],
        }
    ],
    subItems: [
        {
            id: '2', 
            name: 'subitem-1',
            color: 'red',
            polygons: [
               {
                   id: '5', 
                   coordinates: [
                       {
                           latitude: '25.00',
                           longitude: '-25.99',
                       } ,
                       {
                           latitude: '15.00',
                           longitude: '-25.99',
                       } ,
                       {
                           latitude: '25.00',
                           longitude: '-35.99',
                       }
                   ],
               }
           ]
       }
   ],
}]

let result = [];
items.forEach((item) => {
  item.polygons.forEach((polygon) => {
    polygon.color = item.color;
  });
  result = result.concat(item.polygons);
  item.subItems.forEach((subItem) => {
    subItem.polygons.forEach((subItemPolygon) => {
      subItemPolygon.color = subItem.color
    });
    result = result.concat(subItem.polygons);
  });
});

console.log(result);

答案 1 :(得分:0)

这是我的solution

interface NestedWithPolygons{
    polygons: [Polygon],
    subItems: [NestedWithPolygons],
    color: string
};
type Polygon = any
const getPolygons = (x: NestedWithPolygons) => x.polygons.map((p,i) => ({...p, color:x.color})).concat((x.subItems || []).flatMap(getPolygons));

console.log(items.flatMap(getPolygons));