2021年4月1日星期四

Adding values to existing key in object Javascript

How do I add more names to an age group in object with simple javascript? Not using any built in function like reduce(), append(), assign() etc. Please let me know my mistakes and what I am missing. Any help is appreciated :)

My output is { '10': [ 'Victoria' ], '12': [ 'Samantha' ] } My second 'if' statement never executed

Expected Output:

{  "12": ["Kathy", "Krystal", "Samantha"],  "10": ["Victoria"]  }  
const ageGroup = function(girls) {        let people = {};        for(let i = 0; i < girls.length; i++){      if(girls[i].age !== people.age){        people[girls[i].age] = [girls[i].name];        }      if(girls[i].age === people.age){        people.push([girls[i].name]);      }        }    return people;    };    console.log(ageGroup([    {name: "Kathy", age: 12},    {name: "Victoria", age: 10},    {name: "Krystal", age: 12},    {name: "Samantha", age: 12}  ]));    
https://stackoverflow.com/questions/66913497/adding-values-to-existing-key-in-object-javascript April 02, 2021 at 09:43AM

没有评论:

发表评论