关于ecmascript 6:从JavaScript中删除Object中的值

Remove value from an Object in JavaScript

我有如下代码。我想把verseverses对象中删除。

1
2
3
4
5
6
7
8
_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                      
       }
    });
});


1
2
3
4
5
6
7
8
_self.verses.filter(verse => {
    Object.keys(verse).forEach(function(key) {
       if(! _self.recieved_translation.includes(key))
       {
              delete verses[verses];                  
       }
    });
});

filter跳过元素,其回调返回false。只需看看是否有问题的键,然后根据布尔值返回。

1
2
3
4
5
6
7
8
_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;
});


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

使用delete从obj中删除项目