如何在对象内搜索?

时间:2018-06-14 09:30:48

标签: javascript object

我在下面有这些对象,我希望得到所有" event_properties"仅包含" event_name:"已下订单"

的数据

如何在javascript中创建一个循环呢?

先谢谢。

{
    "count": 1,
    "object": "$list",
    "data": [
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Opened Email",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "Peterson",
            },
        },
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Placed Order",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "Davis",
            },
        },      
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Placed Order",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "St. Patrick",
            },
        }       

    ],
    "next": "7e60cb00-6fb0-11e8-8001-ee622c54cc84"
}

enter image description here

3 个答案:

答案 0 :(得分:0)

使用Array.filter

let obj = {"count":1,"object":"$list","data":[{"event_properties":{"Email Domain":"gmail.com","Client Name":"Gmail image proxy"},"uuid":"87519c80-6fb0-11e8-8001-24fdba31a7b0","event_name":"Opened Email","person":{"updated":"2018-06-14 08:54:52","$last_name":"Peterson"}},{"event_properties":{"Email Domain":"gmail.com","Client Name":"Gmail image proxy"},"uuid":"87519c80-6fb0-11e8-8001-24fdba31a7b0","event_name":"Placed Order","person":{"updated":"2018-06-14 08:54:52","$last_name":"Davis"}},{"event_properties":{"Email Domain":"gmail.com","Client Name":"Gmail image proxy"},"uuid":"87519c80-6fb0-11e8-8001-24fdba31a7b0","event_name":"Placed Order","person":{"updated":"2018-06-14 08:54:52","$last_name":"St. Patrick"}}],"next":"7e60cb00-6fb0-11e8-8001-ee622c54cc84"};

let result = obj.data.filter(({event_name}) => event_name === "Placed Order");
console.log(result);

答案 1 :(得分:0)

首先,使用filter过滤您的数组,然后使用map仅返回event_properties



const object = {
    "count": 1,
    "object": "$list",
    "data": [
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Opened Email",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "Peterson",
            },
        },
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Placed Order",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "Davis",
            },
        },      
        {
            "event_properties": {
                "Email Domain": "gmail.com",
                "Client Name": "Gmail image proxy",
            },
            "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0",
            "event_name": "Placed Order",
            "person": {
                "updated": "2018-06-14 08:54:52",
                "$last_name": "St. Patrick",
            },
        }       

    ],
    "next": "7e60cb00-6fb0-11e8-8001-ee622c54cc84"
}

const result = object.data.filter(item => item.event_name === 'Placed Order').map(item => item.event_properties);
console.log(result);




答案 2 :(得分:0)

var a={ "count": 1, "object": "$list", "data": [ { "event_properties": { "Email Domain": "gmail.com", "Client Name": "Gmail image proxy", }, "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0", "event_name": "Opened Email", "person": { "updated": "2018-06-14 08:54:52", "$last_name": "Peterson", }, }, { "event_properties": { "Email Domain": "gmail.com", "Client Name": "Gmail image proxy", }, "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0", "event_name": "Placed Order", "person": { "updated": "2018-06-14 08:54:52", "$last_name": "Davis", }, }, { "event_properties": { "Email Domain": "gmail.com", "Client Name": "Gmail image proxy", }, "uuid": "87519c80-6fb0-11e8-8001-24fdba31a7b0", "event_name": "Placed Order", "person": { "updated": "2018-06-14 08:54:52", "$last_name": "St. Patrick", }, } ], "next": "7e60cb00-6fb0-11e8-8001-ee622c54cc84" }
console.log(a.data.filter(value=>value.event_name=='Placed Order'));