[英]how to loop through values of a nested object in javascript in es6
I am trying to use a condition if all values of an object is not equal to empty or null.如果对象的所有值不等于空或空,我正在尝试使用条件。 I am trying to retrieve the values in es6.我正在尝试检索 es6 中的值。 How can I achieve this?我怎样才能做到这一点?
This is the object这是对象
let obj = painterHomeWizardData: {
hero: {
image_en: 'image1',
image_ar: 'image2',
heading_en: 'heading1',
heading_ar: 'heading2',
description_en: 'description1',
description_ar: 'description2'
},
become_painter: {
image_en: 'image1',
image_ar: 'image2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2',
sections: [
{
image_en: 'image1',
image_ar: 'image2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
testimonials: [
{
name_en: 'name5',
name_ar: 'name2',
testimonial_en: 'testimonial1',
testimonial_ar: 'testimonial2'
}
],
painter_requirements: {
title_en: 'title1',
title_ar: 'title2',
sub_title_en: 'Subtitle1',
sub_title_ar: 'SubTitle2',
sections: [
{
image_en: 'image1',
image_ar: 'image2',
attribute_en: 'Attribute1',
attribute_ar: 'Attribute2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
get_started: {
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2',
sections: [
{
attribute_id: 'attribute1',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
name: 'NAME'
}
I tried this method Used Object.keys and map Please explain how to achieve this我试过这个方法使用 Object.keys 和 map请解释如何实现这个
const z = Object.keys(this.painterHomeWizardData).map(item => {
return this.painterHomeWizardData[item]
})
console.log('z', z)
const y = z.map(a => {
return Object.keys(a).map((item, i) => [item])
})
console.log('y', y)
How can i achieve this?我怎样才能做到这一点? how to loop through values of a nested object in javascript in es6 Thank you如何在es6中的javascript中循环遍历嵌套对象的值谢谢
let obj ={ painterHomeWizardData: {
hero: {
image_en: 'image1',
image_ar: 'image2',
heading_en: 'heading1',
heading_ar: 'heading2',
description_en: 'description1',
description_ar: 'description2'
},
become_painter: {
image_en: 'image1',
image_ar: 'image2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2',
sections: [
{
image_en: 'image1',
image_ar: 'image2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
testimonials: [
{
name_en: 'name5',
name_ar: 'name2',
testimonial_en: 'testimonial1',
testimonial_ar: 'testimonial2'
}
],
painter_requirements: {
title_en: 'title1',
title_ar: 'title2',
sub_title_en: 'Subtitle1',
sub_title_ar: 'SubTitle2',
sections: [
{
image_en: 'image1',
image_ar: 'image2',
attribute_en: 'Attribute1',
attribute_ar: 'Attribute2',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
get_started: {
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2',
sections: [
{
attribute_id: 'attribute1',
title_en: 'title1',
title_ar: 'title2',
description_en: 'description1',
description_ar: 'description2'
}
]
},
name: 'NAME'
}
}
var tempKeys = [];
var emptyField = false;
for (let [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
tempKeys.push(key);
//console.log(typeof value);
if(typeof value == 'object'){
for (let [key, value1] of Object.entries(value)) {
//console.log(typeof value1);
tempKeys.push(key);
console.log(`${key}: ${value1}`);
}
}
else{
console.log(`${key}: ${value}`);
}
}
console.log("temp keys ===>" , tempKeys);
tempKeys.forEach(function(vals , index){
//console.log(vals);
//console.log(obj[tempKeys[0]][vals]);
if(index == 0){
return false;
}
else{
for (let [key, value1] of Object.entries(obj[tempKeys[0]][vals])) {
if(typeof value1 == 'object'){
if(obj[tempKeys[0]][vals][0] != undefined){
for (let [key, value1] of Object.entries(obj[tempKeys[0]][vals][0])) {
console.log(`${key}: ${value1}`);
if(value1.length < 1){
emptyField = true;
}
}
//console.log("typeof value1 == 'object' ====>" , obj[tempKeys[0]][vals][0]);
}
}else{
if(value1.length < 1){
emptyField = true;
}
//console.log(`${key}: ${value1}`);
}
}
}
});
if(emptyField == false){
console.log("no empty field occur");
}
else{
console.log("empty field occur");
}
Through this you can check for non empty field通过这个你可以检查非空字段
The object you provide is also not value , refer the object provided in my answer您提供的对象也不是价值,请参考我的回答中提供的对象
使用Object.values
获取值的数组,然后使用Object.values
遍历那些Object.values
:
Object.values(this.painterHomeWizard).map(Object.values).forEach(value => {...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.