rendering nested arrays in react using map()-ag捕鱼王app官网

current location:home > learning > web front-end > react >

rendering nested arrays in react using map()

author:jiyik last updated:2025/03/10 views:

map()rendering a nested array using :

  1. use map()the method to iterate over the outer array.
  2. on each iteration, the nested array's map()method is called.
  3. renders the elements of a nested array.
export default functionapp() {
  const people = [
    {id: 1, name: 'alice', pets: ['dog', 'cat']},
    {id: 2, name: 'bob', pets: ['turtle', 'rabbit']},
    {id: 3, name: 'carl', pets: ['hamster', 'parrot']},
  ];

  return (
    <div>
      {people.map((person, index) => {
        return (
          <divkey={index}><h2>name: {person.name}h2>
            {person.pets.map((pet, index) => {
              return (
                <divkey={index}><h2>pet: {pet}h2>div>
              );
            })}
            <hr />div>
        );
      })}
    div>
  );
}

the function we pass to array.mapthe method will be called with each element in the array and the index of the current iteration.

in each iteration, we render the name property of the person object and use nested map()to iterate over the pets array for each person.

注意, when calling a method in jsx code map(), we must use curly braces {}to wrap map()the call to .

this is only needed in our jsx code and signals to react that we are writing an expression that must be evaluated.

we map()have used arrow functions with explicit return statements in both calls to the method. if we only need to render some jsx elements and don’t use conditionals, declare variables, etc., we can use implicit returns, which will make our code more readable.

export default functionapp() {
  const people = [
    {id: 1, name: 'alice', pets: ['dog', 'cat']},
    {id: 2, name: 'bob', pets: ['turtle', 'rabbit']},
    {id: 3, name: 'carl', pets: ['hamster', 'parrot']},
  ];

  return (
    <div>
      {people.map((person, index) => (
        <divkey={index}><h2>name: {person.name}h2>
          {person.pets.map((pet, index) => (
            <divkey={index}><h2>pet: {pet}h2>div>
          ))}
          <hr />div>
      ))}
    div>
  );
}

map()we used implicit returns on both arrow functions passed to the method.

const arr = ['a', 'b', 'c'];

// 👇️ explicit return
const result1 = arr.map(element => {
  return element;
});

// 👇️ implicit return
const result2 = arr.map(element => element);

map()we can only use implicit returns when we don't have to use conditionals or define variables in the function passed to .

we used the index of the key prop in the example, but if you have a stable unique identifier, it is better to use it. we can use the id property on each object.

react uses the key prop internally for performance reasons. it helps the library ensure that only array elements that have changed are re-rendered.

that being said, unless we are dealing with thousands of array elements we won't see any noticeable difference.

previous:

next: none

for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.

article url:

related articles

publish date:2025/03/10 views:118 category:react

在 react 中获取表单提交的输入值: 将输入字段的值存储在状态变量中。在表单元素上设置 onsubmit 属性。 在我们的 handlesubmit 函数中访问输入字段的值。

publish date:2025/03/10 views:130 category:react

在 react 中为 body 元素添加一个类: 在 useeffect 或事件处理程序中以 document.body 的形式访问 body 元素。 使用 classlist.add() 方法将类添加到 body 标记。 例如,document.body.classlist.add('my-class'

clearing the value of an input field in react.js

publish date:2025/03/10 views:119 category:react

要在 react 中清除输入字段的值:将输入的值存储在状态变量中。 当某个事件发生时,将状态变量设置为空字符串。对于不受控制的组件,将 ref 的值设置为空字符串,例如 ref.current.va

deleting keys from state object in react

publish date:2025/03/10 views:62 category:react

要从 react 状态对象中删除键: 使用 usestate 挂钩来存储状态对象。 解构对象的键和其余属性。 将状态设置为其余属性。

publish date:2025/03/09 views:170 category:react

要在 react 状态下替换数组中的对象:使用 map() 方法遍历数组。 在每次迭代中,检查是否满足某个条件。 替换满足条件的对象并按原样返回所有其他对象。

how to call a function only once in react

publish date:2025/03/09 views:127 category:react

使用 useeffect 钩子在 react 中只调用一次函数。 当 useeffect 钩子传递一个空的依赖数组时,它仅在组件挂载时运行。 当我们必须在组件安装时获取数据时,这是首选方法。

run react hooks when a component unmounts

publish date:2025/03/09 views:81 category:react

卸载组件时,使用 useeffect 钩子运行react钩子。 我们从 useeffect 钩子返回的函数在组件卸载时被调用,可用于清理目的。

publish date:2025/03/09 views:181 category:react

在 react 中使用带有索引的 map() 方法:在数组上调用 map() 方法。传递给 map() 方法的函数会被元素和索引调用。

setting onclick listener on links in react

publish date:2025/03/09 views:182 category:react

要在 react 中的链接上设置 onclick 监听器:在链接上设置 onclick 属性。每次单击链接时,都会调用我们传递给道具的函数。

scan to read all tech tutorials

social media
  • https://www.github.com/onmpw
  • qq:1244347461

recommended

tags

scan the code
easier access tutorial
网站地图