简体   繁体   English

在javascript中查找和替换嵌套数组对象

[英]Find and replace nested array object in javascript

How to find and replace over deep nested array object in javascript如何在javascript中查找和替换深层嵌套数组对象

I have a sample object name obj and filtered the object by id, in , and out (obj_res).我有一个示例对象名称obj并按id, in , and out (obj_res) 过滤对象。 How to find and update the particular obj with id fund in obj_res in javascript as shown in expected output如何在 javascript 中的obj_res中查找和更新具有id fund的特定 obj,如预期输出所示

I got stuck need help how to do in javascript我被卡住了需要帮助如何在 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
}]

A little bit late to the party, heh.晚会有点晚了,呵呵。 I needed to modify deeply nested objects too, and found no acceptable tool for that purpose.我也需要修改深度嵌套的对象,但没有找到可接受的工具。 Then I've made this and pushed it to npm.然后我做了这个并将它推送到 npm。

https://www.npmjs.com/package/find-and https://www.npmjs.com/package/find-and

This small lib can help with modifying nested objects in a lodash manner.这个小库可以帮助以 lodash 方式修改嵌套对象。 Eg,例如,

var findAnd = require("find-and");

const obj_res =[{
  "id": "trans",
  "in": "bank",
  "out": "bank",
  "value": 10
 },{
 "id": "fund",
 "in": "bank",
 "out": "bank",
 "value": 10
}];

findAnd.changeProps(obj_res, { id: 'fund' }, { in: 'credit' });

outputs exactly what you want.输出你想要的。

https://runkit.com/arfeo/find-and https://runkit.com/arfeo/find-and

Hope this could help someone else.希望这可以帮助别人。

Use find() to get first instance if you think there is only one.如果您认为只有一个,请使用find()获取第一个实例。 Otherwise use filter() and loop over each to modify properties on each matching object否则使用filter()并遍历每个来修改每个匹配对象的属性

 var obj_res = [{ "id": "trans", "in": "bank", "out": "bank", "value": 10 }, { "id": "fund", "in": "bank", "out": "bank", "value": 10 }] var fund = obj_res.find(({id}) => id === 'fund'); // returns array element or false if (fund) { fund.in = 'XXXXX'; console.log(obj_res) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM