-4

I have a object

let data1 = 
  {
    _id: "61d576ecb87f099d033a1930",
    name: 'Milk',
    quality: 'premium',
    price: 10,
    quantity: 10,
    bagSize: '10',
    bagCount: 10,
    status: 'Process',
    sellerDetails: [ [Object] ],
    image: '/uploads/milk.jpg'
  }

and I have array of objects

let data2 = [
  {
    _id: "61d576ecb87f099d033a1930",
    name: 'Milk',
    quality: 'Premium',
    price: 10,
    quantity: 10,
    bagSize: '10',
    bagCount: 10,
    status: 'Process',
    sellerDetails: [ [Object] ],
    image: '/uploads/premium.jpg'
  },
    {
    _id: "61d576ecb87f099d033a1931",
    name: 'Haldi',
    quality: 'Eagle',
    price: 10,
    quantity: 10,
    bagSize: '10',
    bagCount: 10,
    status: 'Process',
    sellerDetails: [ [Object] ],
    image: '/uploads/rai.jpg'
  }
]

Now I want to filter out data1 value from data2 so the expected result after filter should be

let data2 = [
    {
    _id: "61d576ecb87f099d033a1931",
    name: 'Haldi',
    quality: 'Eagle',
    price: 10,
    quantity: 10,
    bagSize: '10',
    bagCount: 10,
    status: 'Process',
    sellerDetails: [ [Object] ],
    image: '/uploads/rai.jpg'
  }
]

I have tried,

function filteredData(data1,data2){
  const filtered = data1._id !== data2._id
  return filtered
}
const filteredArr = data2.filter(filteredData)

Also I have referred this How can I acheive my expected result, am I doing something completely wrong ?

Sachin
  • 149
  • 1
  • 16
  • 2
    This is not how the callback for [`.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) works: `function(element, index, array){ /* ... */ }` – Andreas Jan 05 '22 at 13:25
  • Why does your `filter` function accept `data1, data2` as parameters? Please read the [documentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). `data1` and `data2` are global variables; the first argument of the `filter` callback is an element of the array `data2`. Don’t get confused here: pick a different name for the parameter. – Sebastian Simon Jan 05 '22 at 13:26
  • As per the documentation for [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), this is what the solution should be `data2 = data2.filter(ele => ele._id !== data1._id)` – Tyler Durden Jan 05 '22 at 13:27
  • Thank you Tyler Durden that line worked, and previously I wrote a similar solution but it was data1._id !== ele._id instead of what you provided. Thank you – Sachin Jan 05 '22 at 13:35

1 Answers1

-3

The following probably does what you want (untested).

Read up on the filter() documentation @: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

and map() @: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

data2.filter(el => {
    return data1._id !== el._id;
}
Gijs Beijer
  • 560
  • 5
  • 19
  • 1
    This makes no sense. `data1` is a simple object literal not an array. `map()` only works on arrays. Beyond that using the same map every iteration would be horribly inefficient if it was an array. Try running this yourself and see the errors it produces – charlietfl Jan 05 '22 at 13:33
  • @charlietfl You're totaly right, apologies, I thought it was a comparison of two arrays. As of the inefficiency you're right again. better to do this first, i'll edit the answer. – Gijs Beijer Jan 05 '22 at 14:32