如何迭代一个对象内的对象并打印键,然后再迭代值(该值是一个数组)?

huangapple go评论60阅读模式
英文:

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 &#39;react&#39;;
const Accordion = ({ title, data }) =&gt; {
const [isExpanded, setExpanded] = useState(false);
const onClickHeading = () =&gt; setExpanded(!isExpanded);
return (
&lt;div&gt;
&lt;h6 onClick={onClickHeading}&gt;{title}&lt;/h6&gt;
{isExpanded &amp;&amp; (
&lt;div&gt;
&lt;ul&gt;
{data.map((item, index) =&gt; (
&lt;li key={&#39;accordion-body-&#39; + index}&gt;
{item.attribute_code} - {item.label} - {item.value}
&lt;/li&gt;
))}
&lt;/ul&gt;
&lt;/div&gt;
)}
&lt;/div&gt;
);
};
const App = () =&gt; {
const accordionData = {
&#39;Product Details&#39;: [
{ attribute_code: &#39;type2&#39;, label: &#39;Variant / Model&#39;, value: &#39;100 4jet Vario&#39; },
],
&#39;_General&#39;: [
{ attribute_code: &#39;designer&#39;, label: &#39;Design by&#39;, value: &#39;Hansgrohe&#39; },
{ attribute_code: &#39;delivery_time&#39;, label: &#39;Delivery Time&#39;, value: &#39;on stock&#39; },
],
&#39;_Characteristics&#39;: [
{ attribute_code: &#39;shape_outside&#39;, label: &#39;Shape&#39;, value: &#39;round&#39; },
{
attribute_code: &#39;water_heater&#39;,
label: &#39;Water heater&#39;,
value: &#39;suitable for instantaneous water heaters&#39;,
},
],
&#39;_Color-Material-Surface&#39;: [
{ attribute_code: &#39;material&#39;, label: &#39;Material&#39;, value: &#39;metal / plastic&#39; },
],
};
return (
&lt;div&gt;
{Object.entries(accordionData).map(([title, data], index) =&gt; (
&lt;Accordion key={&#39;accordion-&#39; + index} title={title} data={data} /&gt;
))}
&lt;/div&gt;
);
};
export default App;

huangapple
  • 本文由 发表于 2023年7月6日 13:47:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定