admin管理员组文章数量:1389772
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
}
]));
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
}
]));
Share
Improve this question
edited Apr 2, 2021 at 4:48
Russell
425 bronze badges
asked Apr 2, 2021 at 1:43
ElizawtwElizawtw
31 silver badge2 bronze badges
4 Answers
Reset to default 2I 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}
]));
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.
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}
]));
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}
]));
本文标签: Adding values to existing key in object JavascriptStack Overflow
版权声明:本文标题:Adding values to existing key in object Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744723062a2621809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论