0

How to update particular object based on condition in javascript

I have a object named obj, based on condition:

'in'=="credit" and 'out'=="bank" and "id"=="trans", get the object and add default as 'in'=="bank" and 'out'=="bank" and "id"=="fund.

'in'=="debit" and 'out'=="bank" and "id"=="fund", get the object and add default as 'in'=="bank" and 'out'=="bank" and "id"=="trans".

I tried filter but got stuck

function getValue(send,receive, id){
 const temp = obj.map(e => Object.entries(e).map(([k, val]) => val)).flat(3)
    this.selectedProviderList = temp.filter(x=>x.in== send && x.in ==receive && x.id==id);
}

getValue("credit", "bank", "trans");
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
    }],
 "dtob": [{
      "id": "trans",
      "in": "debit",
      "out": "bank",
      "value": 20
    },{
      "id": "fund",
      "in": "debit",
      "out": "bank",
      "value": 10
    }]
}]
// get the values if 'in' is 'credit' and 'out' is 'bank' and id is "trans" 

Expected Output
[
   {
      "id": "trans",
      "in": "credit",
      "out": "bank",
      "value": 20
    },
    {
      "id": "fund",
      "in": "bank",
      "out": "bank",
      "value": 10
    }
]
// get the values if 'in' is 'debit' and 'out' is 'bank' and id is "fund" 

[{
      "id": "fund",
      "in": "debit",
      "out": "bank",
      "value": 10
    },{
      "id": "trans",
      "in": "bank",
      "out": "bank",
      "value": 10
    }]
1
  • 1
    Sorry, I don't get it. For the expected output, the first object in the array is due to the filtered in, out and fund values. But where does the second object in the array come from? Commented Apr 5, 2019 at 8:06

1 Answer 1

1

Since in is a reserved keyword in JavaScript, you'd need to use [] (square bracket) notation. (I also changed the second in to out).

function getValue(send, receive, id) {
  const temp = obj.map(e => Object.entries(e).map(([, val]) => val)).flat(3);
  this.selectedProviderList = temp.filter(x => x["in"] == send && x["out"] == receive && x["id"] == id);
}

getValue("credit", "bank", "trans");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.