Sen
Sen

Reputation: 773

How to remove object by property in javascript

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
  }
]```

Upvotes: 0

Views: 209

Answers (2)

p u
p u

Reputation: 1445

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)

Upvotes: 0

Maheer Ali
Maheer Ali

Reputation: 36574

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)

Upvotes: 1

Related Questions