【发布时间】:2019-07-02 07:43:42
【问题描述】:
我在创建包含使用现有属性值的字符串的新属性时遇到问题。你能告诉我为什么我不能让它工作吗?
function greetDevelopers(list) {
for(let i=0 ; i< list.length ; i++ ){
return 'Hi '+list[i].firstName +', what do you like the most about
'+list[i].language +'?'
}
}
当我编写这样的代码向每个对象输入新属性时,它会一直显示第一个对象结果。
但是,如果我尝试一下
function greetDevelopers(list) {
for(let i=0 ; i< list.length ; i++ ){
return 'Hi '+list[1].firstName +', what do you like the most about
'+list[1].language +'?'
}
}
它在对象中显示正确的值。
您的任务是返回一个数组,其中每个对象都有一个新属性“greeting”,其字符串值如下:
你好
function greetDevelopers(list) {
for(let i=0 ; i< list.length ; i++ ){
return 'Hi '+list[i].firstName +', what do you like the most about
'+list[i].language +'?'
}
}
var list1 = [
{ firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent:
'Americas', age: 35, language: 'Java' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent:
'Europe', age: 35, language: 'Python' },
{ firstName: 'Madison', lastName: 'U.', country: 'United States',
continent: 'Americas', age: 32, language: 'Ruby' }
];
Test.assertDeepEquals(greetDevelopers(list1), answer);
[
{ firstName: 'Sofia', lastName: 'I.', country: 'Argentina', continent:
'Americas', age: 35, language: 'Java',
greeting: 'Hi Sofia, what do you like the most about Java?'
},
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent:
'Europe', age: 35, language: 'Python',
greeting: 'Hi Lukas, what do you like the most about Python?'
},
{ firstName: 'Madison', lastName: 'U.', country: 'United States',
continent: 'Americas', age: 32, language: 'Ruby',
greeting: 'Hi Madison, what do you like the most about Ruby?'
}
];
我想变成这样。但我的代码不起作用。你能帮忙吗? 谢谢!
【问题讨论】:
-
为什么你开始一个
for循环,但在第一次迭代中返回?
标签: javascript arrays object javascript-objects