Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
Looping through a dictionary in javascript with a list within the values

Not sure how to parse through a list of strings within a loop regarding this dictionary.

var student_nicknames = [    
    {name: "William",  nickname: "Bill"},
    {name: "Joseph",   nickname: "Joe"},
    {name: "Maria",    nickname: "Mary"},
    {name: "Richard",  nickname: ["Rick", "Ricky"]},
    {name: "Elizabeth",  nickname: ["Liz", "Lisa", "Beth"]}
];

total_nicknames = function(){
    student_nicknames.forEach(function(student) {
       console.log(student.nickname); 
    });
}

Output

Bill
Joe
Mary
[ 'Rick', 'Ricky' ]
[ 'Liz', 'Lisa', 'Beth' ]

Desired Output

Bill
Joe
Mary
Rick
Ricky
Liz
Lisa
Beth
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

All you need to do is to have an if condition to check if nickname property of each student is an array or not, if it is an array, then you can loop through it and print each item individually, otherwise follow your logic.

var student_nicknames = [
  { name: "William", nickname: "Bill" },
  { name: "Joseph", nickname: "Joe" },
  { name: "Maria", nickname: "Mary" },
  { name: "Richard", nickname: ["Rick", "Ricky"] },
  { name: "Elizabeth", nickname: ["Liz", "Lisa", "Beth"] }
];

const total_nicknames = function () {
  student_nicknames.forEach(function (student) {
    if (Array.isArray(student.nickname)) { // <- HERE
      student.nickname.forEach((e) => console.log(e));
    } else {
      console.log(student.nickname);
    }
  });
};

total_nicknames();
about 4 years ago · Juan Pablo Isaza Report

0

a simple solution based on recursion.

function total_nicknames(student_nicknames) {
    for(let i = 0; i < student_nicknames.length; i++){
      let student = student_nicknames[i];
        if(typeof student === 'object' && student !== null) {
          if(Array.isArray(student.nickname)){
            total_nicknames(student.nickname);
          }else {
            console.log(student.nickname);
          }
        }else {
          console.log(student);
        }
    }
  }


console.log(total_nicknames(student_nicknames));

/* output */
// "Bill"
// "Joe"
// "Mary"
// "Rick"
// "Ricky"
// "Liz"
// "Lisa"
// "Beth"
about 4 years ago · Juan Pablo Isaza Report

0

You can build a small recursive loop, by calling the total_nicknames function again, if student.nickname is an array. You also need to use || (OR) operator, to get the nickname (which would be a string, if it loops over the array) and not undefined (since string object doesn't have any nickname method/property).

var student_nicknames = [{
    name: "William",
    nickname: "Bill"
  },
  {
    name: "Joseph",
    nickname: "Joe"
  },
  {
    name: "Maria",
    nickname: "Mary"
  },
  {
    name: "Richard",
    nickname: ["Rick", "Ricky"]
  },
  {
    name: "Elizabeth",
    nickname: ["Liz", "Lisa", "Beth"]
  }
];

total_nicknames = function(arr) {
  arr.forEach(function(student) {
    if (Array.isArray(student.nickname)) {
      total_nicknames(student.nickname)
    } else
      console.log((student.nickname || student));
  });
}

total_nicknames(student_nicknames);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!