繁体   English   中英

为什么我的代码返回数组的最后一个值而不是为每个项目返回结果?

[英]Why is my code returning the last value of an array instead of returning a result for each item?

您将获得一组对象(PHP 中的关联数组),这些对象表示已注册参加您正在组织的下一次编码聚会的开发人员的数据。

你的任务是返回一个数组,其中每个对象都有一个新的属性“greeting”,字符串值如下:

嗨<这里的名字>,你最喜欢<这里的语言>的哪一点?

例如,给定以下输入数组:

 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: '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?' }];

到目前为止,我有这个:


var count = 0;

  for (var i = 0; i < list.length; i++){

  count +=1;

    list.forEach (function greet(string){

    string.Greeting = "Hi " + list[i].firstName + ",what do you like most about " + list[i].language + "?";

    });  // For each object in array, add string "greeting:" using function

  }

console.log(list);

}

这将返回问候语,但返回数组名称和语言中的最后一个人,而不是分别返回每个人。

正如@TKoL 所说,您错误地混合了 for 和 forEach。 你只需要:

for (var i = 0; i < list.length; i++){
  list[i].greeting = `Hi ${list[i].firstName}, what do you like most about ${list[i].language}?`;
}

或者

list.forEach(l => l.greeting = `Hi ${l.firstName}, what do you like most about ${l.language}?`);

欢迎使用堆栈溢出。

您应该使用.map()运算符而不是forEach因为它可以返回一个新数组,如下所示:

const newList = list.map (function greet(person){
    return {greeting : "Hi " + person.firstName + ",what do you like most about " + person.language + "?", ...person};
});  
console.log(newList);

这是完整的代码:

var count = 0;
var list = [
    {
        firstName: 'Sofia',
        lastName: 'I.',
        country: 'Argentina',
        continent: 'Americas',
        age: 35,
        language: 'Java'
    },

    {
        firstName: 'Lukas',
        lastName: 'X.',
        country: 'Croatia',
        continent: 'Europe',
        age: 35,
        language: 'Python'
    }
];

for (var i = 0; i < list.length; i++) {
    count += 1;

    list[i].Greeting =
        'Hi ' +
        list[i].firstName +
        ',what do you like most about ' +
        list[i].language +
        '?';
}

console.log(list);

您可以简单地使用for of循环。

这是工作示例。

 const 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' } ]; for (let person of list1){ person.greeting = `Hi ${person.firstName}, what do you like the most about ${person.language}?` } console.log(list1);

创建一个函数来向对象添加属性:

function addGreeting(person) {
  return {...person, greeting: `Hi ${person.name}, what do you like most about ${person.language}?`};
}

并将您的数组元素映射到修改后的元素:

people = people.map(person => addGreeting(person));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM