javascript 属性值的json索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8585085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:54:06  来源:igfitidea点击:

json index of property value

javascript

提问by mcgrailm

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

我需要在一个数组中获取 json 对象的索引,其由对象 id

here is the example code

这是示例代码

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

then pseudo code

然后是伪代码

 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 ?

显然这行不通,但我想知道是否有办法在不循环查找索引的情况下执行此操作?

回答by

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

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

回答by German Attanasio

You can try Array.prototype.map, based on your example it will be:

您可以尝试Array.prototype.map,根据您的示例,它将是:

var index = list.map(function(e) { return e._id; }).indexOf('4dd822c5e8a6c42aa70000ad');

Array.prototype.mapis not available on IE7 or IE8. ES5 Compatibility

Array.prototype.map在 IE7 或 IE8 上不可用。ES5 兼容性

回答by jfriend00

Since there is no index of your data structure by _id, something is going to have to loop over the structure and find an _idvalue that matches.

由于没有数据结构的索引 by _id,因此必须遍历结构并找到_id匹配的值。

function findId(data, id) {
    for (var i = 0; i < data.length; i++) {
        if (data[i]._id == id) {
            return(data[i]);
        }
    }
}

回答by Peter Olson

Just loop through the list until you find the matching id.

只需遍历列表,直到找到匹配的 id。

function indexOf(list, id) {
  for (var i = 0; i < list.length; i++) {
    if (list[i]._id === id) { return i; }
  }
  return -1;
}

回答by rlemon

function index_of(haystack, needle) {
    for (var i = 0, l = haystack.length; i < l; ++i) {
        if( haystack[i]._id === needle ) {
           return i;   
        }
    }
    return -1;
}

console.log(index_of(list, '4dd80b16e8a6c428a900007d'));

same as the above, but caches the length of the haystack.

与上面相同,但缓存了干草堆的长度。

回答by Patrik Wallin

a prototypical way

一种典型的方式

(function(){
  if (!Array.prototype.indexOfPropertyValue){
    Array.prototype.indexOfPropertyValue = function(prop,value){
      for (var index = 0; index < this.length; index++){
        if (prop in this[index]){
          if (this[index][prop] == value){
            return index;
          }
        }
      }
      return -1;
    }
  }
 })();
 // usage:

 list.indexOfPropertyValue('_id','4dd80b16e8a6c428a900007d'); // returns 1 (index of array);
 list.indexOfPropertyValue('_id','Invalid id') // returns -1 (no result);
 var indexOfArray = list.indexOfPropertyValue('_id','4dd80b16e8a6c428a900007d');
 list[indexOfArray] // returns desired object.