如何在状态中更新嵌套json对象的数组?

问题描述 投票:-4回答:1

我有一个JSON对象数组,并希望在状态更新

现状是

this.state.data  has [
  {
     "check_box_list": [
        {"name":"Courier Received by Investor","value":true},
        {"name":"Courier Sent By Investor","value":true},
        {"name":"Signed CA-Doc received","value":true},
        {"name":"Cheque/Payment Received","value":true},
        {"name":"Cancelled Cheque Received","value":true},
        {"name":"ID Proof Received","value":true},     
        {"name":"Address Proof Received","value":true},
        {"name":"KYC Received","value":true},
        {"name":"Signed CA pictures/scan received","value":true},
        {"name":"KRA Updated","value":true}
    ],
    "ca_doc_offline_comment":[""],
    "_id":"5ca89fcd0434ff4ba08b2d37",
    "user_id":"1589",
    "investor_name":"Anupam Mittal",
     "source":"Banner",
     "completed_check_box_list":false,
     "investment_information_id":1154
  }, 
 ...etc
]

想要用过滤后的数据替换状态:

var filtered_data = [
  {
    check_box_list: [
      { name: "Courier Received by Investor", value: false },
      { name: "Courier Sent By Investor", value: false },
      { name: "Signed CA-Doc received", value: false },
      { name: "Cheque/Payment Received", value: false },
      { name: "Cancelled Cheque Received", value: false },
      { name: "ID Proof Received", value: false },
      { name: "Address Proof Received", value: false },
      { name: "KYC Received", value: false },
      { name: "Signed CA pictures/scan received", value: false },
      { name: "KRA Updated", value: false },
      { name: "KYC Updated", value: false }
    ],
    ca_doc_offline_comment: [""],
    _id: "5ca987f8198d13a54714789e",
    investor_name: "Samala Sumanth",
    completed_check_box_list: false
  },
  {
    check_box_list: [
      { name: "Courier Received by Investor", value: false },
      { name: "Courier Sent By Investor", value: false },
      { name: "Signed CA-Doc received", value: false },
      { name: "Cheque/Payment Received", value: false },
      { name: "Cancelled Cheque Received", value: false },
      { name: "ID Proof Received", value: false },
      { name: "Address Proof Received", value: false },
      { name: "KYC Received", value: false },
      { name: "Signed CA pictures/scan received", value: false },
      { name: "KRA Updated", value: false },
      { name: "KYC Updated", value: false }
    ],
    ca_doc_offline_comment: [""],
    _id: "5ca98801198d13a54714789f",
    investor_name: "Samala Sumanth",
    completed_check_box_list: false
  }
];

尝试过不变的帮助包和对象分配似乎没什么工作可以请你帮我解答,提前谢谢

reactjs clone
1个回答
0
投票

你可以做:

const clonedArray = JSON.parse(JSON.stringify(myArray))

然后使用map更新clonedArray

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

© www.soinside.com 2019 - 2024. All rights reserved.