search

Home  >  Q&A  >  body text

javascript - Traverse the object and output the current key-value pair

var obj={ 'sprite-icon.png': 'sprite-icon.png?v=62df6cbea3',
  'sprite-icon_ie9.png': 'sprite-icon_ie9.png?v=5f35b8f025' };
  
  for(name in obj){
    console.log(name);
}

 遍历对象,并输出 当前是第几个键值对。以及这个对象总共有多少个键值对。
大家讲道理大家讲道理2751 days ago659

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-06-26 11:00:41

    var obj = {
      a: 1,
      b: 2,
      c: 3
    };
    
    var keys = Object.keys(obj);
    console.log('长度', keys.length);
    keys.forEach(function(key, index) {
      console.log('当前是第', index + 1, '个, key是', key, ', value是', obj[key]);
    });

    reply
    0
  • 三叔

    三叔2017-06-26 11:00:41

    Object.keys(obj).forEach((item,index)=>{console.log(item,obj[item],index)})
    Object.keys(obj).length

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 11:00:41

    Correct answer upstairs, please add words for like

    reply
    0
  • Cancelreply