为了账号安全,请及时绑定邮箱和手机立即绑定

循环遍历对象数组并更改 JavaScript 中键的值

循环遍历对象数组并更改 JavaScript 中键的值

RISEBY 2023-07-29 15:47:19
我有以下 json 数组,它是我从角度应用程序中的服务响应中解构的。this.selectedList = [  [    {      "meterNumber": "Z00001410",      "utilType": "elec",      "mprnGprn": "3100000334",      "isChecked": true    },    {      "meterNumber": "Z00001410",      "utilType": "gas",      "mprnGprn": "1000003344",      "isChecked": true    }  ],  [    {      "meterNumber": "Z00001410",      "utilType": "elec",      "mprnGprn": "1000003344",      "isChecked": true    }  ],  [    {      "meterNumber": "Z00001410",      "utilType": "elec",      "mprnGprn": "1000003344",      "isChecked": true    }  ]]我需要做的是迭代列表并在单击事件上将isChecked值更改为 false。我尝试过以下方法  selectAll(){    this.selectedList = this.data.reportList.map((meter) => meter.pm);    this.newList = this.selectedList.map((data) => {      data.forEach(item => item.isChecked = false);  });  console.log(this.newList);  }上面的代码将值更改为 false,但是当我尝试控制台 newList 时,它是一个未定义值的数组。这是执行此操作的最佳方法还是我在这里做错了什么。
查看完整描述

2 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

您只需要从return data地图的每次迭代中添加:


this.selectedList = [

  [{

      "meterNumber": "Z00001410",

      "utilType": "elec",

      "mprnGprn": "3100000334",

      "isChecked": true

    },

    {

      "meterNumber": "Z00001410",

      "utilType": "gas",

      "mprnGprn": "1000003344",

      "isChecked": true

    }

  ],

  [{

    "meterNumber": "Z00001410",

    "utilType": "elec",

    "mprnGprn": "1000003344",

    "isChecked": true

  }],

  [{

    "meterNumber": "Z00001410",

    "utilType": "elec",

    "mprnGprn": "1000003344",

    "isChecked": true

  }]

]


selectAll = () => {

  this.newList = this.selectedList.map((data) => {

    data.forEach(item => {item.isChecked = false});

    return data;

  });

  console.log(this.newList);

}


selectAll()


查看完整回答
反对 回复 2023-07-29
?
ibeautiful

TA贡献1993条经验 获得超5个赞

请记住,使用地图时需要返回一个值。这是一个可行的解决方案:


this.selectedList = [

  [

    {

      "meterNumber": "Z00001410",

      "utilType": "elec",

      "mprnGprn": "3100000334",

      "isChecked": true

    },

    {

      "meterNumber": "Z00001410",

      "utilType": "gas",

      "mprnGprn": "1000003344",

      "isChecked": true

    }

  ],

  [

    {

      "meterNumber": "Z00001410",

      "utilType": "elec",

      "mprnGprn": "1000003344",

      "isChecked": true

    }

  ],

  [

    {

      "meterNumber": "Z00001410",

      "utilType": "elec",

      "mprnGprn": "1000003344",

      "isChecked": true

    }

  ]

]


selectAll = () => {

    this.selectedList = this.selectedList.map(subArray => {

        subArray.forEach(_ => _.isChecked = false);

        return subArray;

    });

    console.log(this.selectedList)

}


selectAll();


查看完整回答
反对 回复 2023-07-29
  • 2 回答
  • 0 关注
  • 181 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信