简体   繁体   English

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

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

You will be given an array of objects (associative arrays in PHP) representing data about developers who have signed up to attend the next coding meetup that you are organising.您将获得一组对象(PHP 中的关联数组),这些对象表示已注册参加您正在组织的下一次编码聚会的开发人员的数据。

Your task is to return an array where each object will have a new property 'greeting' with the following string value:你的任务是返回一个数组,其中每个对象都有一个新的属性“greeting”,字符串值如下:

Hi < firstName here >, what do you like the most about < language here >?嗨<这里的名字>,你最喜欢<这里的语言>的哪一点?

For example, given the following input array:例如,给定以下输入数组:

 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' } ]

your function should return the following array:您的函数应返回以下数组:

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

So far I have this:到目前为止,我有这个:


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

}

This returns the greeting, but returns the last person in the array's name and language, rather than each individual separately.这将返回问候语,但返回数组名称和语言中的最后一个人,而不是分别返回每个人。

As @TKoL says, you are wrongly mixing for and forEach.正如@TKoL 所说,您错误地混合了 for 和 forEach。 You just need to:你只需要:

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

or或者

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

Welcome to Stack Overflow.欢迎使用堆栈溢出。

You should use a .map() operator instead of a forEach as it can return a new array, like this:您应该使用.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);

Here is the full code:这是完整的代码:

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

You can simply use for of loop.您可以简单地使用for of循环。

Here is the working example.这是工作示例。

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

Make a function to add a property to an object:创建一个函数来向对象添加属性:

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

And map your array elements to modified ones:并将您的数组元素映射到修改后的元素:

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

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

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