Home > Software engineering >  Check if array of objects already contain a specific combination of values
Check if array of objects already contain a specific combination of values

Time:08-19

In my application I have an array of objects which contains users:

var users = [
  {id: 1, firstName: 'Max', lastname: 'Muster', birthdate: '10.10.1990', number: '123'},
  {id: 2, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '254'}
];

When I click on a button I trigger an event which posts a user object into this array:

var obj = {id: 3, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '284'}
//This object should not be possible to add to the array

Before posting this object into the array I want to check if there is already a user with the combination of firstName lastName birthdate in the array. I already saw some javascript methods like array.some() but as far as I know this works with just 1 value. Is there any method to check multiple values?

CodePudding user response:

You can use && operator to multiple values with some method.

var users = [
 {id: 1, firstName: 'Max', lastname: 'Muster', birthdate: '10.10.1990', number: '123'},
  {id: 2, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '254'}
];

function addUser (user) {
  const isExist = users.some(u => u.firstName === user.firstName && u.lastName === user.lastName && u.birthDate === user.birthDate)
  !isExist && users.push(user)
}

CodePudding user response:

You can use a lambda function as a parameter with array.some() in order to create custom validation like checking multiple values. As a working example with the code you added to the question:

const users = [
  {id: 1, firstName: 'Max', lastname: 'Muster', birthdate: '10.10.1990', number: '123'},
  {id: 2, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '254'}
];

const userToAdd = {id: 3, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '284'};

// Use some with a custom validation an saves the boolean result in "userAlreadyExists"
const userAlreadyExists = users.some(u => 
    u.firstName == userToAdd.firstName &&
    u.lastname == userToAdd.lastname &&
    u.birthdate == userToAdd.birthdate
);

// logs "true", as expected :)
console.log(userAlreadyExists);

CodePudding user response:

Another approach to check the existence of an object in the array

const users = [
  {id: 1, firstName: 'Max', lastname: 'Muster', birthdate: '10.10.1990', number: '123'},
  {id: 2, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '254'}
];

const userToAdd = {id: 3, firstName: 'Mia', lastname: 'Gruber', birthdate: '11.03.2001', number: '284'};

const isUserinStore = (users, userToAdd) => {
  const getHash = ({ firstName, lastname, birthdate }) => 
    [firstName, lastname, birthdate].join('|||');
    
  return users.some((user) => getHash(user) === getHash(userToAdd));
};

console.log(isUserinStore(users, userToAdd));
.as-console-wrapper { max-height: 100% !important; top: 0 }

CodePudding user response:

One method of comparing objects in JavaScript is to compare their strings. Use JSON.stringify() to convert the objects into strings and just compare them after.

Sample code:

function checkIfPresent() {
var users = [
{
  id: 1,
  firstName: "Max",
  lastname: "Muster",
  birthdate: "10.10.1990",
  number: "123",
},
{
  id: 2,
  firstName: "Mia",
  lastname: "Gruber",
  birthdate: "11.03.2001",
  number: "254",
 },
];

var obj = {
 id: 1,
 firstName: "Max",
 lastname: "Muster",
 birthdate: "10.10.1990",
 number: "123",
};

for (var i = 0; i < users.length;   i) {
 if (JSON.stringify(users[i]) === JSON.stringify(obj)) {
  return true;
 }
}

return false;
}

console.log(checkIfPresent());
  • Related