0

I have two array, One of them is like this:

 const array1 =  [
      Object {
        "ItemId": 1,
        
      },
      Object {
        "ItemId": 2,
        
      },
    ]  

other array is :

     const array2 = [
      Object {
        "obj1": Object {
    
          "Id": 4736,
    
        },
        "obj2": Object {
    
          "ItemId": 1,
     
        },
      }
]

I want to get items in array1, which are not equals in obj2 of array2 .

I tried this but doesnt work

array1.filter(function (item) {
        return array2.map((x) => {
          return x.obj2 != item;
        });
3
  • array2 Isn't a valid array? Did something go wrong when copy/pasting? Commented Mar 5, 2021 at 9:40
  • There are three issues that immediately catch my eye: 1. array1.filter is expecting a truthy value and array2.map is always returning an array. In this case it will always be truthy. 2. array2.map is just creating a new array with the results of the callback function (x.obj2 != item). This will return an array filled with boolean values. [true, false, true] 3. The comparison looks off to me. Are both objects (item and x.obj) referring to the same object reference? If not, you might rather compare item.ItemId with obj2.ItemId. Commented Mar 5, 2021 at 9:44
  • @webwelten u are right it return boolean. It is my mistake. Actually I want to get object. and also they referring same object. i can filter with ItemId as well Commented Mar 5, 2021 at 10:03

2 Answers 2

1

instead of array2.map, you're looking for Array.protype.some or Array.prototype.every. Why?

array1.filter is expecting a truthy value for every item.

array2.map is returning an array (!) with the results of its callback. In your case it's the result of each comparisson x.obj2 != item. It could look something like this: [true, false]. But this array will always evaluate to true.

array2.some() or array2.every() also iterate over the array but will return a boolean for the condition you're using. You can try for yourself and check the code sample afterwards.

 const array1 =  [
  {
    "ItemId": 1,
  },
  {
    "ItemId": 2,
  },
];
    
const array2 = [
  {
    "obj1": {
      "ItemId": 4736,
    },
    "obj2": {
      "ItemId": 1,
    },
  },
];

// errornous filter usage
const test = array1.filter(item => {
  const arr = array2.map(x => x.obj2 !== item); 
  return arr;
});

// suggestion
const filteredResult = array1.filter(item => {
  // both ways work
  // return array2.every(x => x.obj2.ItemId !== item.ItemId); 
  return !array2.some(x => x.obj2.ItemId === item.ItemId); 
  });

console.log('initial result', test);
console.log('correct result', filteredResult);

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

2 Comments

I answered your question above
oh it almost work ! actually it works however when array2 has no item it returns an error just
0

Try this

array1=array1.filter((item) => {
    return array2[0].obj2.ItemId !== item.ItemId;
})

1 Comment

how can i handle if array2 has more object ?

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.