微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据对象键过滤 JavaScript 对象数组

如何解决根据对象键过滤 JavaScript 对象数组

我有一个从 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",{200 + objects just like the first without the message: "message text"},...
]

解决方法

您可以查看它们并删除这些键:

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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。