从JavaScript中的对象中删除值

时间:2018-06-28 12:12:00

标签: javascript ecmascript-6

我有如下代码。我想从verse对象中删除verses

_self.verses.filter(verse => {
    Object.keys(verse).forEach(function(key) {
       if(! _self.recieved_translation.includes(key))
       {
         // I would like to remove this verse from verses                       
       }
    });
});

4 个答案:

答案 0 :(得分:2)

_self.verses.filter(verse => {
    Object.keys(verse).forEach(function(key) {
       if(! _self.recieved_translation.includes(key))
       {
              delete verses[verses];                  
       }
    });
});

答案 1 :(得分:0)

使用delete从obj中删除项目

答案 2 :(得分:0)

_self.verses.forEach(verse => delete verse.verse);

答案 3 :(得分:0)

filter会跳过其回调返回false的元素。只要看看您是否有问题的密钥,然后根据布尔值返回即可。

_self.verses = _self.verses.filter(verse => {
    // We don't need foreach here, we just need the first key
    var keyWithoutTranslation = Object.keys(verse).find(function(key) {
       return !_self.recieved_translation.includes(key);
    });
    // Now the filter will do the rest
    return keyWithoutTranslation === undefined;
});