1

using angular, i have an array

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

then i have an object

data = {name: "conrad", email: "[email protected]", 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.

3
  • You can't compare objects using === Commented Aug 4, 2020 at 8:24
  • use loadash _.isEqual(object, other) Commented Aug 4, 2020 at 8:25
  • This explains why it doesn't work, and how to solve the issue: Commented Aug 4, 2020 at 8:26

1 Answer 1

0

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

list = [
    {id: 1, name: "conrad", email: "[email protected]", password: "123456", department: "Admin"},
    {id: 2, name: "two", email: "[email protected]", password: "123456", department: "Finance"},
    {id: 3, name: "three", email: "[email protected]", password: "123456", department: "Finance"},
    {id: 4, name: "four", email: "[email protected]", password: "123456", department: "Marketing"},
    {id: 5, name: "five", email: "[email protected]", password: "123456", department: "Marketing"},
    {id: 6, name: "six", email: "[email protected]", password: "123456", department: "Marketing"},
    {id: 7, name: "seven", email: "[email protected]", password: "123456", department: "Service"},
    {id: 8, name: "eight", email: "[email protected]", password: "123456", department: "Service"},
    {id: 9, name: "nine", email: "[email protected]", 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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.