简体   繁体   English

如何在JavaScript对象中过滤数组中的元素

[英]How to filter elements in array in JavaScript object

If I have a JavaScript object like this: 如果我有这样的JavaScript对象:

{"products":
    [
        {
            "id":"6066157707315577",
            "reference_prefix":"BB",
            "name":"BeanieBaby",
            "product_line":false,
            "has_ideas":true
        },
        {
           "id":"6066197229601550",
           "reference_prefix":"BBAGS",
           "name":"BlackBags",
            "product_line":false,
            "has_ideas":false
        }
    ],  
    "pagination": {
        "total_records":4,
        "total_pages":1,
        "current_page":1
    }
}

How do I write a function in js to loop over each pair and only return the elements of the array where has_ideas === true ? 如何在js中编写一个函数以遍历每一对,并仅返回has_ideas === true的数组元素?

I have started with this but I'm stuck. 我从这个开始,但是我被卡住了。 Clearly I am new to this. 显然,我对此并不陌生。 Any help appreciated. 任何帮助表示赞赏。

product: function(mybundle) {

    var json = JSON.parse(mybundle.response.content);
    for(var i = 0; i < json.length; i++) {
        var obj = json[i];

        if (json[i].id === "has_ideas" && json[i].value === true) {
            return json;
        }
       return [];
    } 
}

You can filter out each pair by simply checking that property: 您可以通过简单地检查该属性来过滤出每一对:

var json = {"products":[{"id":"6066157707315577","reference_prefix":"BB","name":"BeanieBaby","product_line":false,"has_ideas":true},{"id":"6066197229601550","reference_prefix":"BBAGS","name":"BlackBags","product_line":false,"has_ideas":false}],"pagination":{"total_records":4,"total_pages":1,"current_page":1}}

var stuff = json.products.filter(function(obj) {
    return obj.has_ideas === true
});

console.log(stuff);

Demo: http://jsfiddle.net/bsyk18cb/ 演示: http//jsfiddle.net/bsyk18cb/

try this 尝试这个

product: function(mybundle) {
    var json = JSON.parse(mybundle.response.content);
    for(var i = 0; i < json.length; i++) {
        if(json[i].has_ideas === true){
            return json;
        }
        return [];
    }
}

You want to check the "has_ideas" attribute and if true, return the id. 您要检查“ has_ideas”属性,如果为true,则返回ID。

product: function(mybundle) {

    var json = JSON.parse(mybundle.response.content);
    for(var i = 0; i < json.length; i++) {
         if (json[i].has_ideas === true) {
            return json[i].id;
        }
       return [];
    } 
}

Use code below. 使用下面的代码。

this will return array of elements having has_ideas=true 这将返回具有has_ideas=true的元素数组

var json = "{'products':"+
    "["+
        "{"+
            "'id':'6066157707315577',"+
            "'reference_prefix':'BB',"+
            "'name':'BeanieBaby',"+
            "'product_line':false,"+
            "'has_ideas':true"+
        "},"+
        "{"+
           "'id':'6066197229601550',"+
           "'reference_prefix':'BBAGS',"+
           "'name':'BlackBags',"+
            "'product_line':false,"+
            "'has_ideas':false"+
        "}"+
    "],"+
    "'pagination': {"+
        "'total_records':4,"+
        "'total_pages':1,"+
        "'current_page':1"+
    "}"+
"}";

function filter(){
    var jsonArr = [];
    var gList = eval( "(" + json + ")");
    alert(gList.products.length);
    for(var i=0;i<gList.products.length;i++){
        if(gList.products[i].has_ideas){
            jsonArr.push(gList.products[i]);
        }
    }
    return jsonArr;
}

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM