简体   繁体   English

从 JSON 数据中循环遍历第一个数组中的多个对象,显示两个 arrays 与使用 FUNCTION 组件的 React 中的对象

[英]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 里面的每个项目。

console.log(data)控制台日志(数据)

(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.

相关问题 如何在React组件中遍历数组内部对象的属性? - How to loop through properties of objects inside of an array within a React component? 需要一个函数,使用 lodash 从数组中的两个嵌套对象中解析出数据,并将其作为 prop 传递给 react 组件 - Need a function that parse out data from two nested objects in an array using lodash and pass it as a prop in a react component 如何循环通过 JSON 对象和 Arrays 内部 - How to Loop through JSON Objects having Objects and Arrays Inside Javascript 嵌套 for 循环显示来自 JSON 对象数组的数据 - Javascript nested for loop displaying data from JSON objects array 尝试遍历对象数组以使用对象中的数据填充数组 - Trying to loop through an array of objects to populate arrays with data in objects 如何将两个 arrays 组合成具有来自每个数组的数据的多个对象? - How to combine two arrays into multiple objects with data from each array? 使用来自多个 JSON 对象和 arrays 的数据动态渲染 React 组件 - Dynamic rendering React components using data from multiple JSON objects and arrays 如何遍历一个复杂的JSON对象数组,其中包含数组和动态键? - How do I loop through a complicated array of JSON objects with arrays inside it and dynamic keys? 如何遍历两个具有多个 arrays 内部的对象以获得我想要的值? - How do I loop through two objects with multiple arrays inside to get the values I want? 显示来自文档内部对象数组的数据 - Displaying data from an array of objects inside document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM