简体   繁体   English

显示循环遍历对象数组的键值对

[英]Display key value pairs from looping through an array of objects

I'm learning react and I've got a problem displaying the key value pairs in JSX.我正在学习 react,但在 JSX 中显示键值对时遇到问题。 An array of objects is shown below.下面显示了一组对象。

const heroSection = [
  {
    "Heading color": "#1F1235",
    "Text color": "#1B1425",
    "Button color": "#FF6E6C",
    "Background color": "#F4EFFC",
    "Accent text color": "#E2D9F1"
  },
  {
    "Heading color": "#1F1235",
    "Text color": "#1B1425",
    "Button color": "#FF6E6C",
    "Background color": "#F4EFFC",
    "Accent text color": "#E2D9F1"
  }
];

I'm trying to display key value pairs in JSX.我正在尝试在 JSX 中显示键值对。 My looping method.我的循环方法。

HeroSectionColors.map(color => {
  Object.keys(color).forEach(colorItem => {
    console.log(colorItem, color[colorItem]);
  });
});

In the console the key value pair is shown as I want to.在控制台中,键值对显示为我想要的。 I want to display the key value pairs in the JSX.我想在 JSX 中显示键值对。

{HeroSectionColors.map(color => {
  Object.keys(color).forEach(colorItem => (
   <button className="button-hues">
    <span className="hues-display"></span>
    <div className="hues-info">
    <span className="section-color-name">{colorItem}</span>
    <span className="color-hex">{color[colorItem]}</span>
   </div>
  </button>
 ));
})}

The above code does not work and renders nothing in the browser.上面的代码不起作用并且在浏览器中什么也不呈现。 Any help is appreciated.任何帮助表示赞赏。 PS Apologies for any formatting errors. PS 对任何格式错误表示歉意。

Use map for object keys iteration instead of forEach .使用map进行对象键迭代而不是forEach Also you can use Object.entries with array destructuring for easier access to the properties:您也可以将Object.entries与数组解构一起使用,以便更轻松地访问属性:

{
  HeroSectionColors.map(color => {
    return Object.entries(color).map(([colorName, color]) => (
      <button className="button-hues">
        <span className="hues-display"></span>
        <div className="hues-info">
          <span className="section-color-name">{colorName}</span>
          <span className="color-hex">{color}</span>
        </div>
      </button>
    ));
  });
}

You need to return that Object.keys() call.您需要返回 Object.keys() 调用。 Or, as that's all you're doing, you could just do it like this:或者,因为这就是你所做的一切,你可以这样做:

{HeroSectionColors.map(color => 
  Object.keys(color).forEach(colorItem => (
   <button className="button-hues">
    <span className="hues-display"></span>
    <div className="hues-info">
    <span className="section-color-name">{colorItem}</span>
    <span className="color-hex">{color[colorItem]}</span>
   </div>
  </button>
 ));
)}

Note the removal of the {} from the top map请注意从顶部地图中移除了{}

Assuming that your bracketed code block is appearing within a return value of a React component, you'll need to have the code block return the <button> JSX that would have gone here if it was hard-coded.假设括号中的代码块出现在 React 组件的返回值中,您需要让代码块返回<button> JSX,如果它是硬编码的,它就会出现在这里。 In that case, forEach is the wrong iterator to use, as it doesn't return a mapped value.在这种情况下, forEach是错误的迭代器,因为它不返回映射值。 Using map instead should suffice:改用map就足够了:

{heroSection.map(colorObject => {
          const colorPairs = Object.entries(colorObject)
          return colorPairs.map(([key, value]) => {
            return (
              <button className="button-hues">
                <span className="hues-display"></span>
                <div className="hues-info">
                  <span className="section-color-name">{key}</span>
                  <span className="color-hex">{value}</span>
                </div>
              </button>
            );
          });
        })}

Use map instead of forEach .使用map而不是forEach As forEach doesn't return anything.至于forEach不返回任何东西。 Also you need to write return explicitly in your outer map method.此外,您还需要在外部 map 方法中明确写入return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM