简体   繁体   English

ReactJS 通过对象映射

[英]ReactJS map through Object

I have a response like this:我有这样的回应:

在此处输入图片说明

I want to display the name of each object inside this HTML:我想在这个 HTML 中显示每个对象的名称:

{subjects.map((item, i) => (
  <li className="travelcompany-input" key={i}>
    <span className="input-label">{ item.name }</span>
  </li>
))}   

But it throws an error of subjects.map is not a function .但它抛出了一个错误subjects.map is not a function

First, I have to define the keys of the objects where it creates an array of keys, where I want to loop through and show the subject.names .首先,我必须定义对象的键,在其中创建键数组,我想在其中循环并显示subject.names

What I also tried is this:我还尝试过的是:

{Object.keys(subjects).map((item, i) => (
  <li className="travelcompany-input" key={i}>
    <span className="input-label">key: {i} Name: {subjects[i]}</span>
  </li>
))}

When calling Object.keys it returns a array of the object's keys.调用Object.keys它返回对象键的数组。

Object.keys({ test: '', test2: ''}) // ['test', 'test2']

When you call Array#map the function you pass will give you 2 arguments;当您调用Array#map ,您传递的函数将为您提供 2 个参数;

  1. the item in the array,数组中的项目,
  2. the index of the item.项目的索引。

When you want to get the data, you need to use item (or in the example below keyName ) instead of i当您想要获取数据时,您需要使用item (或在下面的示例中keyName )而不是i

{Object.keys(subjects).map((keyName, i) => (
    <li className="travelcompany-input" key={i}>
        <span className="input-label">key: {i} Name: {subjects[keyName]}</span>
    </li>
))}

You get this error because your variable subjects is an Object not Array , you can use map() only for Array.您收到此错误是因为您的变量subjectsObject而不是Array ,您只能将map()用于 Array。

In case of mapping object you can do this:在映射对象的情况下,您可以这样做:

{ 
    Object.keys(subjects).map((item, i) => (
        <li className="travelcompany-input" key={i}>
            <span className="input-label">{ subjects[item].name }</span>
        </li>
    ))
}  

Map over the keys of the object using Object.keys() :使用Object.keys()映射对象的键:

{Object.keys(yourObject).map(function(key) {
  return <div>Key: {key}, Value: {yourObject[key]}</div>;
})}

Use Object.entries() function.使用Object.entries()函数。

Object.entries(object) return: Object.entries(object)返回:

[
    [key, value],
    [key, value],
    ...
]

see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entrieshttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

{Object.entries(subjects).map(([key, subject], i) => (
    <li className="travelcompany-input" key={i}>
        <span className="input-label">key: {i} Name: {subject.name}</span>
    </li>
))}

Do you get an error when you try to map through the object keys, or does it throw something else.当您尝试通过对象键进行映射时,您是否遇到错误,或者是否抛出其他内容。

Also note when you want to map through the keys you make sure to refer to the object keys correctly.另请注意,当您想通过键进行映射时,请确保正确引用对象键。 Just like this:就像这样:

{ Object.keys(subjects).map((item, i) => (
   <li className="travelcompany-input" key={i}>
     <span className="input-label">key: {i} Name: {subjects[item]}</span>
    </li>
))}

You need to use {subjects[item]} instead of {subjects[i]} because it refers to the keys of the object.您需要使用{subjects[item]}而不是{subjects[i]}因为它指的是对象的键。 If you look for subjects[i] you will get undefined.如果您查找 subject[i],您将得到未定义。

I am not sure why Aleksey Potapov marked the answer for deletion but it did solve my problem.我不确定为什么 Aleksey Potapov 将答案标记为删除,但它确实解决了我的问题。 Using Object.keys(subjects).map gave me an array of strings containing the name of each object, while Object.entries(subjects).map gave me an array with all data inside witch it's what I wanted being able to do this:使用 Object.keys(subjects).map 给了我一个包含每个对象名称的字符串数组,而 Object.entries(subjects).map 给了我一个包含所有数据的数组,这是我想要的:

const dataInfected = Object.entries(dataDay).map((day, i) => {
    console.log(day[1].confirmed);
});

I hope it helps the owner of the post or someone else passing by.我希望它可以帮助帖子的所有者或其他路过的人。

I use the below Object.entries to easily output the key and the value:我使用下面的Object.entries来轻松输出键和值:

{Object.entries(someObject).map(([key, val], i) => (
    <p key={i}>
        {key}: {val}
    </p>
))}

Also you can use Lodash to direct convert object to array:您也可以使用Lodash将对象直接转换为数组:

_.toArray({0:{a:4},1:{a:6},2:{a:5}})
[{a:4},{a:6},{a:5}]

In your case:在你的情况下:

_.toArray(subjects).map((subject, i) => (
    <li className="travelcompany-input" key={i}>
        <span className="input-label">Name: {subject[name]}</span>
    </li>
))}

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

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