简体   繁体   English

JS map数组返回arrays的object给React

[英]JS map array to return object of arrays for React

If I want to return an array of objects from a nested array in JS/React, what would be the way to do this?如果我想从 JS/React 中的嵌套数组返回一个对象数组,该怎么做? I have tried the following, but this gives an error about react children - objects cant be rendered as children try an array.我已经尝试了以下方法,但这给出了一个关于反应孩子的错误 - 当孩子尝试数组时无法呈现对象。

data:数据:

{
      "title": 0,
      "name": 0,
      "score": 0,
      "summary": [
        {
          "id": 1,
          "start": "2019-11-01",
          "end": "2019-11-04"
        },
        {
          "id": 2,
          "start": "2019-11-01",
          "end": "2019-11-04"
        }
      ]
    } 

I have tried:我努力了:

const newArray = result?.summary?.map((inner: any) => {
    return (
        {
            from: new Date(inner?.start),
            to: new Date(inner?.end),
            label: {
                text: (inner?.id),
            },
         }
    )
})

I want to create a new array of objects with the data from the summary nested array?我想用summary嵌套数组中的数据创建一个新的对象数组?

any idea's?有任何想法吗?

This is the issue.这是问题。 You need to convert your dates to strings .您需要将dates转换为strings The third-party package you input this array tries to render the Date objects and fails to do so.您输入此数组的第三方包尝试呈现 Date 对象,但未能这样做。 The date is object type and that is what the error is all about.日期是object type ,这就是错误的全部内容。

Try this:尝试这个:

const newArray = result?.summary?.map((inner: any) => {
    return (
        {
            from: new Date(inner?.start).toString(),
            to: new Date(inner?.end).toString(),
            label: {
                text: (inner?.id),
            },
         }
    )
})

Use toDateString to get a formatted date string.使用toDateString获取格式化的日期字符串。

I have a different issue, but with similar data.我有一个不同的问题,但有类似的数据。 I'm trying to create a React component which iterates through a collection and returns selected data from within each document, using the render() function.我正在尝试创建一个 React 组件,它使用 render() 函数遍历一个集合并从每个文档中返回选定的数据。 For the data above, the data-fetching part of my code within render() would look like this:对于上面的数据,render() 中我的代码的数据获取部分将如下所示:

              {this.state.buttonClicked
                ? this.state.data.map(data => {
                    return (
                      <React.Fragment>
                      <tr>
                        <td 
                            style={{align: "left", width: "10%"}}
                            >{data.title}</td>
                        <td
                            style={{align: "left", width: "10%"}}
                            >{data.name}</td>
                        <td>{data.score}</td>
                        <td>{data.summary.start}</td>
                        <td>{data.summary.end}</td>
                        </tr>
                      </React.Fragment>
                    );
                  })
                : null}

I've tried putting array indexes into the expression, eg data.summary[1].start , but whatever I do the data from within the array doesn't appear.我试过将数组索引放入表达式中,例如data.summary[1].start ,但无论我做什么,数组中的数据都不会出现。

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

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