0

I have two arrays: productos and prevProductos, I'm filtering them to check if they have an object with the same properties and deleting them if it's true.
I want to check if they have the same id, the same amount and the same cost, if the three of those properties are the same I want to delete that object but if one changes I need to keep it.
This line of code doesn't work because I changed the "amount" property so they're not equals but I get an empty object, how can I fix that?

example: 

const productos = [{
prodId: 3,
amount: 30,
const:100
}];

const prevProductos = [{
prodId: 3,
amount: 20,
const:100
}]

let addedProducts = productos.filter( item => 
  !prevProductos.some( obj => 
     ( obj.prodId === item.prodId 
    && obj.amount === item.amount 
    && obj.cost   === item.cost
    )
  )
);

console.log(addedProducts)
//should show 
[{
prodId: 3,
amount: 20,
const:100
}]
// currently getting
[]

  • "This line of code doesn't work because I changed the "amount" property so they're not equals but I get an empty object" what does this mean? – cmgchess Apr 13 '23 at 13:52
  • It means that the two arrays have an object with the same prodId but with different amoun – alexgarciaalcuadrado Apr 13 '23 at 13:54
  • do you have some (small) data and wanted result? – Nina Scholz Apr 13 '23 at 13:55
  • I added an example to make easier to read – alexgarciaalcuadrado Apr 13 '23 at 13:57
  • 1
    you should be getting the amount:30 back since all fields are not same. not an empty array – cmgchess Apr 13 '23 at 13:58
  • 1
    it is working as expected ... – Nina Scholz Apr 13 '23 at 13:59
  • Yes, that's what i'm expecting but i get an empty array. I have a button that updates the amount of that object and I can see the whole "productos" array is updated correctly but when I run it I get the empty array as if the condition was true – alexgarciaalcuadrado Apr 13 '23 at 14:00
  • maybe the reason is something else – cmgchess Apr 13 '23 at 14:03
  • This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). – Mister Jojo Apr 13 '23 at 14:03
  • Thanks, I just wanted to make sure it wasn't that line of code. I'll check the rest – alexgarciaalcuadrado Apr 13 '23 at 14:06
  • @alexgarciaalcuadrado I did not see any issue in the code you posted. It is working fine as per your requirement. Here is the working fiddle : https://jsfiddle.net/7fxe5tmv/ – Debug Diva Apr 14 '23 at 07:48
  • It does work, the problem was another part of my code that I could not fix yet. Inside a useeffect that runs only once I set the state of both productos and prevProductos to the same value (the prev products). After that I have a button that changes the amount propertie of only the "productos" variable, but i found out that it also updates "prevProductos". I don't understand why because prevProductos is a variable that doesn't depend on "productos" and I only set its value whe the component mounts for the first time. Any idea what might be? – alexgarciaalcuadrado Apr 14 '23 at 13:51

0 Answers0