微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在javascript嵌套对象数组中设置特定属性值和另一个属性值?

如何解决如何在javascript嵌套对象数组中设置特定属性值和另一个属性值?

 var arr = {
    "data": [
    {
        id    : "a1",guid  : "sdfsfd",value : "abc","status": "active"
      },{
        id    : "a2",guid  : "deaf",value : "def2",details : {
         "status": "inactive","body": done
                  }
        "stat": "inactive"
      },guid  : "blind",value : "def4",details : {
           "status": "inactive","body": donenot
                  }
        "stat": "inactive"
      },]
      
    }
    
    console.log(arr.data.filter(item => item.stat === "inactive"))

输出

 [{
  details: {
    body: "done",status: "inactive"
  },guid: "deaf",id: "a2",stat: "inactive",value: "def2"
},{
  details: {
    body: "done",guid: "blind",id: "a3",value: "def4"
}]

如果我想设置

  1. 每个此类非活动状态的值(在这两个输出中)-> value 属性 guid 属性...?
  2. 每个此类非活动状态的值(在这两个输出中)-> 状态详细信息中的正文相同?

解决方法

对于

  1. 您可以执行以下操作:arr.data = arr.data.filter(item => item.stat === "inactive").map(e => ({...e,value : e.guid}))

输出

{
  data: [{
  details: {
    body: "done",status: "inactive"
  },guid: "deaf",id: "a2",stat: "inactive",value: "deaf"
},{
  details: {
    body: "done",guid: "blind",--> same as value
  id: "a3",value: "blind"
}]
}

对于

  1. 您可以执行以下操作:arr.data = arr.data.filter(item => item.stat === "inactive").map(e => ({...e,details : {...e.details,status: e.details.body}}))

输出

{
  data: [{
  details: {
    body: "done",status: "done"
  },value: "def2"
},id: "a3",value: "def4"
}]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。