How to find the id looping through the array of objects using typescript and react?

后端 未结 3 1199
广开言路
广开言路 2021-01-29 11:17

i have data like below,

const items = [
    {
        id: \'1\',
        color: \'green\',
        name: \'item1\',
        polygons: [
            {
                     


        
相关标签:
3条回答
  • 2021-01-29 11:27

    You can run nested loops, or you can access the subitems directly and use some or filter but if you wanted full recursion here's a fairly verbose gist for the sake of explaining

    const iterate = (obj) => {
      // loop through object properties
      Object.entries(obj).forEach(([key, value]) => {
        if (obj.hasOwnProperty(key)) {
          // do something with the current iteration
          
          if (typeof value === 'object') {
            // if the property value is an object then iterate into that object
            iterate(value)
          }
        }
      });
    };
    
    iterate(items);
    
    0 讨论(0)
  • 2021-01-29 11:35
    a.findIndex(b => b.subItem.id ===2)
    

    try the above solution. here a is the list you're using

    but I have a solution/example

    you have some structure like const a = [{id:'some1', subItem:{ id:1}},{id:'some2', subItem:{ id:2}}]

    Below will return index for subItem with ID === 2

    a.findIndex(b => b.subItem.id === 2);

    const a = [{id:'some1', subItem:{ id:1}},{id:'some2', subItem:{ id:2}}]
    
    //Below will return index for subItem with ID === 2
    a.findIndex(b => b.subItem.id === 2);
    
    to get both indexes
    
    const matchedItemIndex    = a.findIndex(b => b.subItem.id === 2);// return matched itemIndex
    const matchedItem    = a.find(b => b.subItem.id === 2);
    const matchedSubItemIndex   = a[matchedItemIndex].findIndex(b => b.id === matchedItem.id);// return mamtching subItemIndex 

    0 讨论(0)
  • 2021-01-29 11:47

    The correct search can be done like this:

    items.findIndex((item) => item.subItems.some((sub) => sub.id == '2'))
    

    Example:

    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 itemIndex = items.findIndex((item) => item.subItems.some((sub) => sub.id == '2'));
    
    console.log('Item index with subItems with id=2 is:', itemIndex)

    0 讨论(0)
提交回复
热议问题