0

I would like to know how to remove the object by property in nested array object

I have whole list of object in sampleobj, compare each id with apitrans, apifund, if success is false, remove obj in sampleobj

Remove the object if the success is false, in sampleobj.

I have tried:

var result = sampleobj.foreach(e=>{
   if(e.id === "trans" && apitrans.success== true){
      Object.assign(e, apitrans);
   }
  if(e.id === "fund" && apifund.success== true){
      Object.assign(e, apifund);
   }

  // if success false remove the object.
})

//inputs scenario 1
var sampleobj=[{
    id: "trans",
    amount: "100",
    fee: 2
   },
   {
    id: "fund",
    amount: "200",
    fee: 2
   }]
var apitrans = 
  {
   success: true,
   id: "trans",
   tamount: "2000",
   fee: "12"
  }
var apifund =
  {
   success: false,
   id: "fund",
   tamount: "4000",
   fee: "10"
  } 

//inputs scenario 2 how to do same if property name differs
if error, status error, or success false remove obj in sampleobj

var sampleobj=[{
    id: "trans",
    amount: "100",
    fee: 2
   },
   {
    id: "fund",
    amount: "200",
    fee: 2
   },
{ id: "insta", amount: "400", fee: 2 }
]

var apitrans = {success: true,id: "trans",tamount: "2000",fee: "12"}
var apiinsta = { errors: [{code:"error.route.not.supported"}],id: "insta",tamount: "2000",fee: "12"}
var apifund = { status: "error", id: "fund", tamount: "4000", fee: "10" }

var sampleobj=[{
//Expected Output
result: [
  {
    id: "trans",
    amount: "100",
    fee: 2
  }
]```
6
  • 1
    You can use filter e.g. ` const result = sampleobj.filter(o=>o.id===apitrans.id && !apitrans.success)`. Change above condition accordingly. See more here Commented May 3, 2019 at 4:50
  • can you explain more..? and tell us what have you tried so far because i am not getting your question you want to compare sampleobj arr with both obj.both objs have ids which is present in an array (id-fund n id-trans) Commented May 3, 2019 at 4:50
  • @the_ultimate_developer I have updated the post, added what i have tried Commented May 3, 2019 at 4:53
  • @the_ultimate_developer I need to compare apitrans with sampleobj and apifund with sampleobj if success false remove the obj in sampleobj Commented May 3, 2019 at 4:54
  • @sowmiya yep understood and have posted my answer Commented May 3, 2019 at 5:05

2 Answers 2

1

You can use filter() to remove elements from array.

  • Create a helper function(func) which takes two objects as parameter and compare id property of both and check success property of one of them.
  • Then use filter() of the given array and put both given objects array [apitrans,apifund].
  • Then use some() method on [apitrans,apifund] and check if any of them have id equal the current element using Helper Function.

var arr=[ { id: "trans", amount: "100", fee: 2 }, { id: "fund", amount: "200", fee: 2 } ]

var apitrans = {success: true,id: "trans",tamount: "2000",fee: "12"}
var apifund = { success: false, id: "fund", tamount: "4000", fee: "10" }

const func = (obj1,obj2) => obj1.id === obj2.id && obj2.success

const res = arr.filter(x => [apitrans,apifund].some(a => func(x,a)));
console.log(res)

Sign up to request clarification or add additional context in comments.

6 Comments

thanks , but what if property differs, updated your answer input
@sowmiya You didn't mentioned anything like that in the question.
yes,apologies. I havent but its different scenario,
@sowmiya You should edit the question with complete information.
@sowmiya var apiinsta = { errors: [code:""],id: "insta",tamount: "2000",fee: "12"} is wrong because it contains array with keys. [code:""] array can't have key:value.
|
0

You can filter out your array with conditions i.e filter gives you new array instead of changing the original array

var arr = [{
    id: "trans",
    amount: "100",
    fee: 2
  },
  {
    id: "fund",
    amount: "200",
    fee: 2
  }
]
var apitrans = {
  success: true,
  id: "trans",
  tamount: "2000",
  fee: "12"
}
var apifund = {
  success: false,
  id: "fund",
  tamount: "4000",
  fee: "10"
}



var filter = arr.filter(function(item) {
  //console.log(item);
  if (item.id === apitrans.id && apitrans.success) {
    return item
  }

});
console.log(filter);

Or if you want an original array to be modified instead of getting a new array, you can use your given approach with some update i.e

var arr = [{
    id: "trans",
    amount: "100",
    fee: 2
  },
  {
    id: "fund",
    amount: "200",
    fee: 2
  }
]
var apitrans = {
  success: true,
  id: "trans",
  tamount: "2000",
  fee: "12"
}
var apifund = {
  success: false,
  id: "fund",
  tamount: "4000",
  fee: "10"
}


arr.forEach(e => {
  if (e.id === "trans" && apitrans.success == true) {
    Object.assign(e, apitrans);
  } else if (e.id === "fund" && apifund.success == true) {
    Object.assign(e, apifund);
  } else {
    // if success false remove the object.
    var index = arr.indexOf(e);
    arr.splice(index, 1);
  }
})
console.log("resulted original arr", arr)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.