javascript 返回json对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7211535/
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-25 23:18:36  来源:igfitidea点击:

return json object

javascriptjqueryjson

提问by dardub

I have an array of objects like

我有一系列的对象,比如

 objArray =   [{"color":"red", "width":5, "price":10},
    {"color":"green", "width":4, "price":8},
    {"color":"red", "width":7, "price":11}]

how can I return an array of objects where the color is red in javascript or jquery

如何在javascript或jquery中返回颜色为红色的对象数组

采纳答案by AlienWebguy

 var objArray =   [{"color":"red", "width":5, "price":10},
    {"color":"green", "width":4, "price":8},
    {"color":"red", "width":7, "price":11}]

 var tmp = []

 $.each(objArray,function(i,val){
     if(val.color == 'red')
     {
         tmp.push(objArray[i]);
     }
 });

 console.log(tmp);

回答by user113716

If by "return", you mean return from a function, then you can use Array.prototype.filterlike this:

如果“返回”是指从函数返回,则可以Array.prototype.filter像这样使用:

return objArray.filter(function(v) { return v.color === 'red'; });

If you just wanted to save it to a variable, then it's just about the same:

如果你只是想把它保存到一个变量中,那么它几乎是一样的:

var red_array = objArray.filter(function(v) { return v.color === 'red'; });

To cover for older browsers, use the MDN .filter()shim:

要覆盖旧浏览器,请使用MDN .filter()shim

if (!Array.prototype.filter) {
    Array.prototype.filter = function (fun /*, thisp */ ) {
        "use strict";

        if (this === void 0 || this === null) throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== "function") throw new TypeError();

        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in t) {
                var val = t[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, t)) res.push(val);
            }
        }

        return res;
    };
}


Or if you want jQuery, use the $.map()[docs]method :

或者,如果您想要 jQuery,请使用$.map()[docs]方法:

var arr = $.map( objArray, function(v) { if( v.color === 'red' ) return v; });

or the $.grep()[docs]method :

$.grep()[docs]方法:

var arr = $.grep( objArray, function(v) { return v.color === 'red'; });

Examples:http://jsbin.com/udawir/edit#javascript,live(change the Array passed to $.each()to log the resulting color values)

示例:http : //jsbin.com/udawir/edit#javascript,live (更改传递给的数组$.each()以记录生成的颜色值)

回答by AmanDeepSharma

Short & Simple:

简短而简单:

let objArray =   [ {"color":"red", "width":5, "price":10},
                   {"color":"green", "width":4, "price":8},
                   {"color":"red", "width":7, "price":11} ]


let newRedArray = objArray.filter(item => item.color === "red");

Hope it helps!

希望能帮助到你!