简体   繁体   中英

Filter data from object inside array typescript

I have an array events as follows,

[{
    "_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"
}]

How to get all the city from the above json using typescript?

i have tried this,

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

try this:

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

EDIT

i want to filter the events which has speficic places?

 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); 

EDIT 2

i have a field named "parent" which is an array of string. i already have a string named "12344" which is a part of array parent. how do i check if the events.parent cotains this string and filter those objects

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

Filter used for filter records with where conditions . In your case you want transform data and you just want a array of city from an array of object. So in this case use map instance of filter

Example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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