簡體   English   中英

從數組打字稿中的對象過濾數據

[英]Filter data from object inside array 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);

嘗試這個:

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

我有一個名為“父”的字段,它是一個字符串數組。 我已經有一個名為“ 12344”的字符串,它是數組父級的一部分。 我如何檢查events.parent是否包含此字符串並過濾這些對象

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

篩選器用於篩選where conditionswhere conditions記錄。 在您的情況下,您需要轉換數據,而您只需要一個對象數組中的一個城市數組。 因此,在這種情況下,請使用filter map實例

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM