Reputation: 3
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
}
]));
Upvotes: 0
Views: 5653
Reputation: 802
I edited your code so it works better now!
const ageGroup = function(girls) {
let people = {}; // Create "people" object
for (let girl of girls) { // For each item in "girls",
const age = girl.age; // Get the age of the girl
const name = girl.name; // Get the name of the girl
if (age in people) { // If the current age is already a key in "people",
people[age].push(name); // Push the girl's name to the array.
}
else { // Otherwise (if the current age is not yet a key in "people"),
people[age] = [name]; // Set it to an array with the one name in it.
}
}
return people;
};
console.log(ageGroup([
{name: "Kathy", age: 12},
{name: "Victoria", age: 10},
{name: "Krystal", age: 12},
{name: "Samantha", age: 12}
]));
Upvotes: 2
Reputation: 155
Your first if condition is always meeting, thats why it never moves on to the next one. girls.age
will never be equal to people.age
since you dont have any value stored in people.age
when function runs. So it is not your code, you should find another way to make the control. For adding objects to arrays, simply use .map
and on every item
in the array that you want to add objects, specify the object inside the callback function of the .map
method. Of course you can do it in a for loop as well but since we have the built-in methods it is way more convenient to employ them.
Upvotes: 2
Reputation: 3844
This is my solution:
const ageGroup = function(girls){
let result={};
for (let i=0;i<girls.length;i++){
if (result[girls[i].age]){
result[girls[i].age].push(girls[i].name);
} else {
result[girls[i].age]=[girls[i].name];
}
}
return result;
}
console.log(ageGroup([
{name: "Kathy", age: 12},
{name: "Victoria", age: 10},
{name: "Krystal", age: 12},
{name: "Samantha", age: 12}
]));
Upvotes: 1
Reputation: 16606
Your initial check should be to see if the object key exists. If not, create a new object key with a one-element array. If the key does exist, then just append the name to the existing array.
const ageGroup = function(girls) {
let people = {};
for(let i = 0; i < girls.length; i++){
if(!people[girls[i].age]){
people[girls[i].age] = [girls[i].name];
} else {
people[girls[i].age].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}
]));
Upvotes: 1