gpt4 book ai didi

javascript - 将对象的值分组到新数组中 - JavaScript

转载 作者:行者123 更新时间:2023-11-28 11:29:11 25 4
gpt4 key购买 nike

我需要返回一个数组,其中包含从对象数组中提取的多个元素。

这是原始对象:

    data: Array(11)
0: {id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96, …}
1: {id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96, …}
2: {id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96, …}
3: {id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81, …}
4: {id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44, …}
5: {id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44, …}
6: {id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44, …}
7: {id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44, …}
8: {id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44, …}
9: {id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44, …}
10: {id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66, …}

我正在使用这样的 map 功能:

    const FECHA_DATA = [
datosDiarios &&
datosDiarios.data.map(fecha => {
const { FECHA, PD_GAS, PD_AGUA, PD_PETROLEO } = fecha;
return FECHA;
}),
];

我得到了这个结果:

    [Array(11)]
0: (11) ["2010-06-01", "2010-06-02", "2010-06-03", "2010-06-04", "2010-06-05", "2010-06-06", "2010-06-08", "2010-06-09", "2010-06-1..."]

但是,对于 PD_GAS、PD_PETROLEO 和 PD_AGUA,我需要这样的数组:

    [Array(11)]
0: (11) ["2010-06-01", "2010-06-02", "2010-06-03", "2010-06-04", "2010-06-05", "2010-06-06", "2010-06-08", "2010-06-09", "2010-06-1..."],
1: (11) [96, 96, 96, 81, 44, 44, 44, 44, 44, 44, 66],
2: (11) [1138.48, 1138.48, 1138.48, 1146.85, 1160.95, 1160.95, 1160.95, 1160.95, 1160.95, 1160.95, 1157.58],
3: (11) [369.168, 364.21, 361.559, 322.346, 339.027, 346.939, 321.934, 351.404, 360.167, 371.895, 356.232],

我认为使用函数映射是可能的,但如果有人有其他方法可以做到这一点,我将不胜感激。

最佳答案

.map() 不起作用的原因是映射数组会根据原始数据集生成 1->1 结果。看起来您想要一个 4->1 结果,对于原始数据中的每条记录,您希望在 4 个数组中生成一个条目。最简单的方法可能是使用 .reduce() 函数,如下所示:

let data = [{id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96},
{id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96},
{id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96},
{id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81},
{id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44},
{id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44},
{id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44},
{id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44},
{id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44},
{id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44},
{id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66}];

let mappedData = data.reduce((res, curr) => {
// push whatever property you want to each spot in the array
// you can change the order however you'd like
res[0].push(curr.COMPLETAMIENTO);
res[1].push(curr.FECHA);
res[2].push(curr.PD_PETROLEO);
res[3].push(curr.PD_GAS);
return res;
}, [[], [], [], []]); // start with an array of 4 empty arrays into which we can push our values

console.log(mappedData)

关于javascript - 将对象的值分组到新数组中 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59902043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com