英文:
How to iterate an object of objects and print key and again iterate the value which is an array?
问题
{
"Product Details": [
{
"attribute_code": "type2",
"label": "Variant / Model",
"value": "100 4jet Vario"
}
],
"_General": [
{
"attribute_code": "designer",
"label": "Design by",
"value": "Hansgrohe"
},
{
"attribute_code": "delivery_time",
"label": "Delivery Time",
"value": "on stock"
}
],
"_Characteristics": [
{
"attribute_code": "shape_outside",
"label": "Shape",
"value": "round"
},
{
"attribute_code": "water_heater",
"label": "Water heater",
"value": "suitable for instantaneous water heaters"
}
],
"_Color-Material-Surface": [
{
"attribute_code": "material",
"label": "Material",
"value": "metal / plastic"
}
]
}
英文:
I have an object which I need to iterate and print the keys and again iterate the values which is an array. Below is the sample object.
{
"Product Details": [
{
"attribute_code": "type2",
"label": "Variant / Model",
"value": "100 4jet Vario"
}
],
"_General": [
{
"attribute_code": "designer",
"label": "Design by",
"value": "Hansgrohe"
},
{
"attribute_code": "delivery_time",
"label": "Delivery Time",
"value": "on stock"
}
],
"_Characteristics": [
{
"attribute_code": "shape_outside",
"label": "Shape",
"value": "round"
},
{
"attribute_code": "water_heater",
"label": "Water heater",
"value": "suitable for instantaneous water heaters"
}
],
"_Color-Material-Surface": [
{
"attribute_code": "material",
"label": "Material",
"value": "metal / plastic"
}
]
}
I am displaying the values in an accordion where the object key eg. "Product details" is accordion heading and inside the accordion the content is the value of the array which I need to map and display. Also these keys "Product details", "_General" are dynamic and will not be the same. Below is a screenshot how I want it to be displayed.
答案1
得分: 2
你可以使用 Object.entries() 来迭代对象,它会返回 键-值 对的数组。根据你的 JSON 数据,你可以创建如下组件:
import React, { useState } from 'react';
const Accordion = ({ title, data }) => {
const [isExpanded, setExpanded] = useState(false);
const onClickHeading = () => setExpanded(!isExpanded);
return (
<div>
<h6 onClick={onClickHeading}>{title}</h6>
{isExpanded && (
<div>
<ul>
{data.map((item, index) => (
<li key={'accordion-body-' + index}>
{item.attribute_code} - {item.label} - {item.value}
</li>
))}
</ul>
</div>
)}
</div>
);
};
const App = () => {
const accordionData = {
'Product Details': [
{ attribute_code: 'type2', label: 'Variant / Model', value: '100 4jet Vario' },
],
'_General': [
{ attribute_code: 'designer', label: 'Design by', value: 'Hansgrohe' },
{ attribute_code: 'delivery_time', label: 'Delivery Time', value: 'on stock' },
],
'_Characteristics': [
{ attribute_code: 'shape_outside', label: 'Shape', value: 'round' },
{
attribute_code: 'water_heater',
label: 'Water heater',
value: 'suitable for instantaneous water heaters',
},
],
'_Color-Material-Surface': [
{ attribute_code: 'material', label: 'Material', value: 'metal / plastic' },
],
};
return (
<div>
{Object.entries(accordionData).map(([title, data], index) => (
<Accordion key={'accordion-' + index} title={title} data={data} />
))}
</div>
);
};
export default App;
英文:
You can use Object.entries() to iterate the object, it returns array of key-value pairs. As per your json data you can create component like this:
import React, { useState } from 'react';
const Accordion = ({ title, data }) => {
const [isExpanded, setExpanded] = useState(false);
const onClickHeading = () => setExpanded(!isExpanded);
return (
<div>
<h6 onClick={onClickHeading}>{title}</h6>
{isExpanded && (
<div>
<ul>
{data.map((item, index) => (
<li key={'accordion-body-' + index}>
{item.attribute_code} - {item.label} - {item.value}
</li>
))}
</ul>
</div>
)}
</div>
);
};
const App = () => {
const accordionData = {
'Product Details': [
{ attribute_code: 'type2', label: 'Variant / Model', value: '100 4jet Vario' },
],
'_General': [
{ attribute_code: 'designer', label: 'Design by', value: 'Hansgrohe' },
{ attribute_code: 'delivery_time', label: 'Delivery Time', value: 'on stock' },
],
'_Characteristics': [
{ attribute_code: 'shape_outside', label: 'Shape', value: 'round' },
{
attribute_code: 'water_heater',
label: 'Water heater',
value: 'suitable for instantaneous water heaters',
},
],
'_Color-Material-Surface': [
{ attribute_code: 'material', label: 'Material', value: 'metal / plastic' },
],
};
return (
<div>
{Object.entries(accordionData).map(([title, data], index) => (
<Accordion key={'accordion-' + index} title={title} data={data} />
))}
</div>
);
};
export default App;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论