【问题标题】:How to compare and update an Array property value in react native?如何在本机反应中比较和更新数组属性值?
【发布时间】:2020-05-19 05:13:48
【问题描述】:

我有两个数组,“电话”和“其他电话”以及一些联系方式。现在我想比较这两个数组并将“调用”数组中的“状态”属性更新为 false。状态值应仅在“Othercalls”数组中的联系人中更改。

我在这里尝试的是,

这是我的两个数组。

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
    })

我希望这个解决方案在这里解决我的问题:(How to compare two array in react native?)

【问题讨论】:

    标签: arrays react-native react-native-flatlist


    【解决方案1】:

    您应该执行以下操作来更新调用数组

     const data = this.state.calls.map(item => {
        if (this.state.Othercalls.find(contact => contact.phoneNumber === item.phoneNumber)) {
          item.status = false;
        }
        return item;
      });
    

    数据会有更新后的数组,你可以根据需要设置状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2021-10-26
      • 1970-01-01
      相关资源
      最近更新 更多