简体   繁体   English

根据对象键值从对象数组中删除对象

[英]Remove object from array of objects based on object key value

I have an object like 我有一个像

let arr = [
    {isManaged: true, id:1},
    {isManaged: false, id:2},
    {isManaged:false, id:3}
]

to get the values which are true, i do 为了获得真实的价值,我愿意

arr.map(shift => ({
    id: shift.id,
    isPartnerManaged: shift.isManaged,
}))

but this will only return me the values where i true, now, I want to remove them from the array of objects. 但这只会返回我为true的值,现在,我想从对象数组中删除它们。 I tried to use the array.pop but i don't know what index to feed it. 我试图使用array.pop但我不知道该用什么索引。 Any ideeas? 有什么想法吗?

arr = arr.filter(shift => shift.isManaged);

You could filter the array and build new objects. 您可以过滤数组并构建新对象。

 var array = [{ isManaged: true, id: 1 }, { isManaged: false, id: 2 }, { isManaged: false, id: 3 }], result = array .filter(({ isManaged }) => isManaged) .map(({ isManaged: isPartnerManaged, id }) => ({ id, isPartnerManaged })); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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