我有如下代码。我想从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
}
});
});
答案 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;
});