简体   繁体   English

ReactJS 将我的奇怪数组转换为 object 以将其添加到列表中

[英]ReactJS Convert my strange array to an object to add it to a list

I have the following array:我有以下数组:

[{…}]
0: {id: 2, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user", …}
1: ....
2: ....
3: ....
....
length: 5
__proto__: Array(0)

I want to convert it to an object and I found this code, but all I receive is the 0:我想将其转换为 object,我找到了这段代码,但我收到的只是 0:

Object.keys(fetchedData.data).map(key => console.log(key))

How to access and convert my 5 array values?如何访问和转换我的 5 个数组值?

So I've iterated across your array and used the 'id' field as a key.因此,我遍历了您的数组并将“id”字段用作键。 The key for each object needs to be unique, so this assumes your ID is unique...每个 object 的密钥必须是唯一的,因此假设您的 ID 是唯一的...

 const fetchedData = [ {id: 1, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"}, {id: 2, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"}, {id: 3, createdAt: "2021-06-11T10:13:46.814Z", exchangedAt: "2021-06-11T08:04:11.415Z", imageUrl: "url", user: "user"} ] obj = {} fetchedData.forEach(x => { tempX = {...x} delete tempX["id"] obj[x.id] = tempX }) console.log('Converted object', obj) //Using your method, just to log the values without converting it into an object, you could do... fetchedData.forEach(el => console.log("Individual Element", el))

...so when you use Object.keys() you are iterating across the keys of an object, but as this is an array of objects, the keys are just the indexes of the array. ...因此,当您使用Object.keys()时,您正在遍历 object 的键,但由于这是一个对象数组,因此键只是数组的索引。 .forEach iterates across the array and gives you each element to work with. .forEach遍历数组并为您提供每个要使用的元素。

You've used .map() in your code, which also iterates through an array, but returns a new array, so you could change each element.您在代码中使用了.map() ,它也遍历一个数组,但返回一个新数组,因此您可以更改每个元素。 .forEach just iterates and doesn't return, so you're better off using this for logging to the console. .forEach只是迭代并且不返回,所以你最好使用它来登录到控制台。

a = [
  {
     "id":1,
     "createdAt":"2021-06-11T10:13:46.814Z",
     "exchangedAt":"2021-06-11T08:04:11.415Z",
     "imageUrl":"url",
     "user":"user"
  },
  {
     "id":2,
     "createdAt":"2021-06-11T10:13:46.814Z",
     "exchangedAt":"2021-06-11T08:04:11.415Z",
     "imageUrl":"url",
     "user":"user"
  },
  {
     "id":3,
     "createdAt":"2021-06-11T10:13:46.814Z",
     "exchangedAt":"2021-06-11T08:04:11.415Z",
     "imageUrl":"url",
     "user":"user"
  },
  {
     "id":2,
     "createdAt":"2021-06-11T10:13:46.814Z",
     "exchangedAt":"2021-06-11T08:04:11.415Z",
     "imageUrl":"url",
     "user":"user"
  },
  {
     "id":4,
     "createdAt":"2021-06-11T10:13:46.814Z",
     "exchangedAt":"2021-06-11T08:04:11.415Z",
     "imageUrl":"url",
     "user":"user"
  }
]
(5) [{…}, {…}, {…}, {…}, {…}]
Object.keys(a).map(key => console.log(key))
VM532:1 0
VM532:1 1
VM532:1 2
VM532:1 3
VM532:1 4

It is accessing to 5 values.它正在访问 5 个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM