how to loop over objects in react
looping over an object in react:
-
use
object.keys()
the method to get an array of object keys. -
use
map()
the method to iterate over the array of keys.
export default functionapp() {
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
return (
<div>
{/* 👇️ iterate object keys */}
{object.keys(employee).map((key, index) => {
return (
<divkey={index}><h2>
{key}: {employee[key]}
h2><hr />div>
);
})}
<br /><br /><br />
{/* 👇️ iterate object values */}
{object.values(employee).map((value, index) => {
return (
<divkey={index}><h2>{value}h2><hr />div>
);
})}
div>
);
}
we use object.keys
the method to get an array of the object's keys.
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
// 👇️ ['id', 'name', 'salary']
console.log(object.keys(employee));
// 👇️ [1, '迹忆客', 123]
console.log(object.values(employee));
we can only call the get method on arrays map()
, so we need to get either the array of object keys or the value of the object.
the function we pass to array.map
the method will be called with each element in the array and the index of the current iteration.
we used an index on the key in our examples
prop
, but if you have a stable, unique identifier, it's better to use that.
when iterating over an object's keys, it is safe to use the object's key for the key prop, since keys within an object are guaranteed to be unique.
export default functionapp() {
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
return (
<div>
{/* 👇️ iterate object keys */}
{object.keys(employee).map(key => {
return (
<divkey={key}><h2>
{key}: {employee[key]}
h2><hr />div>
);
})}
div>
);
}
however, if we are iterating over the values of an object, we cannot safely use the value of the key property unless we can be sure that all values in the object are unique.
react uses the key prop internally for performance reasons. it helps the library ensure that only array elements that have changed are re-rendered.
having said that, unless we are dealing with thousands of array elements, we won't see any noticeable difference between using indices and stable unique identifiers.
looping over object values in react:
-
use
object.values()
the method to get an array of object values. -
use
map()
the method to iterate over an array of values.
export default functionapp() {
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
return (
<div>
{/* 👇️ iterate object values */}
{object.values(employee).map((value, index) => {
return (
<divkey={index}><h2>{value}h2><hr />div>
);
})}
div>
);
}
we use object.values
the method to get an array of object values.
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
// 👇️ [1, '迹忆客', 123]
console.log(object.values(employee));
if we only want to render the values of the object, we can access them directly using this method.
we can also use object.entries
the method, which returns an array of arrays of key-value pairs.
export default functionapp() {
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
console.log(object.entries(employee));
return (
<div>
{object.entries(employee).map(([key, value]) => {
return (
<divkey={key}><h2>
{key}: {employee[key]}
h2><hr />div>
);
})}
div>
);
}
here is object.entries()
what the output of the method looks like.
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
// 👇️ [
// ['id', 1],
// ['name', '迹忆客'],
// ['salary', 123],
// ]
const result = object.entries(employee);
console.log(result);
this method returns an array containing a sub-array of key-value pairs.
another approach is to use array.foreach()
the method to iterate over the keys of the object and push the jsx elements into an array, which we will then render.
export default functionapp() {
const employee = {
id: 1,
name: '迹忆客',
salary: 123,
};
const results = [];
object.keys(employee).foreach(key => {
results.push(
<h2key={key}>
{key}: {employee[key]}
h2>,
);
});
return (
<div>
{results}
div>
);
}
the method is called for each key array.foreach()
, but foreach()
the method returns undefined , so we can't use it directly in jsx code.
instead, we push the jsx element into the array we render.
注意
, which is a more indirect approach that we won't see used often in react applications.
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
scroll to bottom of div in react
publish date:2025/03/11 views:169 category:react
-
在 react 中滚动到 div 的底部: 在 div 的底部添加一个元素。 在底部的元素上设置一个 ref。 当事件发生时,调用 ref 对象的 scrollintoview() 方法。
how to draw a horizontal line in react
publish date:2025/03/11 views:73 category:react
-
在 react 中画一条水平线: 使用 hr / 标签并在其上设置 style 属性。 设置线条的高度,并可选择设置背景颜色和颜色。
how to use buttons as links in react
publish date:2025/03/10 views:140 category:react
-
要将按钮用作 react 中的链接,请将按钮包装在 标记中,如果使用 react 路由器,则将其包装在 link 组件中。 该按钮将被呈现而不是链接,单击它将导致浏览器导航到指定的页面。
using onclick on div elements in react
publish date:2025/03/10 views:121 category:react
-
要在 react 中的 div 元素上设置 `onclick` 监听器: 在 div 上设置 `onclick` 属性。 每次单击 div 时,都会调用我们传递给 prop 的函数。 我们可以通过 event.currenttarget 访问 div。
react: how to upload multiple files using axios
publish date:2025/03/10 views:146 category:react
-
这篇简洁明了的文章向您展示了如何在 axios 的帮助下在 react 中上传多个文件。这项工作并不太庞大,可以通过以下几个简单的步骤完成(您将在第 3 步中看到 javascript 和 typescript 代码片
how to toggle a boolean state in react
publish date:2025/03/10 views:93 category:react
-
在 react 中切换布尔状态:使用 usestate 钩子来跟踪布尔值的状态。 将函数传递给钩子返回的 setstate 函数。根据当前值切换布尔值,例如 setisloading(current => !current)。
creating a back button with react router
publish date:2025/03/10 views:155 category:react
-
使用 react router 创建后退按钮: 将按钮上的 onclick 属性设置为函数。使用 usenavigate() 钩子,例如 const navigate = usenavigate();。调用 navigate() 函数,传递它 -1 - navigate(-1)。
how to create a read more/less button in react
publish date:2025/03/10 views:168 category:react
-
在构建 web 应用程序时,有时我们需要显示一些长文本。在这种情况下,一个聪明的设计是只显示部分文本并添加一个显示更多按钮供用户在需要时展开文本。当文本已展开并完全显示时
insert style tags into react components
publish date:2025/03/10 views:66 category:react
-
要在 react 组件中插入样式标签,请将我们的 css 包装在样式标签中的模板字符串中。 花括号之间的表达式将被评估并添加样式。