如何在Reactjs中引用嵌套数组元素?

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

How to refer to a nested array element in Reactjs?

问题

我有一个配置文件,在我的app.js中引用它。我需要使用数组中嵌套元素的键。以下是数组结构。我需要引用contact元素下的label。

export const detailConfig = [
  {
    name: "Pr1",
    shortDescription: "Pr1 is a health related product",
    longDescription: "Pr1 is a health related product",
    contacts: [
      { label: "a", link: "1" },
      { label: "b", link: "1" }
    ]
  },

  {
    name: "pr2",
    shortDescription: "Pr2 is a mobile related product",
    longDescription: "Pr2 is a mobile related product",
    contacts: [
      { label: "c", type: "1" },
      { label: "d", type: "1" }
    ]
  }
];

React代码

```javascript
import "./styles.css";

import { detailConfig } from "./config";

export default function App() {
  return (
    <div className="App">
      {detailConfig.map((detail) => (
        <div key={detail.name}>
          <h1>{detail.name}</h1>
          {detail.contacts.map((contact) => (
            <p key={contact.label}>{contact.label}</p>
          ))}
        </div>
      ))}
    </div>
  );
}

代码和演示:
https://codesandbox.io/s/objective-wright-ekktrg?file=/src/App.js

英文:

I have a config file which i am refering to in my app.js. I need to use key from a nested element from the array. Here is the array struct. I need to refer to label under contact element.

export const detailConfig = [
  {
    name: &quot;Pr1&quot;,
    shortDescription: &quot;Pr1 is a health related product&quot;,
    longDescription: &quot;Pr1 is a health related product&quot;,
    contacts: [
      { label : &quot;a&quot;, link :&quot;1&quot; },
      { label : &quot;b&quot;, link: &quot;1&quot; }
    ]
  },

  {
    name: &quot;pr2&quot;,
    shortDescription: &quot;Pr2 is a mobile related product&quot;,
    longDescription: &quot;Pr2 is a mobile related product&quot;,
    contacts: [
      { label : &quot;c&quot;, type :&quot;1&quot; },
      { label : &quot;d&quot;, type: &quot;1&quot; }
    ]
  }
];

React code:

import &quot;./styles.css&quot;;

import {detailConfig} from &quot;./config&quot;

export default function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      {detailConfig.map(detailConfig=&gt;
        &lt;div&gt;
      &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
      &lt;p&gt;{detailConfig.contacts.label}&lt;/p&gt;
      &lt;/div&gt;

      )}
    &lt;/div&gt;
  );
}

code and demo:
https://codesandbox.io/s/objective-wright-ekktrg?file=/src/App.js

答案1

得分: 2

你的 contacts 属性本身就是一个数组。因此,你可以选择以下方式之一:

  1. 遍历/映射它并按你的喜好呈现其项(https://codesandbox.io/s/peaceful-frost-mnv2fw)
  2. 从中选择你想要的一个项(例如,仅选择第一个联系人 - contacts[0])并使用它。
英文:

Your contacts property is an array itself. So, you either need to:

  1. Loop/map over it and render its items however you'd like to (https://codesandbox.io/s/peaceful-frost-mnv2fw)
  2. Choose the one item that you want from it (e.g. the first contact only - contacts[0]) and use it instead.

答案2

得分: 0

Your contacts property is just an array. To access a value of a nested array, you could generally do

// access the first products first contact label
const someLabel = detailsConfig[0].contacts[0].label;

However you are already mapping the initial array, which loops over every single entry, so you do not need to specify which element you are referring since it is handled by the .map(...) function.

// returns the label of the first contact for each product if a contact exists
const labels = detailsConfig.map(product =&gt; {
  // no need to specify what exactly product is
  return product.contacts[0].label;
});

This would logically be equal to

const labels = [];
for(let i = 0; i &lt; detailsConfig.length; i++) {
  // the first line is handled in the `.map(...)` function
  const product = detailsConfig[i];
  labels[i] = product.contacts[0].length;
}

In your use case, there are many things which could make sense

// list each individual contact in its separate &lt;p&gt;&lt;/p&gt;
// this would make most sense to me since it gives you the power to easily
// adapt the view in case more information would be needed
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    &lt;p&gt;
      {
        detailConfig.contacts.map(contact =&gt; (
          &lt;p&gt;{contact.label}&lt;/p&gt;
        ))
      }
    &lt;/p&gt;
  &lt;/div&gt;
))

// show label of first contact if available
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    {/* added null check in case no contact was found */}
    &lt;p&gt;{detailConfig.contacts[0]?.label ?? &#39;No Contact found&#39;}&lt;/p&gt;
  &lt;/div&gt;
))

// join the label of all contacts together, separated by `/`
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    &lt;p&gt;{detailConfig.contacts.map(contact =&gt; contact.label).join(&#39; / &#39;)}&lt;/p&gt;
  &lt;/div&gt;
))
英文:

Your contacts property is just an array. To access a value of a nested array, you could generally do

// access the first products first contact label
const someLabel = detailsConfig[0].contacts[0].label;

However you are already mapping the initial array, which loops over every single entry, so you do not need to specify which element you are referring since it is handled by the .map(...)

// returns the label of the first contact for each product if a contact exists
const labels = detailsConfig.map(product =&gt; {
  // no need to specify what exactly product is
  return product.contacts[0].label;
});

This would logically be equal to

const labels = [];
for(let i = 0; i &lt; detailsConfig.length; i++) {
  // the first line is handled in the `.map(...)` function
  const product = detailsConfig[i];
  labels[i] = product.contacts[0].length;
}

In your use case, there are many things which could make sense

// list each individual contact in its seperate &lt;p&gt;&lt;/p&gt;
// this would make most sense to me since it gives you the power to easily
// adapt the view in case more information would be needed
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    &lt;p&gt;
      {
        detailConfig.contacts.map(contact =&gt; (
          &lt;p&gt;{contact.label}&lt;/p&gt;
        ))
      }
    &lt;/p&gt;
  &lt;/div&gt;
))

// show label of first contact if available
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    {/* added null check in case no contact was found */}
    &lt;p&gt;{detailConfig.contacts[0]?.label ?? &#39;No Contact found&#39;}&lt;/p&gt;
  &lt;/div&gt;
))

// join the label of all contacts together, seperated by `/`
detailConfig.map(detailConfig=&gt; (
  &lt;div&gt;
    &lt;h1&gt;{detailConfig.name}&lt;/h1&gt;
    &lt;p&gt;{detailConfig.contacts.map(contact =&gt; contact.label).join(&#39; / &#39;)}&lt;/p&gt;
  &lt;/div&gt;
))

huangapple
  • 本文由 发表于 2023年2月10日 16:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408345.html
匿名

发表评论

匿名网友

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

确定