[英]loop through multiple objects inside the first array from JSON data displaying two arrays with objects in React using FUNCTION COMPONENT
what is the best and simple way to access this data in ReactJS using FUNCTION COMPONENT ?使用FUNCTION COMPONENT在 ReactJS 中访问这些数据的最佳和简单方法是什么?
The problem is that I have two arrays with objects and I want TO LOOP THROUGH THE FIRST ARRAY AND DISPLAY EACH ITEMS FROM THE OBJECT INSIDE.问题是我有两个带有对象的 arrays 并且我想循环通过第一个数组并显示 OBJECT 里面的每个项目。
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
This is how the data are represented in each array这是数据在每个数组中的表示方式
0:
first_name: "Jane"
id: 6
last_name: "Doe"
city: "budapest"
1:
first_name: "Michael"
id: 10
last_name: "Holland"
city: "France"
etc...
(Please take a look at the picture below to understand what I mean by "0:" or "1:". That is the way the json data is displayed inside the array by records i guess) (请看下面的图片来理解我所说的“0:”或“1:”是什么意思。我猜这就是 json 数据在数组中显示的方式)
My current solution below only work when you have one array with objects inside but in this case as you can see I have two.我下面的当前解决方案仅在您有一个包含对象的数组时才有效,但在这种情况下,您可以看到我有两个。
const items = [
{first_name: "Jane",id: "6",last_name: "Doe",city: "Budapest"},
{first_name: "David",id: "10",last_name: "Smith",city: "Paris"},
]
const newData = items.map((item) => {
return (
<div className="something" key={item.id}>{item.first_name}</div>
)
})
Please note that I am using ReactJS 16.9请注意,我使用的是ReactJS 16.9
Thank you in advance.先感谢您。
If I am getting you correctly, then you can merge both Array and then use it.如果我的理解正确,那么您可以合并两个 Array 然后使用它。
Something like this:像这样的东西:
// First Array
const stations = [
{first_name: "Jane",id: "6",last_name: "Doe",city: "Budapest"},
{first_name: "David",id: "10",last_name: "Smith",city: "Paris"},
];
// Second Array
const stations2 = [
{first_name: "Jane",id: "6",last_name: "Doe",city: "Budapest"},
{first_name: "David",id: "10",last_name: "Smith",city: "Paris"},
];
const allStations = [...stations , ...stations2 ]; // <----- HERE
const newData = allStations.map((item) => {
return (
<div className="something" key={item.id}>{item.first_name}</div>
)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.