Arrays 如何在react native中比较和更新数组属性值?
我有两个数组,calls和Othercalls以及一些联系方式。现在我想比较这两个数组,并在calls数组中将status属性更新为false。状态值应仅在Othercalls数组中的联系人中更改Arrays 如何在react native中比较和更新数组属性值?,arrays,react-native,react-native-flatlist,Arrays,React Native,React Native Flatlist,我有两个数组,calls和Othercalls以及一些联系方式。现在我想比较这两个数组,并在calls数组中将status属性更新为false。状态值应仅在Othercalls数组中的联系人中更改 let tempList = this.state.Othercalls.map(item => item.phoneNumber); let result = this.state.calls.filter(item => (tempList.includes(item.phon
let tempList = this.state.Othercalls.map(item => item.phoneNumber);
let result = this.state.calls.filter(item => (tempList.includes(item.phoneNumber)))
result.map((listItem, index) => {
listItem.status = false
})
我在这里试过的是
这是我的两个数组
this.state = {
calls: [
{
fullname: 'Mark Doe',
phoneNumber: '0112234567',
status: true
},
{
fullname: 'Clark Man',
phoneNumber: '0723434567',
status: true
},
{
fullname: 'Jaden Boor',
phoneNumber: '0778902356',
status: true
},
{
fullname: 'Srick Tree',
phoneNumber: '0980234589',
status: true
},
{
fullname: 'John Doe',
phoneNumber: '0112255644',
status: true
},
{
fullname: 'Mark Doe',
phoneNumber: '0723567890',
status: true
},
{
fullname: 'John Doe',
phoneNumber: '0778904321',
status: true
},
{
fullname: 'Mark Doe',
phoneNumber: '0785674334',
status: true
},
{
fullname: 'Jaden Boor',
phoneNumber: '0713456980',
status: true
},
{
fullname: 'Mark Doe',
phoneNumber: '0112357654',
status: true
},
],
Othercalls: [
{fullname: 'Mark Doe', phoneNumber: '0112234567'},
{fullname: 'Clark Man', phoneNumber: '0723434567'},
{fullname: 'Jaden Boor', phoneNumber: '0778902356'},
]
};
这里我得到了othercalls数组的详细信息,并将状态更改为false。但这不是我想要的。我需要比较这两个数组,并仅在othercalls数组中的联系人中将状态值更新为false。我还需要调用数组的完整数据
let tempList = this.state.Othercalls.map(item => item.phoneNumber);
let result = this.state.calls.filter(item => (tempList.includes(item.phoneNumber)))
result.map((listItem, index) => {
listItem.status = false
})
我希望这个解决方案可以解决我的问题:您应该执行以下操作来更新calls数组
const data = this.state.calls.map(item => {
if (this.state.Othercalls.find(contact => contact.phoneNumber === item.phoneNumber)) {
item.status = false;
}
return item;
});
数据将具有更新的数组,如果需要,可以将其设置为状态