如何从一个属性中返回数组中的对象?

时间:2017-05-24 17:06:31

标签: javascript

我有一个对象数组,我试图弄清楚如何通过名字的属性返回一个对象。

这是测试:

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]);
  });

这就是我尝试过的:

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

它没有通过,似乎是在数组中返回一个对象。任何人都可以解释原因吗?

5 个答案:

答案 0 :(得分:1)

是的,它找不到,因为

this.students.indexOf(name);

name{firstName: "Jalil", scores: [100, 100]}等对象进行比较。

如果要返回所有找到的项目,则需要使用Array.prototype.filter

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

Array.prototype.find如果您只想返回找到的第一个项目:

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

答案 1 :(得分:0)



// 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);




答案 2 :(得分:0)

使用简单的for循环:

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;
};

答案 3 :(得分:0)

您可以使用void __fastcall TForm1::PBoxPaint(TObject *Sender) { TRect R , DrawRect ; unsigned int DrawFlags = 0; TDrawTextParams DrawParams ; PBox->Font->Orientation = 900 ; PBox->Canvas->Rectangle(PBox->ClientRect) ; String S = L"Angualar Text" ; R = PBox->ClientRect ; InflateRect(R,-2,-2) ; DrawRect = R ; DrawFlags = DT_END_ELLIPSIS | DT_NOPREFIX | DT_WORDBREAK | DT_EDITCONTROL | DT_CENTER ; DrawText(PBox->Canvas->Handle, S.c_str() , -1 , &DrawRect, DrawFlags | DT_CALCRECT) ; DrawRect.Right = R.Right; if (DrawRect.Bottom < R.Bottom) { OffsetRect( DrawRect, 0, ( R.Bottom - DrawRect.Bottom ) / 2 ); } else { DrawRect.Bottom = R.Bottom; } DrawParams.iTabLength = 0 ; DrawParams.iLeftMargin = 0 ; DrawParams.iRightMargin = 0 ; DrawParams.uiLengthDrawn = 0 ; DrawParams.cbSize = sizeof(DrawParams) ; DrawTextEx(PBox->Canvas->Handle,S.c_str(),-1,&DrawRect,DrawFlags,&DrawParams) ; } 将学生数组转换为仅包含学生姓名的数组,然后找到匹配的名称。

map

答案 4 :(得分:0)

您可以使用Array.prototype.find(),对象解构

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

&#13;
&#13;
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"));
&#13;
&#13;
&#13;

相关问题