javascript - Javascript 对象数组,具有两个方法和两个调用函数
问题描述
我是 JavaScript 新手。我必须用两个方法和两个调用函数构建一个包含五个对象的数组。与我的一位 Web 开发人员朋友一起,我们创建了以下代码。然而,我不知道如何进一步进步。你们能帮我一把吗?
指示:
创建一个名为 actor 的变量,并为其分配一个由五个对象组成的数组,每个对象代表一个著名的演员。每个演员对象都应具有演员姓名、演员年龄和演员获得的奥斯卡奖数量的属性(您可以将此属性称为 oscars 或 numOscars)。这些是要使用的演员:
Leonardo DiCaprio (age 41, 1 Oscar)
Jennifer Lawrence (age 25, 1 Oscar)
Samuel L. Jackson (age 67, no Oscars)
Meryl Streep (age 66, 3 Oscars)
John Cho (age 43, no Oscars)
除了已经提到的三个属性外,每个actor对象还应该有以下两个方法:
hello - this method logs to the console the string "Hello, my name is "
followed by the actor's name.
hasMoreOscarsThan - this method accepts one actor object as a parameter and
returns true if the actor has more Oscars than the one that is passed as
a parameter and false otherwise.
现在编写以下函数:
getActorByName - this function expects a string as a parameter and returns
the object in the actors array whose name property is equal to the
string that is passed in (if there is one).
getAverageAge - this function returns the average age of all the actors in
the array.
您可以运行以下命令并验证输出。
var leo = getActorByName('Leonardo DiCaprio');
var jlaw = getActorByName('Jennifer Lawrence');
var jcho = getActorByName('John Cho');
var meryl = getActorByName('Meryl Streep');
jlaw.hasMoreOscarsThan(jcho);
jlaw.hasMoreOscarsThan(meryl);
leo.hasMoreOscarsThan(jlaw);
meryl.hello();
leo.hello();
getAverageAge();
var BaseActor = function ( actor_details ) {
this.details = actor_details
this.hello = function () {
console.log(this.details.name)
}
this.hasMoreOscarsThan = function( otherActor ) {
return this.details.oscars > otherActor.details.oscars
}
}
function getActorByName(name) {
console.log(name)
var i;
for (i = 0; i < actors.length; i++) {
console.log(i, actors[i]);
if (actors[i].name == name) {
return actor[i]
}
}
console.log('not found a shit', i)
return false
}
var leo = new BaseActor({ id: 0, name: "Leonardo di Caprio", age: 41, oscars: 1, hello:"Hello, my name is Leonardo di Caprio"})
var jen = new BaseActor({ id: 1, name: "Jennifer Lawrence", age: 25, oscars: 1, hello:"Hello my name is Jennifer Lawrence"})
var sam = new BaseActor({ id: 2, name: "Samuel L. Jackson", age: 67, oscars: 0, hello:"Hello my name is Samuel L. Jackson"})
var meryl = new BaseActor({ id: 3, name: "Meryl Streep", age: 66, oscars: 3, hello:"Hello my name is Meryl Streep"})
var john = new BaseActor({ id: 4, name: "John Cho", age: 43, oscars: 0, hello:"Hello my name is John Cho"})
var actors = [
leo,
jen,
sam,
meryl,
john
]
leo.hello()
console.log(leo.hasMoreOscarsThan(jen))
console.log(
getActorByName("John Cho").id
)
解决方案
您可以将数组减少为演员年龄的总和,然后将其除以数组的长度。
function getAverageAge() {
return actors.reduce(//reduce actors array to sum of actor ages
function(totalAge,actor){
return totalAge + actor.details.age;
},
0//initial value is 0
) / actors.length
}
推荐阅读
- ruby-on-rails - Rails - 拥有完全两种不同设计的最佳实践
- javascript - 为 MongoDB 集合中打开的事务总数创建聚合阶段
- ios - 在 iPad 上同时运行 Homekit Home Hub 和自主单模式应用程序?
- azure-ad-b2c - 取消按钮在 Azure AD B2C 注册上引发错误
- elasticsearch-5 - 如何在 Elasticsearch 中将每月索引重新索引为每周索引?
- javascript - 播放和暂停按钮正在工作...但是如何重置按钮以在音频结束后显示播放符号?
- javascript - 打开内联 OnClick 以单击 Jquery
- opencart - admin-sales-orders > 修改模型文件的订单状态码在哪里?
- javascript - 从命令行使用 JavaScript 从 SVG 创建 PNG
- android - Android 自动完成搜索