Compare two JSON and create a json with the common value

Viewed 609

I want to create two JSON array based on a common property value by comparing two JSONs.

this.linkedParticipants =[
  {
    "id": 3,
    "name": "Participant 2",
    "email": "participant2@fico.com"
  },
  {
    "id": 2,
    "name": "Participant 1",
    "email": "participant1@fico.com"
  },
  {
    "id": 1,
    "name": "Libin Varghese",
    "email": "LibinVarghese@fico.com"
  },
  {
    "id": 7,
    "name": "Participant 5",
    "email": "participant5@fico.com"
  }
]
this.appointmentList = [
  {
    "id": 32,
    "participant": {
      "id": 1,
      "name": "Libin Varghese",
      "email": "LibinVarghese@fico.com"
    }
  },
  {
    "id": 33,
    "participant": {
      "id": 7,
      "name": "Participant 5",
      "email": "participant5@fico.com"
    }
  }
]
this.invitedList = [];
this.confirmedList = [];
let temp = {}
this.linkedParticipants.forEach((participant, i) => {
  this.appointmentList.forEach((appointment, j) => {
    if (appointment.participant.name.indexOf(participant.name)) {
      temp = {
        'participant': participant
      }
      this.invitedList.push(temp);
    }
    else {
      this.confirmedList.push(appointment)
    }
  })
})

But the code is not working as expected as the two values this.invitedList is giving out duplicate values. My comparing condition has some issues I think.

3 Answers

can be done using filter and comparer function.

 var linkedParticipants =[
      {
        "id": 3,
        "name": "Participant 2",
        "email": "participant2@fico.com"
      },
      {
        "id": 2,
        "name": "Participant 1",
        "email": "participant1@fico.com"
      },
      {
        "id": 1,
        "name": "Libin Varghese",
        "email": "LibinVarghese@fico.com"
      },
      {
        "id": 7,
        "name": "Participant 5",
        "email": "participant5@fico.com"
      }
    ]
    var appointmentList = [
      {
        "id": 32,
        "participant": {
          "id": 1,
          "name": "Libin Varghese",
          "email": "LibinVarghese@fico.com"
        }
      },
      {
        "id": 33,
        "participant": {
          "id": 7,
          "name": "Participant 5",
          "email": "participant5@fico.com"
        }
      }
    ]


    function comparer(otherArray){
        return function (current) {
            return otherArray.filter(function(other) {
                return other.id === current.id
            }).length===0;
        }
    }

   

   var invitedList = appointmentList.map(i=> {return i.participant});


    var confirmedList=linkedParticipants.filter(comparer(invitedList ));
    
    console.log(invitedList);
    console.log(confirmedList)

You can achieve this with different combinations of filter() :

 var linkedParticipants =[
  {
    "id": 3,
    "name": "Participant 2",
    "email": "participant2@fico.com"
  },
  {
    "id": 2,
    "name": "Participant 1",
    "email": "participant1@fico.com"
  },
  {
    "id": 1,
    "name": "Libin Varghese",
    "email": "LibinVarghese@fico.com"
  },
  {
    "id": 7,
    "name": "Participant 5",
    "email": "participant5@fico.com"
  }
];
var appointmentList = [
  {
    "id": 32,
    "participant": {
      "id": 1,
      "name": "Libin Varghese",
      "email": "LibinVarghese@fico.com"
    }
  },
  {
    "id": 33,
    "participant": {
      "id": 7,
      "name": "Participant 5",
      "email": "participant5@fico.com"
    }
  }
];


var confirmedList = appointmentList.filter((a) => {
  return linkedParticipants.find((p) => p.id === a.participant.id);
});

var invitedList = linkedParticipants.filter((p) => {
  return !appointmentList.find((a) => p.id === a.participant.id);
});

console.log(confirmedList)
console.log(invitedList)

You may consider using Set to find an intersection. The common approach is like this:

function intersect(a, b) {
  const setB = new Set(b);
  return [...new Set(a)].filter(x => setB.has(x));
}

So, regarding your question, you may do your task by:

function intersect(linkedParticipants, appointmentList) {
  const setB = new Set(linkedParticipants);
  return [...new Set(appointmentList)].filter(({participant}) => setB.has(participant));
}

I create arrays in the following way:

const id1 = {
    "id": 1,
    "name": "Libin Varghese",
    "email": "LibinVarghese@fico.com"
  };
const id2 = {
    "id": 2,
    "name": "Participant 1",
    "email": "participant1@fico.com"
  };
const id3 = {
    "id": 3,
    "name": "Participant 2",
    "email": "participant2@fico.com"
  };
const id7 = {
    "id": 7,
    "name": "Participant 5",
    "email": "participant5@fico.com"
  };
const linkedParticipants =[id1, id2, id3, id7]
const appointmentList = [
  {
    "id": 32,
    "participant": id1
  },
  {
    "id": 33,
    "participant": id7
  }
];

https://i.stack.imgur.com/oqtZw.png Please, remember, that object is reference type.

Related