Filter array of objects by object key

后端 未结 3 433
甜味超标
甜味超标 2021-01-07 05:46

I have an array of objects in Javascript:

var List = [
            {
                employee:\'Joe\',
                type:\'holiday\',
            },
              


        
3条回答
  •  没有蜡笔的小新
    2021-01-07 06:06

    var joe = List.filter(function(el){
     return el.employee === "Joe"
    });
    
    var jerry = List.filter(function(el){
     return el.employee === "Jerry"
    });
    

    This uses Array.prototype.filter and will work in IE9 and up + all recent Chrome/Firefox/Safari/Opera releases.

    If you don't know the names in advance then you can create a map var names = {};

    for(var i =0; i

    }

    As a side note, Javascript convention is to only capitalize the first letter of a variable for constructors. So List should probably be list.

提交回复
热议问题