0
const array1 = [
  {id: 1, Q_type: "AL"},
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 4, Q_type: "DL"}
]
const array2 = [
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 4, Q_type: "DL"}
]

const arrAfterComparison = array1.filter(val => !array2.includes(val))

I am trying to compare array1 and array2 and getting the object which is not present in both of these arrays

Expected output

arrAfterComparison = [{id:1,Q_type:"AL"}]
5
  • 2
    What have you tried so far to solve this on your own? Commented Feb 5, 2021 at 14:26
  • @Andreas added my unsuccefful attempt.. i have tried many solutions but i am stuck here :( Commented Feb 5, 2021 at 14:29
  • @Phil expected o/p would be this [{id:4,Q_type:"AL"}] or [{id:1,Q_type:"AL"}] ? Commented Feb 5, 2021 at 14:31
  • my bad .. it will be [{id:1,Q_type:"AL"}] Commented Feb 5, 2021 at 14:38
  • @Phil u sure it's just one object there that is different? Commented Feb 5, 2021 at 14:44

2 Answers 2

1

Use Array.some() inside Array.filter() method callback.

const array1 = [
  {id: 1, Q_type: "AL"},
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 4, Q_type: "DL"}
]
const array2 = [
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 1, Q_type: "DL"}
]

const output = array1.filter(item => !array2.some(a => a.Q_type === item.Q_type))
console.log(output);

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

2 Comments

This solves it flawlessly.. been stuck here for a while .. never had to use the some() method before i will read the docx thanks
Don't you think there will be another question anywhere on SO that already solves this problem?
1

When you compare, actually 2 objects are not similar.. look closely(it isn't an error)

  1. Both the ones with id:1 have different values for Q_type
  2. The one with the id of 4(we all see this one)

const array1 = [
  {id: 1, Q_type: "AL"},
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 4, Q_type: "DL"}
]
const array2 = [
  {id: 2, Q_type: "BL"},
  {id: 3, Q_type: "CL"},
  {id: 1, Q_type: "DL"}
]

function oddIndexesOut(_arr1,_arr2){
  //objects may "look" the same but if they don't point to the same thing, they're not equal.. however if i turn it into a string, things that "look" equal are equal
  _arr1=_arr1.map(a=>JSON.stringify(a))
  _arr2=_arr2.map(a=>JSON.stringify(a))
  
  //comparison function(if things "look" similar)
  function compare(arr1,arr2){
    var x=arr1.filter(a=>!arr2.includes(a))
    return x.map(a=>JSON.parse(a))
  }
  
  //the longest array is used(so that checking can be full)
  if(_arr1.length>_arr2.length){
    return compare(_arr1,_arr2)
  }
  return compare(_arr2,_arr1)
}

console.log(oddIndexesOut(array1,array2))

Comments

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.