这是我的删除项目代码
const list = await AsyncStorage.getItem("ParcelList");
var res = JSON.parse(list);
//it contains array of multiple stored values [{value:{},value:{},value:{},value:{},]
var select = this.state.selectedData;
//this is the selected items I want to delete [{value:{},value:{}}]
const postsItems = res.filter(item => !select.includes(item));
await AsyncStorage.setItem('ParcelList',JSON.stringify(postsItems));
var resl = await AsyncStorage.getItem('ParcelList',);
请帮助我这有什么问题,并为我提供从数组中删除多个对象的解决方案
答案 0 :(得分:0)
您可以将Array.some用作.filter
谓词来测试多个属性上的对象,例如
var res = [{description: 'ab', VolumeWeight: 0.92}, {description: 'cb', VolumeWeight: 0.90}, {description: 'ab', VolumeWeight: 0.89}]
var selected = [{description: 'ab', VolumeWeight: 0.92}]
var result = res.filter(item => !selected.some(itemToRemove => item.VolumeWeight === itemToRemove.VolumeWeight && item.description === itemToRemove.description));
console.log(result);
在上面的示例中,将基于{description: 'ab', VolumeWeight: 0.92}
和res
属性值从description
数组中过滤VolumeWeight
。您可以在.some
方法中放入任意数量的属性以符合您的要求。
希望这会有所帮助!