簡體   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