1

using angular, i have an array

list: Employee[] = [
    {id: 1, name: "conrad", email: "conrad@test.com", password: "123456", department: "Admin"},
    {id: 2, name: "two", email: "2@test.com", password: "123456", department: "Finance"},
    {id: 3, name: "three", email: "3@test.com", password: "123456", department: "Finance"},
    {id: 4, name: "four", email: "4@test.com", password: "123456", department: "Marketing"},
    {id: 5, name: "five", email: "5@test.com", password: "123456", department: "Marketing"},
    {id: 6, name: "six", email: "6@test.com", password: "123456", department: "Marketing"},
    {id: 7, name: "seven", email: "7@test.com", password: "123456", department: "Service"},
    {id: 8, name: "eight", email: "8@test.com", password: "123456", department: "Service"},
    {id: 9, name: "nine", email: "9@test.com", password: "123456", department: "Service"}
  ];

then i have an object

data = {name: "conrad", email: "conrad@test.com", password: "123456", department: "Admin", id: 1}

how do i find out if data is present in list i have tried following gives answer as undefined

signUp(data) {
    console.log(data);
    console.log(this.list.find(l => l === data));
  }

also tried some, includes.

wuarmin
  • 3,274
  • 3
  • 18
  • 31
conrad
  • 51
  • 8

1 Answers1

0

Just use Array.prototype.some() to check if your list contains a specific employee-Object

list = [
    {id: 1, name: "conrad", email: "conrad@test.com", password: "123456", department: "Admin"},
    {id: 2, name: "two", email: "2@test.com", password: "123456", department: "Finance"},
    {id: 3, name: "three", email: "3@test.com", password: "123456", department: "Finance"},
    {id: 4, name: "four", email: "4@test.com", password: "123456", department: "Marketing"},
    {id: 5, name: "five", email: "5@test.com", password: "123456", department: "Marketing"},
    {id: 6, name: "six", email: "6@test.com", password: "123456", department: "Marketing"},
    {id: 7, name: "seven", email: "7@test.com", password: "123456", department: "Service"},
    {id: 8, name: "eight", email: "8@test.com", password: "123456", department: "Service"},
    {id: 9, name: "nine", email: "9@test.com", password: "123456", department: "Service"}
  ];
  
  function employeeIsInList(employee, list) {
    return list.some((listEmployee) => {
      return employee.id === listEmployee.id;
    });
  }
  
  console.log(
    'employee with id 3 is in the list, therefore result is',
    employeeIsInList({id: 3, name: "three"}, list)
  ); //true
  
  console.log(
    'employee with id 100 is not in the list, therefore result is',
    employeeIsInList({id: 100, name: "woswasi"}, list)
  ); //false
wuarmin
  • 3,274
  • 3
  • 18
  • 31