0

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?

1
  • "but as far as I know this works with just 1 value" It works with a function where you can compare anything. Commented Aug 18, 2022 at 15:08

4 Answers 4

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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 }

Comments

-2

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());

1 Comment

JSON.stringify({a:1,b:2}) == JSON.stringify({b:2,a:1}) returns false. And OP wants to compare only 3 properties.

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.