ReactNative:我想从列表中的数组中删除对象

时间:2018-10-28 07:56:49

标签: javascript react-native

这是我的删除项目代码

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',);

请帮助我这有什么问题,并为我提供从数组中删除多个对象的解决方案

1 个答案:

答案 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方法中放入任意数量的属性以符合您的要求。

希望这会有所帮助!