3

尝试基于另一个对象数组创建对象数组。决定使用 flatmap 然后减少,但问题是当我在结果数组中有多个对象时 - 我无法在一个对象中收集多个状态。添加了我尝试过的内容以及我想要达到的结果。

是)我有的

const data = [
    {
        "timestamp": "2021-08-31T15:29:18Z",
        "result": [
            {
                "label": "Not Covered",
                "value": 132
            }
        ]
    },
    {
        "timestamp": "2021-09-30T15:29:18Z",
        "result": [
            {
                "label": "Not Covered",
                "value": 135
            }
        ]
    },
    {
        "timestamp": "2021-10-31T16:29:18Z",
        "result": [
            {
                "label": "Not Covered",
                "value": 135
            }
        ]
    }
]

我需要得到什么

[
    {
        "Not Covered":132,
        "Active":0,
        "Expiring Soon":0,
        "timestamp": "2021-08-31T15:29:18Z"
    },
    {
        "Not Covered":135,
        "Active":0,
        "Expiring Soon":0,
        "timestamp": "2021-09-30T15:29:18Z"
    },
    {
        "Not Covered":135,
        "Active":0,
        "Expiring Soon":0,
        "timestamp": "2021-10-31T16:29:18Z"
    }
]

我在做什么

let flattenedResult = data.flatMap(({result,...r}) => result.map(o => ({ ...o,...r})));

const chartData = flattenedResult.reduce((acc, item) => {
    const {timestamp, value,label} = item;

    acc.push({timestamp, "Not Covered":"Not Covered"===label?value:0,"Active":"Active"===label?value:0,"Expiring Soon":"Expiring Soon"===label?value:0});
    return acc;
  }, []); 

我得到了什么:

[
    {
        "timestamp": "2021-08-31T15:29:18Z",
        "Not Covered": 132,
        "Active": 0,
        "Expiring Soon": 0
    },
    {
        "timestamp": "2021-09-30T15:29:18Z",
        "Not Covered": 135,
        "Active": 0,
        "Expiring Soon": 0
    },
    {
        "timestamp": "2021-10-31T16:29:18Z",
        "Not Covered": 135,
        "Active": 0,
        "Expiring Soon": 0
    }
]
4

2 回答 2

2

您还没有解释如何获得“活动”和“即将到期”的值,但我相信这样的事情应该会有所帮助

const data = [
    {
        "timestamp": "2021-08-31T15:29:18Z",
        "result": [
            {
                "label": "Expiring Soon",
                "value": 57
            },
            {
                "label": "Not Covered",
                "value": 132
            },
            {
                "label": "Active",
                "value": 42
            }
        ]
    },
    {
        "timestamp": "2021-09-30T15:29:18Z",
        "result": [
            {
                "label": "Not Covered",
                "value": 135
            }
        ]
    },
    {
        "timestamp": "2021-10-31T16:29:18Z",
        "result": [
            {
                "label": "Not Covered",
                "value": 135
            },
            {
                "label": "Active",
                "value": 42
            }
        ]
    }
];

console.log(data.reduce((acc, {timestamp, result}) => {
    const datum = result.reduce((acc, {label, value}) => ({
        ...acc,
        [label]: value,
    }), {
        timestamp,
        'Not Covered': 0,
        Active: 0,
        'Expiring Soon': 0
    });
    return acc.concat(datum);
}, []));

于 2021-09-01T13:47:46.963 回答
1

这样的事情呢?然后,您可以根据您的业务逻辑放置“Active”和“ExpringSoon”。

const array = data.map(item => {
  const results = {}
  item.result.forEach(({ label, value }) => results[label] = value )
  return {
    Active: 0,
    ExpiringSoon: 0,
    timestamp: item.timestamp,
    ...results
  }
})

结果

[
  {
    Active: 0,
    ExpiringSoon: 0,
    timestamp: '2021-08-31T15:29:18Z',
    'Not Covered': 132
  },
  {
    Active: 0,
    ExpiringSoon: 0,
    timestamp: '2021-09-30T15:29:18Z',
    'Not Covered': 135
  },
  {
    Active: 0,
    ExpiringSoon: 0,
    timestamp: '2021-10-31T16:29:18Z',
    'Not Covered': 135
  }
]
于 2021-09-01T13:54:53.537 回答