Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Arrow Function (ES6) [closed]

Not sure how to exactly use this .filter() with an arrow function.

Instructions:

Use the built in filter method to filter over the jobs array of objects and return the object of the person with a job as a programmer. Make sure to use the arrow function in conjunction with the filter method.

Tried Solution:

var jobs = [{receptionist: "James"}, 
            {programmer: "Steve"},
            {designer: "Alicia"}];

var solution = jobs.filter(person => person === "programmer");
like image 930
PBandJ333 Avatar asked Dec 20 '25 17:12

PBandJ333


1 Answers

You can do it by many ways e.g by using in, using includes() and so on. But If I were you I'll try this way using hasOwnProperty

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

var jobs = [{
    receptionist: "James"
  },
  {
    programmer: "Steve"
  },
  {
    designer: "Alicia"
  }
];

var solution = jobs.filter(obj => obj.hasOwnProperty('programmer'));
console.log(solution)

With in:

var jobs = [{receptionist: "James"}, 
            {programmer: "Steve"},
            {designer: "Alicia"}];

var solution = jobs.filter(obj => 'programmer' in obj);
console.log(solution)

With includes:

var jobs = [{receptionist: "James"}, 
            {programmer: "Steve"},
            {designer: "Alicia"}];

var solution = jobs.filter(obj => Object.keys(obj).includes('programmer'));
console.log(solution)
like image 134
Always Sunny Avatar answered Dec 23 '25 06:12

Always Sunny