如何在对象中渲染对象中的数组?

时间:2019-12-17 10:02:51

标签: arrays reactjs object

我正在尝试在react中呈现以下数据。

        const fakeData = {
            201907: {
                15: [{
                    yearMonthKey: '201907',
                    dayKey: '15',
                    startDate: '2019-07-15 00:00:00+0900',
                    title: 'testProgrma',
                    time: '04:29 ~ 04:29',
                    imageUrl: [faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false)], 
                    firstCome: 10
                },
                {
                    yearMonthKey: '201907',
                    dayKey: '15',
                    startDate: '2019-07-15 00:00:00+0900',
                    title: 'testProgrma123132',
                    time: '04:29 ~ 04:29',
                    imageUrl: [faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false)], 
                    firstCome: 10
                }],
                16: [{
                        yearMonthKey: '201907',
                        dayKey: '16',
                        startDate: '2019-07-15 00:00:00+0900',
                        title: 'testProgrma',
                        time: '04:29 ~ 04:29',
                        imageUrl: [faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false)], 
                        firstCome: 10
                    }]
            },
                201908: {
                    17: [{
                        yearMonthKey: '201908',
                        dayKey: '17',
                        startDate: '2019-08-15 00:00:00+0900',
                        title: 'testProgrma',
                        time: '04:29 ~ 04:29',
                        imageUrl: [faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false), faker.image.imageUrl(612, 477, undefined, true, false)], 
                        firstCome: 10
                    }]
                }
        }

fakeData包含两个大数组(201907,201908),每个对象,另一个对象嵌套在内部(15、16、17)。每个嵌套对象都将数组作为值。

但这是问题所在。

组件必须呈现如下。

201907
  ㅣ
   - 15
     ㅣ
      - title:'testProgrma',
      - title:'testProgrma123132'
   - 16
     ㅣ
      - title: 'testProgrma'
201908
  ㅣ
   - 17
     ㅣ
      - title: 'testProgrma'

它是深层嵌套的,并根据yearMonthKey和dayKey进行绑定。

1 个答案:

答案 0 :(得分:0)

反应不会让对象渲染。因此,在循环之前尝试构建数组。

    let newArray = []

    for(let j in fakeData){
       for(let i in j){
       newArray.push(i)
    }
    }

console.log(newArray)

然后使用.map()渲染newArray