Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用typescript找到在对象数组中循环的id并作出反应?_Javascript_Reactjs_Typescript - Fatal编程技术网

Javascript 如何使用typescript找到在对象数组中循环的id并作出反应?

Javascript 如何使用typescript找到在对象数组中循环的id并作出反应?,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我有如下数据 const items = [ { id: '1', color: 'green', name: 'item1', polygons: [ { id: '1', coordinates: [ { latitude: '25.00',

我有如下数据

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 siIndex = Items.forEach(
    (item: any) =>
        item.subItems ?
            item.subItems.findIndex((subItem:any) => subItem.id === '2');//error here
    );
现在,我想从上面的Items数组中找到id为“2”的子项的索引

我试过下面的方法

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 siIndex = Items.forEach(
    (item: any) =>
        item.subItems ?
            item.subItems.findIndex((subItem:any) => subItem.id === '2');//error here
    );
但这给了我解析错误“:”的预期值

如何从Items数组中找到id为“2”的子项的索引。有人能帮我吗

我不知道如何循环遍历Items数组并找到id为“2”的子项的索引。 谢谢

尝试上述解决方案。这是您正在使用的列表

但我有一个解决方案/例子

你有一些结构,比如 常量a=[{id:'some1',子项:{id:1},{id:'some2',子项:{id:2}]

下面将返回ID为==2的子项的索引

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

const a=[{id:'some1',子项:{id:1}},{id:'some2',子项:{id:2}]
//下面将返回ID为==2的子项的索引
a、 findIndex(b=>b.subItem.id==2);
获取两个索引
const matchedItemIndex=a.findIndex(b=>b.subItem.id==2);//返回匹配项索引
const matchedItem=a.find(b=>b.subItem.id==2);

常量matchedSubItemIndex=a[matchedItemIndex].findIndex(b=>b.id==matchedItem.id);//返回子项索引
正确的搜索方法如下:

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

const项=[{
id:'1',
颜色:“绿色”,
名称:'item1',
多边形:[{
id:'1',
坐标:[{
纬度:'25.00',
经度:'-25.99',
},
{
纬度:'15.00',
经度:'-25.99',
},
{
纬度:'25.00',
经度:'-35.99',
}
],
}],
分项:[{
id:'1',
名称:“子项-1”,
颜色:“绿色”,
多边形:[{
id:'2',
坐标:[{
纬度:'25.00',
经度:'-25.99',
},
{
纬度:'15.00',
经度:'-25.99',
},
{
纬度:'25.00',
经度:'-35.99',
}
],
}]
}],
},
{
id:'2',
颜色:“红色”,
名称:'item2',
多边形:[{
id:'3',
坐标:[{
纬度:'25.00',
经度:'-25.99',
},
{
纬度:'15.00',
经度:'-25.99',
},
{
纬度:'25.00',
经度:'-35.99',
}
],
}],
分项:[{
id:'2',
名称:“子项-1”,
颜色:“红色”,
多边形:[{
id:'5',
坐标:[{
纬度:'25.00',
经度:'-25.99',
},
{
纬度:'15.00',
经度:'-25.99',
},
{
纬度:'25.00',
经度:'-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)
您可以运行嵌套循环,也可以直接访问子项并使用一些或过滤器,但如果您想要完全递归,为了解释,这里有一个相当详细的要点

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

您没有正确使用三元运算符。请咨询。这就是出现解析错误的原因。您需要
find
方法。看一看,谢谢。但是您提供了我的代码片段作为您的答案。@saritha有多个使用上述artur的示例。请回顾这一点,并尝试解决问题。我没有提供您问题的答案。只需将您指向修复解析错误所需的资源,然后自己进行筛选。如果我想回答,我会发布一个答案。你想从子项数组或项数组中找到匹配的索引吗?我想同时签入子项和项数组。然后你需要编写一个函数并保留所有匹配的索引