简体   繁体   English

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

[英]How to loop through array of objects and create a new array of objects with only certain properties using typescript and react?

i have data like below,我有如下数据,

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',
                         }
                     ],
                 }
             ]
         }
     ],
 }

] ]

Now i want to get all the polygons from each item and subitem into an array of objects so the expected ouptut is like below,现在我想从每个项目和子项目中获取所有多边形到一个对象数组中,所以预期的输出如下所示,

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',
         }
     ],
 },

] ]

Now the question how can i use reduce method to get an array polygons like above from Items array using javascript or typescript. thanks.现在的问题是如何使用 reduce 方法使用 javascript 或 typescript 从 Items 数组中获取上面的数组多边形。谢谢。

EDIT:编辑:

i have tried doing something like this but this retrieves only the polygons from Item object but i also want the subItems polygons too.我试过做这样的事情,但这只检索项目 object 中的多边形,但我也想要子项目多边形。

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;
   }, []);
}

how can i fix this.我怎样才能解决这个问题。 thanks.谢谢。

Please try below请尝试以下

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);

Here's my 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));

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

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