从数组打字稿中的对象过滤数据

时间:2017-02-07 02:28:10

标签: javascript angular typescript

我有一个如下的数组事件,

[{
    "_id": "5890b4796166c457ffdee243",
    "description": "Adele",
    "name": "Adele",
    "place": {
        "name": "Houston Toyota Center",
        "location": {
            "city": "Houston",
            "country": "United States",
            "latitude": 29.751054939716,
            "longitude": -95.362142762854,
            "state": "TX",
            "street": "1510 Polk St",
            "zip": "77002",
            "_id": "58992aebf2dbf4369c0a0325"
        },
        "id": "200866860532",
        "_id": "5890b47c6166c457ffdee394"
    },
    "start_time": "2016-11-09T20:00:00-0600",
    "id": "1644669702488073"
}, {
    "_id": "5890b4796166c457ffdee242",
    "description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.",
    "name": "Adele",
    "place": {
        "name": "Talking Stick Resort Arena",
        "location": {
            "city": "Phoenix",
            "country": "United States",
            "latitude": 33.445995372225,
            "longitude": -112.07135782626,
            "state": "AZ",
            "street": "201 E Jefferson St",
            "zip": "85004",
            "_id": "58992aebf2dbf4369c0a0327"
        },
        "id": "53475637527",
        "_id": "5890b4856166c457ffdee427"
    },
    "start_time": "2016-11-21T19:30:00-0700",
    "id": "905384112862937"
}, {
    "_id": "5890b4796166c457ffdee24a",
    "description": "Delivery of tickets will be delayed until 12/31/15",
    "name": "Adele",
    "place": {
        "name": "AmericanAirlines Arena",
        "location": {
            "city": "Miami",
            "country": "United States",
            "latitude": 25.781236943411,
            "longitude": -80.188316709574,
            "state": "FL",
            "street": "601 Biscayne Blvd",
            "zip": "33132",
            "_id": "58992aebf2dbf4369c0a0329"
        },
        "id": "120400119061",
        "_id": "5890b4946166c457ffdee464"
    },
    "start_time": "2016-10-25T19:30:00-0400",
    "id": "445046279020601"
}, {
    "_id": "5890b4796166c457ffdee244",
    "description": "Adele",
    "name": "Adele",
    "place": {
        "name": "Houston Toyota Center",
        "location": {
            "city": "Houston",
            "country": "United States",
            "latitude": 29.751054939716,
            "longitude": -95.362142762854,
            "state": "TX",
            "street": "1510 Polk St",
            "zip": "77002",
            "_id": "58992aebf2dbf4369c0a032b"
        },
        "id": "200866860532",
        "_id": "5890b47c6166c457ffdee354"
    },
    "start_time": "2016-11-08T20:00:00-0600",
    "id": "1662607760654203"
}, {
    "_id": "5890b4796166c457ffdee245",
    "description": "Delivery will be delayed until Oct 2, 2016.",
    "name": "Adele",
    "place": {
        "name": "American Airlines Center",
        "location": {
            "city": "Dallas",
            "country": "United States",
            "latitude": 32.790485550848,
            "longitude": -96.810278349053,
            "state": "TX",
            "street": "2500 Victory Ave",
            "zip": "75219",
            "_id": "58992aebf2dbf4369c0a032d"
        },
        "id": "26606856232",
        "_id": "5890b47b6166c457ffdee2e4"
    },
    "start_time": "2016-11-02T20:00:00-0500",
    "id": "649884741817020"
}]

如何使用打字稿从上面的json获取所有城市?

我试过这个,

this.eventsFiltered = this.events.filter(
          book => book.place.location.city);

2 个答案:

答案 0 :(得分:1)

试试这个:

this.eventsFiltered = this.events.map(
              book => book.place.location.city);

修改

  

我想过滤那些有特定地方的事件吗?



let events = [{
"_id": "5890b4796166c457ffdee243",
"description": "Adele",
"name": "Adele",
"place": {
    "name": "Houston Toyota Center",
    "location": {
        "city": "Houston",
        "country": "United States",
        "latitude": 29.751054939716,
        "longitude": -95.362142762854,
        "state": "TX",
        "street": "1510 Polk St",
        "zip": "77002",
        "_id": "58992aebf2dbf4369c0a0325"
    },
    "id": "200866860532",
    "_id": "5890b47c6166c457ffdee394"
},
"start_time": "2016-11-09T20:00:00-0600",
"id": "1644669702488073"
}, {
"_id": "5890b4796166c457ffdee242",
"description": "Please note that delivery will be delayed on all tickets until Friday January 8, 2016. Please adhere to the published ticket limits, which will be strictly enforced. If you exceed these limits, you may have any or all of your orders and tickets cancelled without notice. Please note: Every person, regardless of age, must have a ticket to be admitted to this event. RAIL RIDE EVENT: When you purchase a ticket to a Talking Stick Resort Arena event, you can ride the METRO LIGHT RAIL at no cost for four hours prior to the event through the end of the transit day.",
"name": "Adele",
"place": {
    "name": "Talking Stick Resort Arena",
    "location": {
        "city": "Phoenix",
        "country": "United States",
        "latitude": 33.445995372225,
        "longitude": -112.07135782626,
        "state": "AZ",
        "street": "201 E Jefferson St",
        "zip": "85004",
        "_id": "58992aebf2dbf4369c0a0327"
    },
    "id": "53475637527",
    "_id": "5890b4856166c457ffdee427"
},
"start_time": "2016-11-21T19:30:00-0700",
"id": "905384112862937"
}, {
"_id": "5890b4796166c457ffdee24a",
"description": "Delivery of tickets will be delayed until 12/31/15",
"name": "Adele",
"place": {
    "name": "AmericanAirlines Arena",
    "location": {
        "city": "Miami",
        "country": "United States",
        "latitude": 25.781236943411,
        "longitude": -80.188316709574,
        "state": "FL",
        "street": "601 Biscayne Blvd",
        "zip": "33132",
        "_id": "58992aebf2dbf4369c0a0329"
    },
    "id": "120400119061",
    "_id": "5890b4946166c457ffdee464"
},
"start_time": "2016-10-25T19:30:00-0400",
"id": "445046279020601"
}, {
"_id": "5890b4796166c457ffdee244",
"description": "Adele",
"name": "Adele",
"place": {
    "name": "Houston Toyota Center",
    "location": {
        "city": "Houston",
        "country": "United States",
        "latitude": 29.751054939716,
        "longitude": -95.362142762854,
        "state": "TX",
        "street": "1510 Polk St",
        "zip": "77002",
        "_id": "58992aebf2dbf4369c0a032b"
    },
    "id": "200866860532",
    "_id": "5890b47c6166c457ffdee354"
},
"start_time": "2016-11-08T20:00:00-0600",
"id": "1662607760654203"
}, {
"_id": "5890b4796166c457ffdee245",
"description": "Delivery will be delayed until Oct 2, 2016.",
"name": "Adele",
"place": {
    "name": "American Airlines Center",
    "location": {
        "city": "Dallas",
        "country": "United States",
        "latitude": 32.790485550848,
        "longitude": -96.810278349053,
        "state": "TX",
        "street": "2500 Victory Ave",
        "zip": "75219",
        "_id": "58992aebf2dbf4369c0a032d"
    },
    "id": "26606856232",
    "_id": "5890b47b6166c457ffdee2e4"
},
"start_time": "2016-11-02T20:00:00-0500",
"id": "649884741817020"
}];

let eventsFiltered = events.map(
          book => book.place.location.city);

console.log(eventsFiltered);
let city = 'Houston';
let eventsCt = events.filter(book => book.place.location.city == city);
console.log('2nd question');
console.log(eventsCt);




编辑2

  

我有一个名为" parent"这是一个字符串数组。我已经   有一个名为" 12344"这是数组父级的一部分。我如何能   检查events.parent是否包含此字符串并过滤这些字符串   对象

let p = '1234';
let eventsF = events.filter(book => book.parent && Array.isArray(book.parent)&& book.parent.indexOf(p) !== -1);

答案 1 :(得分:0)

用于where conditions的过滤器记录的过滤器。在你的情况下,你想要变换数据,你只需要一个对象数组的城市数组。因此,在这种情况下,请使用map

filter实例

实施例

this.cityList = this.events.map(book => book.place.location.city);