0

I have the following data with me.

var Inputdata = {};
Inputdata.firstName = 'Raul'
Inputdata.lastName = 'Peters'

I want to check if the firstName and lastName (together) is already present in the array of objects. Can someone please let me know how to achieve this.

UserData: [0] 
      firstName: 'Raul'
      lastName: 'Peters'
      Id: '4'
      [1]
      firstName: 'Amil'
      lastName: 'Rikia'
      Id: '5'
      [2]
      firstName: 'Riya'
      lastName: 'Pillai'
      Id: '6'
      [3]
      firstName: 'Natasha'
      lastName: 'Shacke'
      Id: '6'
      [4]
      firstName: 'Eric'
      lastName: 'Coles'
      Id: '6'

As you can see, Raul Peters is present in the array of objects in one array. I want the output to be true in this case. If Raul and Peters were in different objects, the answer should be false as Raul and Peters are not in the same object. Can anyone please let me know how to achieve this

3
  • Using a loop and checking their properties Commented Jun 27, 2017 at 16:24
  • 1
    Array.prototype.some Commented Jun 27, 2017 at 16:25
  • Use Array#find. Commented Jun 27, 2017 at 16:29

4 Answers 4

2

Just do an Array.some check:

let isPresent = UserData.some(user => user.firstName == 'Raul' && user.lastName == 'Peters')
Sign up to request clarification or add additional context in comments.

Comments

0

Use Array.find

arr.find( function(e){
   if(e.firstname == 'Raul' && e.lastname == 'Peters')
     return e;
});

Comments

0
const inputData = {};

inputData.firstName = 'Raul';
inputData.lastName = 'Peters';

const userData = [
  {
    firstName: 'Raul',
    lastName: 'Peters',
    Id: '4'
  },
  {
    firstName: 'Amil',
    lastName: 'Rikia',
    Id: '5'
  },
  {
    firstName: 'Riya',
    lastName: 'Pillai',
    Id: '6'
  },
  {
    firstName: 'Natasha',
    lastName: 'Shacke',
    Id: '6'
  },
  {
    firstName: 'Eric',
    lastName: 'Coles',
    Id: '6'
  }
];

This function receives 2 arguments: the userData array of objects and the inputData object. Then, it uses the Array.prototype.some() method to see if there is at least one entry within the userData array that is an object matching the inputData object.

function alreadyExists(userData, inputData) {
  return userData.some(entry => entry['firstName'] === inputData['firstName'] && entry['lastName'] === inputData['lastName']);
};

console.log(alreadyExists(userData, inputData));

Comments

0

If you wanna use underscore or lodash you can use function _.some. Using internal variable you can get needle element.

var needle = false;
var has = _.some(UserData, function(v) {
    return (v.firstName===Inputdata.firstName && v.lastName===Inputdata.lastName) ? needle = v : false;
});
console.log(has)
console.log(needle)

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.