简体   繁体   English

循环一个物体反应?

[英]Loop an object in react?

Data : 数据:

在此输入图像描述

I have data that have inside arrays, I try loop by default code: 我有数据内部数组,我尝试循环默认代码:

 <div className="loop-container">
    {
        joblist.map((itemb,index) => (
            <div>
            </div>
        ))
    }
</div>

I want show name of that arrays first, (this is date) then show what this arrays consist, But I get error: 我想首先显示该数组的名称,(这是日期),然后显示这个数组包含的内容,但是我收到错误:

joblist.map is not a function joblist.map不是一个函数

Directly we can't use map , filter on object, first we need to get an array (either property or property values) from that object then only we can. 直接我们不能使用mapfilter对象,首先我们需要从该对象获取一个数组(属性或属性值)然后我们才能。


Data that you are getting is not an array, so directly we can't use Array.prototype.map . 您获得的数据不是数组,因此我们不能直接使用Array.prototype.map What you need to do is first use Object.keys to get all the keys in an array. 您需要做的是首先使用Object.keys来获取数组中的所有键。 Once you get the array use map on that and inside map function body use another map to iterate the values. 一旦你获得数组使用map和内部map功能体使用另一个map来迭代值。

You can also use Object.values or Object.entries , but pattern will be not same as Object.keys() . 您也可以使用Object.valuesObject.entries ,但pattern与Object.keys()不同

Write it like this: 写这样:

<div className="loop-container">
    {
        Object.entries(joblist).map(([key, value]) => (
            <div id={key}>
                Date: {key}
                {
                    value.map(el => <div key={el.id}> {el.created_time} </div> )
                }
            </div>
        ))
    }
</div>

Check this Snippet Using Object.entries : 使用Object.entries检查此代码段:

 let obj = { 'key1': [{a:1}, {a:2}, {a:3}], 'key2': [{a:4}, {a:5}, {a:6}], 'key3': [{a:7}, {a:8}, {a:9}] }; Object.entries(obj).map(([key, value]) => { console.log('key name = ', key); value.map(el => { console.log(el.a); }) }) 

Check this Snippet Using Object.keys : 使用Object.keys检查此代码段:

 let obj = { 'key1': [{a:1}, {a:2}, {a:3}], 'key2': [{a:4}, {a:5}, {a:6}], 'key3': [{a:7}, {a:8}, {a:9}] }; Object.keys(obj).map(key => { console.log('key name = ', key); obj[key].map(el => { console.log(el.a); }) }) 

Check this Snippet Using Object.values : 使用Object.values检查此代码

 let obj = { 'key1': [{a:1}, {a:2}, {a:3}], 'key2': [{a:4}, {a:5}, {a:6}], 'key3': [{a:7}, {a:8}, {a:9}] }; Object.values(obj).map(value => { console.log('value = ', JSON.stringify(value)); value.map(el => { console.log(el.a); }) }) 

As you see joblist is not array, it is object. 如你所见, joblist不是数组,它是对象。 If you really want to use map then you can use new Map() . 如果你真的想使用map那么你可以使用new Map() The new Map() constructor accepts an iterable of entries. 新的Map()构造函数接受一个可迭代的条目。 With Object.entries , you can easily convert from Object to Map: 使用Object.entries ,您可以轻松地从Object转换为Map:

var mapjoblist = new Map(Object.entries(joblist));

<div className="loop-container">
    {mapjoblist.map((itemb,index) => (
       <div>
        .......

       </div>

     ))}
</div>

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

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