What's the best way to query an array in javascript to get just the items from it I want?

后端 未结 7 1167
悲哀的现实
悲哀的现实 2020-12-13 18:48

I have an array like this (with just over 3000 objects instead of the 3 here):

items = [{name:\'charlie\', age:\'16\'}, {name:\'ben\', age:\'18\'}, {name:\'s         


        
7条回答
  •  盖世英雄少女心
    2020-12-13 18:58

    making use of javascript magnificent function eval() which evaluates string as code at runtime, we can define a prototype method for Array type

    Array.prototype.where = function (query) {
    var newArray = [];
    for(var i=0; i

    and use it with any array, passing the query as string

    var newArray= items.where('.age >= 18');
    

提交回复
热议问题