0

I know this will be so simple but I am trying this for two days so I finally decided to take help from you guys... I have tried this probably the same question as mine but it is not giving me the answer.

ok so these are the two array

a = [{toNumber: "123", message: "Hi Deep "}, {toNumber: "321", message: "Test1"}]
b = [{toNumber: "321", message: "Test2"}, {toNumber: "123", message: "Hi Deep "}]

What I want is

diff = [{toNumber: "321", message: "Test2"}]

so quick help would be much appriciated.

deep bhatt
  • 27
  • 1
  • 9
  • 1
    So is toNumber the "key/id" ? – epascarello Apr 22 '20 at 16:36
  • 1
    Your objects do not have a `.value` and `.display` property. – Jonas Wilms Apr 22 '20 at 16:37
  • no toNumber is in array value... – deep bhatt Apr 22 '20 at 16:39
  • HOW are they linked, that is the question. Something has to be the same. Like the toNumber has to be the same or is it the message can be the same? Because your example would have 2 differences – epascarello Apr 22 '20 at 16:39
  • you have "312" and "321". is that a typo or deliberate? depending on what you are doing that might be a non-trivial comparison – user120242 Apr 22 '20 at 16:39
  • @JonasWilms please suggest what can I do? – deep bhatt Apr 22 '20 at 16:40
  • You could try to understand what the code does, comparing the datastructure of the other question to yours. Then adapt the solution. – Jonas Wilms Apr 22 '20 at 16:41
  • @epascarello yes all the time number would be the same and the message would be different. I need to find that different message. – deep bhatt Apr 22 '20 at 16:41
  • change .value and .display to toNumber and message, depending on what you are trying to do – user120242 Apr 22 '20 at 16:41
  • so map one to a lookup object and compare.... Your code does not work since you are using wrong properties – epascarello Apr 22 '20 at 16:42
  • @user120242 yes it was thank you for bringing it in the notice. – deep bhatt Apr 22 '20 at 16:42
  • 2
    If you're looking for the symmetric difference between 2 arrays of objects, then your `diff` result is wrong. Please clarify what you're looking for _mathematically_ – Raz Chiriac Apr 22 '20 at 16:44
  • @RazChiriac Yes. – deep bhatt Apr 22 '20 at 16:48
  • @deepbhatt The correct answer for a **symmetric difference** should be `[{toNumber: "321", message: "Test1"}, {toNumber: "321", message: "Test2"}]`. That is the collection of objects that is NOT found in BOTH arrays. Am I correct in my assumption? I'm confused by the discrepancy between your question and all these answers. Let me know so I can try to help. – Raz Chiriac Apr 22 '20 at 17:58
  • Thank you, everyone. You guys saved my day. Specially @JonasWilms to allow other people to see it and give their feedback. Much appreciated. – deep bhatt Apr 22 '20 at 19:18
  • Honestly I think you would've learned much more if you would've adopted one of the solutions you've already found to your usecase (with our help for sure) instead of getting a copy & paste piece ... whatever if you're happy with the answers then that's fine too. – Jonas Wilms Apr 22 '20 at 21:48

2 Answers2

0

So with your code you need to look at the other object and see if it has any keys that match. If it matches, you need to see if the message matches. So you can make a look up object that has the list of ids. You can than loop over your second array and see if they march.

var a = [
  {toNumber: "123", message: "Hi Deep "},
  {toNumber: "321", message: "Test1"}
]
var b = [
  {toNumber: "321", message: "Test2"},
  {toNumber: "123", message: "Hi Deep "}
]

// create the lookup from the first array
var lookup = a.reduce( function (lookUpObj, entryA) {
  // set the object property with the toNumber property
  lookUpObj[entryA.toNumber] = entryA.message
  return lookUpObj
}, {})

// Now loop over the array and look for the differences
var diff = b.reduce(function (arr, entryB) {
  // grab the entry from the lookup object we created
  var orginalMessage = lookup[entryB.toNumber]
  // if we do not have it listed OR the message is different
  // add it to the list as changed.
  if (!orginalMessage || orginalMessage !== entryB.message) {
    arr.push(entryB)
  }
  return arr
}, [])

console.log(diff)

Now that will match any differences from a to b. If anything was removed in B that is not in A it will not be caught.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Where is the problem ???

const a = 
    [ { toNumber: "123", message: "Hi Deep " } 
    , { toNumber: "321", message: "Test1"    } 
    ]
const b = 
    [ { toNumber: "321", message: "Test2"    } 
    , { toNumber: "123", message: "Hi Deep " } 
    ] 

const diff = b.filter(eB=>!a.some(eA=>( eA.toNumber===eB.toNumber
                                      && eA.message===eB.message )))

document.write( JSON.stringify( diff ) )
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40