属性值的json索引 [英] json index of property value

查看:52
本文介绍了属性值的json索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取数组中json对象的索引,该数组的对象ID是

I need to get the index of the json object in an array whose by the objects id

这是示例代码

var list = [ { _id: '4dd822c5e8a6c42aa70000ad',
    metadata: 
     { album: 'American IV: Man Comes Around',
       song: 'Hurt',
       coverart: 'http://images.mndigital.com/albums/044/585/435/m.jpeg',
       artist: 'Johnny Cash',
       length: 216,
       mnid: '44585439' } },
  { _id: '4dd80b16e8a6c428a900007d',
    metadata: 
     { album: 'OK Computer (Collector\'s Edition)',
       song: 'Paranoid Android',
       coverart: 'http://images.mndigital.com/albums/026/832/735/m.jpeg',
       artist: 'Radiohead',
       length: 383,
       mnid: '26832739' } },
  { _id: '4dd68694e8a6c42c80000479',
    metadata: 
     { album: 'The Presidents Of The United States Of America: Ten Year Super Bonus Special Anniversary Edition',
       song: 'Lump',
       coverart: 'http://images.mndigital.com/albums/011/698/433/m.jpeg',
       artist: 'The Presidents Of The United States Of America',
       length: 134,
       mnid: '11698479' } }
]

然后是伪代码

 var index_of  = list.indexOf("4dd80b16e8a6c428a900007d");

显然这是行不通的,但是我想知道是否有某种方法可以在不循环查找索引的情况下进行?

obviously that is not gonna work but I wonder if there is anyway to do this without looping to find the index ?

推荐答案

var i = list.length;
while( i-- ) {
    if( list[i]._id === '4dd80b16e8a6c428a900007d' ) break;
}

alert(i);  // will be -1 if no match was found

这篇关于属性值的json索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆