如何在 React Native 中比较和更新 Array 属性 值?
How to compare and update an Array property value in react native?
我有两个数组,"calls" 和 "Othercalls",其中包含一些联系方式。现在我想比较这两个数组并在 "calls" 数组中将 "status" 属性 更新为 false。状态值应该只在 "Othercalls" 数组中的联系人发生变化。
What I tried here is,
这是我的两个数组。
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。而且我还需要 "calls" 数组的完整数据。
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
})
我想要这个解决方案来解决我的问题:(How to compare two array in react native?)
您应该像下面这样更新调用数组
const data = this.state.calls.map(item => {
if (this.state.Othercalls.find(contact => contact.phoneNumber === item.phoneNumber)) {
item.status = false;
}
return item;
});
数据会有更新后的数组,需要的话可以设置成状态
我有两个数组,"calls" 和 "Othercalls",其中包含一些联系方式。现在我想比较这两个数组并在 "calls" 数组中将 "status" 属性 更新为 false。状态值应该只在 "Othercalls" 数组中的联系人发生变化。
What I tried here is,
这是我的两个数组。
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。而且我还需要 "calls" 数组的完整数据。
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
})
我想要这个解决方案来解决我的问题:(How to compare two array in react native?)
您应该像下面这样更新调用数组
const data = this.state.calls.map(item => {
if (this.state.Othercalls.find(contact => contact.phoneNumber === item.phoneNumber)) {
item.status = false;
}
return item;
});
数据会有更新后的数组,需要的话可以设置成状态