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

时间:2019-04-07 07:11:24

标签: reactjs clone

我有一个JSON对象数组,想在状态中添加udpdate

当前状态为

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
  }
];

尝试使用不变性帮助程序包和对象分配似乎没有任何效果,请您帮帮我 预先感谢

1 个答案:

答案 0 :(得分:0)

您可以这样做:

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

然后使用map来更新clonedArray

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