user6495201
user6495201

Reputation:

How to return an object in an array from one of its properties?

I have an array of objects, I'm trying to figure out how to return an object by the property of their name.

This is the test:

describe("Classroom", function() {
  var classroom, jalil, irene, kelvin, myra;

  beforeEach(function() {
    // Define student objects
    jalil  = new Student({firstName: "Jalil", scores: [100, 100]});
    irene  = new Student({firstName: "Irene", scores: [95, 95]});
    kelvin = new Student({firstName: "Kelvin", scores: [94, 94]});
    myra   = new Student({firstName: "Myra", scores: [70, 70]});

    // Assign classroom
    classroom = new Classroom([jalil, irene, kelvin, myra]);
  });

This is what i've tried:

Classroom.prototype.find = function(name) {
  var index = this.students.indexOf(name);
  return this.students.splice(index, 1);
};

It's not passing, seems to be returning an object inside an array. Anyone can explain why?

Upvotes: 0

Views: 50

Answers (5)

guest271314
guest271314

Reputation: 1

You can use Array.prototype.find(), object destructuring

Classroom.prototype.find = name => 
  classroom.find(({firstName}) => firstName === name);
Classroom.find("Myra");

let jalil = {
    firstName: "Jalil",
    scores: [100, 100]
  },
  irene = {
    firstName: "Irene",
    scores: [95, 95]
  },
  kelvin = {
    firstName: "Kelvin",
    scores: [94, 94]
  },
  myra = {
    firstName: "Myra",
    scores: [70, 70]
  };
  
  let classroom = [jalil, irene, kelvin, myra];
  
  let find = name => classroom.find(({firstName}) => firstName === name);
  
  console.log(find("Myra"));

Upvotes: 0

Max Koretskyi
Max Koretskyi

Reputation: 105489

Yes, it's not finding, because

this.students.indexOf(name);

compares name to objects like {firstName: "Jalil", scores: [100, 100]}.

You need to use Array.prototype.filter if you want to return all found items:

Classroom.prototype.find = function(name) {
   return this.students.filter((o)=>{
       return o.firstName === name;
    });
};

Or Array.prototype.find if you want to return only the first found item:

Classroom.prototype.find = function(name) {
   return this.students.find((o)=>{
       return o.firstName === name;
    });
};

Upvotes: 1

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You can use map to convert the students array to an array having only the student name and then find the matching name.

Classroom.prototype.find = function(name) {
  var studentNames = this.students.map(x=>x.firstName);
  var index = studentNames.indexOf(name);
  return this.students.splice(index, 1);
};

Upvotes: 0

Barmar
Barmar

Reputation: 780879

Use a simple for loop:

Classroom.prototype.find = function(name) {
  for (var i = 0; i < this.students.length; i++) {
    if (this.students[i].firteName == name) {
      return this.students[i];
    }
  }
  return null;
};

Upvotes: 0

Ken
Ken

Reputation: 466

// Define student objects
jalil = {
  firstName: "Jalil",
  scores: [100, 100]
};
irene = {
  firstName: "Irene",
  scores: [95, 95]
};
kelvin = {
  firstName: "Kelvin",
  scores: [94, 94]
};
myra = {
  firstName: "Myra",
  scores: [70, 70]
};
// Assign classroom
classroom = [jalil, irene, kelvin, myra];

var filterFunction = function(someArray, theNameYouWant) {
  return someArray.filter(function(person) {
    return person.firstName == theNameYouWant;
  });
}

var filteredArray = filterFunction(classroom, "Jalil");
console.log(filteredArray);

Upvotes: 0

Related Questions