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

470
Views
loop through nested object and change property
const mydata = { 
    message: 'Hello Vues!',
    language: {
        en: {show: false, displayname: "English"},
        ja: {show: true, displayname: "Japanese"},
        zh: {show: false, displayname: "Chinese"},
    }
 }

I would like to loop through the 'langauge' object, target it's children, and change all their property "show" into false.

The goal is as follow:

const mydata = { 
    message: 'Hello Vues!',
    language: {
        en: {show: false, displayname: "English"},
        ja: {show: false, displayname: "Japanese"},
        zh: {show: false, displayname: "Chinese"},
    }
 }

I am looking at Object.entries and I can sort of extract the 3 keys (which is en, ja and zh).

for (const [key, value] of Object.entries(mydata.language)) {
        console.log(key); //----> gets names of keys
        console.log(key.displayname);//----> undefined
  }

However I cant seemed to reference them, loop through it's children and change all 3 records of "show" into false.

Am I looking at the wrong command here?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can just loop over the value of mydata.language with the help of forEach and make show as false as:

Object.values(mydata.language).forEach((o) => o.show = false);

const mydata = {
  message: 'Hello Vues!',
  language: {
    en: { show: false, displayname: 'English' },
    ja: { show: true, displayname: 'Japanese' },
    zh: { show: false, displayname: 'Chinese' },
  },
};

Object.values(mydata.language).forEach((o) => o.show = false);
console.log(mydata);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

you can use

mydata.language[key].show = false;

Or

value.show = false;
about 4 years ago · Juan Pablo Isaza Report

0

You can achieve this via the Object.keys method which will return an array of a given object's own enumerable property names, followed by the forEach method which will execute a provided function once for each array element. In that function you will set the show value to false.

const mydata = { 
    message: 'Hello Vues!',
    language: {
        en: {show: false, displayname: "English"},
        ja: {show: true, displayname: "Japanese"},
        zh: {show: false, displayname: "Chinese"},
    }
 }
 
 Object.keys(mydata.language).forEach(x => mydata.language[x].show = false);
 
 console.log(mydata);

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!