簡體   English   中英

根據對象鍵過濾對象數組 JavaScript

[英]Filter JavaScript Array of Objects based on Objects Key

我有一個從 JSON 響應生成的對象數組。

所述數組的內容結構如下:

const array = [
{title: "user generated title", message: "random user generated text", status: "Active/Inactive"},
 {200 + more objects with the exact same key/values as the first},
...,
...
]

我想用完全相同的對象減去特定的鍵/值對來創建一個新數組。

即,制作完全相同的數組而不說所有消息:“用戶消息”鍵/值對。 (雖然我想從數組中的對象中刪除多個鍵/值對。)

所以就像

const array = [
{title: "user generated title", status: "Active/Inactive"},
{200 + objects just like the first without the message: "message text"},
...,
...
]

您可以通過 go 刪除這些密鑰:

array.forEach(o => delete o.message); 
var newArray = [];
for (var i = 0; i < array.length; i++) {
    var obj = array[i];
    
    if (!("message" in obj)) { //put the conditions to keep the object here
        newArray.push(obj); //copy reference to new array
    }
}

暫無
暫無

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

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