JavaScript查找对象数组中某个元素的下标

转 https://blog.csdn.net/weixin_42504805/article/details/126137779 (原文博主)
var array = [1,2,3,'4'];
 
var indexOf4 = (array || []).findIndex((item) => item === '4');
 
console.log(indexOf4); // 3
var profiles= [
    {
      id: 'id123',
      name: "lin",
      age: 23
    },
    {
      id: 'id456',
      name: "lin2",
      age: 12
    },
    {
      id: 'id678',
      name: "lin3",
      age: 13
    }
];
 
var currentProfile = {
      id: 'id456',
      name: "lin2",
      age: 12
}; 
 
var currentProfileIndex = (profiles|| []).findIndex((profile) => profile.id === currentProfile .id);
console.log(currentProfileIndex );

  

posted @ 2022-09-26 13:56  隰荷华  阅读(1015)  评论(0编辑  收藏  举报