簡體   English   中英

我有一個對象數組,在所有對象中我都刪除了一些數據

[英]I have an Array of objects, in all objects I have delete some data

我有一個對象數組,在所有對象中我都有 event_date_utc: 2008-09-28T23:15:00Z 這里 event_date_utc 是一個鍵,值是 2008-09-28T23:15:00Z。 現在我只想顯示像 2008-09-28 這樣的有價值的日期,所以我如何在 javascript 中實現這一點

這是我的代碼

const data = [
    {
        "title": "Falcon 1 Makes History",
        "event_date_utc": "2008-09-28T23:15:00Z",
        "flight_number": 4,
        "details": "Falcon 1 becomes the first privately developed liquid fuel rocket to reach Earth orbit.",
    },
    {
        "title": "SpaceX Wins $1.6B CRS Contract",
        "event_date_utc": "2008-12-23T01:00:00Z",
        "flight_number": null,
        "details": "NASA awards SpaceX $1.6B Commercial Resupply Services (CRS) contract.",
    },
    {
        "title": "Falcon 1 Flight 5 Makes History",
        "event_date_utc": "2009-07-13T03:35:00Z",
        "flight_number": 5,
        "details": "Falcon 1 Flight 5 makes history, becoming the first privately developed liquid fuel rocket to deliver a commercial satellite to Earth orbit."
    },
    {
        "title": "Falcon 9 First Flight",
        "event_date_utc": "2010-06-04T18:45:00Z",
        "flight_number": 6,
        "details": "Met 100% of mission objectives on the first flight!"
    },
    {
        "title": "Dragon Returns From Earth Orbit",
        "event_date_utc": "2010-12-08T15:43:00Z",
        "flight_number": 7,
        "details": "On December 8, 2010, Dragon became the first privately developed spacecraft in history to re-enter from low-Earth orbit."
    },
    {
        "title": "First Dragon Visit to Space Station",
        "event_date_utc": "2012-10-08T00:35:00Z",
        "flight_number": 9,
        "details": "Dragon becomes the first private spacecraft in history to visit the space station."
    },
    
]

我猜您只想修改日期,然后使用包含已消毒日期的新數組。 為此,您可以像這樣就地進行。

 var data = [{ "title": "Falcon 1 Makes History", "event_date_utc": "2008-09-28T23:15:00Z", "flight_number": 4, "details": "Falcon 1 becomes the first privately developed liquid fuel rocket to reach Earth orbit.", }, { "title": "SpaceX Wins $1.6B CRS Contract", "event_date_utc": "2008-12-23T01:00:00Z", "flight_number": null, "details": "NASA awards SpaceX $1.6B Commercial Resupply Services (CRS) contract.", }, { "title": "Falcon 1 Flight 5 Makes History", "event_date_utc": "2009-07-13T03:35:00Z", "flight_number": 5, "details": "Falcon 1 Flight 5 makes history, becoming the first privately developed liquid fuel rocket to deliver a commercial satellite to Earth orbit." }, { "title": "Falcon 9 First Flight", "event_date_utc": "2010-06-04T18:45:00Z", "flight_number": 6, "details": "Met 100% of mission objectives on the first flight!" }, { "title": "Dragon Returns From Earth Orbit", "event_date_utc": "2010-12-08T15:43:00Z", "flight_number": 7, "details": "On December 8, 2010, Dragon became the first privately developed spacecraft in history to re-enter from low-Earth orbit." }, { "title": "First Dragon Visit to Space Station", "event_date_utc": "2012-10-08T00:35:00Z", "flight_number": 9, "details": "Dragon becomes the first private spacecraft in history to visit the space station." }, ]; data.forEach((item) => item.event_date_utc = item.event_date_utc.substr(0, 10)); console.log(JSON.stringify(data));

你應該使用filter

const filteredData = data.filter(
  (d) => d.event_date_utc.split("T")[0] === "2008-09-28"
)

console.log(filteredData)

完整示例

 const data = [ { title: "Falcon 1 Makes History", event_date_utc: "2008-09-28T23:15:00Z", flight_number: 4, details: "Falcon 1 becomes the first privately developed liquid fuel rocket to reach Earth orbit.", }, { title: "SpaceX Wins $1.6B CRS Contract", event_date_utc: "2008-12-23T01:00:00Z", flight_number: null, details: "NASA awards SpaceX $1.6B Commercial Resupply Services (CRS) contract.", }, { title: "Falcon 1 Flight 5 Makes History", event_date_utc: "2009-07-13T03:35:00Z", flight_number: 5, details: "Falcon 1 Flight 5 makes history, becoming the first privately developed liquid fuel rocket to deliver a commercial satellite to Earth orbit.", }, { title: "Falcon 9 First Flight", event_date_utc: "2010-06-04T18:45:00Z", flight_number: 6, details: "Met 100% of mission objectives on the first flight!", }, { title: "Dragon Returns From Earth Orbit", event_date_utc: "2010-12-08T15:43:00Z", flight_number: 7, details: "On December 8, 2010, Dragon became the first privately developed spacecraft in history to re-enter from low-Earth orbit.", }, { title: "First Dragon Visit to Space Station", event_date_utc: "2012-10-08T00:35:00Z", flight_number: 9, details: "Dragon becomes the first private spacecraft in history to visit the space station.", }, ] const filteredData = data.filter( (d) => d.event_date_utc.split("T")[0] === "2008-09-28" ) console.log(filteredData)

暫無
暫無

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

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