get one item from an array of name,value JSON

后端 未结 7 958
孤城傲影
孤城傲影 2020-12-13 01:33

I have this array:

var arr = [];
arr.push({name:\"k1\", value:\"abc\"});
arr.push({name:\"k2\", value:\"hi\"});
arr.push({name:\"k3\", value:\"oa\"});
         


        
7条回答
  •  無奈伤痛
    2020-12-13 02:17

    To answer your exact question you can get the exact behaviour you want by extending the Array prototype with:

    Array.prototype.get = function(name) {
        for (var i=0, len=this.length; i

    this will add the get() method to all arrays and let you do what you want, i.e:

    arr.get('k1'); //= abc
    

提交回复
热议问题