我在下面有这些对象,我希望得到所有" 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"
}
答案 0 :(得分:0)
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'));