猿问

在JavaScript中查找并替换嵌套数组对象

如何在JavaScript中查找和替换深层嵌套数组对象


我有一个示例对象名称,obj并按id, in , and out(obj_res)过滤了该对象。如何在javascript中使用id fundin查找和更新特定的obj,obj_res如预期的输出所示


我陷入了需要帮助的情况下如何在javascript中做


var obj_res = getValue("bank", "bank", "trans");

function getValue(send, receive, id){

   const temp = obj.map(e => Object.entries(e).map(([k, val]) => val)).flat(3)

    result_obj = temp.filter(x=>x.in ==send && && x=>x.out ==receive && x.id == id);

  return result_obj;}

//whole object input

var obj = [{

    "btob": [{

      "id": "trans",

      "in": "bank",

      "out": "bank",

      "value": 10,

    },{

      "id": "fund",

      "in": "bank",

      "out": "bank",

      "value": 10

    }],

    "ctob": [{

      "id": "trans",

      "in": "credit",

      "out": "bank",

      "value": 20

    },{

      "id": "fund",

      "in": "credit",

      "out": "bank",

      "value": 10

    }]

}]

//resultant obj after filter by id , in ,out 

var obj_res =[{

  "id": "trans",

  "in": "bank",

  "out": "bank",

  "value": 10

 },{

 "id": "fund",

 "in": "bank",

 "out": "bank",

 "value": 10

}]


 Expected Output:

  res=[{

  "id": "trans",

  "in": "bank",

  "out": "bank",

  "value": 10

 },{

"id": "fund",

"in": "credit",

"out": "bank",

"value": 10

}]


有只小跳蛙
浏览 325回答 2
2回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答