gpt4 book ai didi

javascript - 如何分配和分组 obj by?

转载 作者:行者123 更新时间:2023-12-02 20:58:55 24 4
gpt4 key购买 nike

我正在运行以下命令:

const result = data.map(function(item) {
const ditem = dictionary.find(d => d.state == item.state);
if(ditem) {
item.date = (item.date.toString().slice(4)+item.date.toString().slice(0,2)).match(/.{1,2}/g).join("/")
return {
...item,
"lat": ditem.lat ,
"long": ditem.long
}
}
return item;
});

这给出:

  {
"date": "04/21/20",
"state": "AK",
"positive": 329,
"negative": 10790,
"pending": null,
"hospitalizedCurrently": 42,
"hospitalizedCumulative": 36,
"inIcuCurrently": null,
"inIcuCumulative": null,
"onVentilatorCurrently": null,
"onVentilatorCumulative": null,
"recovered": 168,
"hash": "10d6907c6ec12caa4a5896fc50c4a3f06c836c0c",
"dateChecked": "2020-04-21T20:00:00Z",
"death": 9,
"hospitalized": 36,
"total": 11119,
"totalTestResults": 11119,
"posNeg": 11119,
"fips": "02",
"deathIncrease": 0,
"hospitalizedIncrease": 0,
"negativeIncrease": 987,
"positiveIncrease": 8,
"totalTestResultsIncrease": 995,
"lat": "9875.33.00",
"long": "-8371.42.00"
},

但是我需要组数据下的所有值,就像这样,因为这是我用于代码其他部分的格式:

State: "AK"
Lat: 34.802075
Long: 38.996815000000005
data: Array(92)
0: {date: "1/22/20", positive: 0, death: 0, recovered: 0}
1: {date: "1/23/20", positive: 0, death: 0, recovered: 0}
2: {date: "1/24/20", positive: 0, death: 0, recovered: 0}

坐标位于一个单独的文件中,我根据State名称抓取并插入该文件

item.date = item.date.toString();
item.date = (item.date.toString().slice(4)+item.date.toString().slice(0,2)).match(/.{1,2}/g).join("/")

我想我已经快到了,但我不知道在哪里添加位来获取纬度和经度,所以这个

const ditem = dictionary.find(d => d.state == item.state);

需要添加

const formatted = Object.assign(
{},data.map(
area => {
return {
"state": area["state"],
"lat": area["lat"],
"long": area["long"],
"data": Object.assign({},Object.keys(area)
.map(
date => ({
date: area[date], negative: area[date], positive: area[date], pending: area[date], hospitalizedCurrently: area[date],
hospitalizedCumulative: area[date], inIcuCurrently: area[date], inIcuCumulative: area[date], onVentilatorCurrently: area[date],
onVentilatorCumulative: area[date], recovered: area[date], dateChecked: area[date], death: area[date],
hospitalized: area[date], total: area[date], totalTestResults: area[date], posNeg: area[date],
fips: area[date], deathIncrease: area[date], hospitalizedIncrease: area[date], negativeIncrease: area[date],
positiveIncrease: area[date], totalTestResultsIncrease: area[date]
})
)
)
};
}
)
);

另一个重要的事情是日期格式,我使用:

item.date = (item.date.toString().slice(4)+item.date.toString().slice(0,2)).match(/.{1,2}/g).join("/")

因为我们收到的日期类似于:日期:20200421,而我们需要它,例如24/4/20,因此在构建组时也需要对其进行转换

最佳答案

您可以按州减少,而不是映射。

const data = [
{state: 'fr', date:'20200421', city: 'lyon', positive:1},
{state: 'fr', date:'20200422', city:'paris', positive:2},
{state: 'en', date:'20200423', city:'london', positive:3},
{state: 'en', date:'20200424', city:'oxford', positive:4}
]
const dictionary = [{state: 'en', lat: 52.6412763, long: -1.2767434,}, {state:'fr', lat:45.7731412, long:4.8628491}]
const stateToCities = data.reduce((acc, { state, ...item }) => {
// for big array use hashmap for dictionary instead
const ditem = dictionary.find(d => d.state == state)
if (!ditem) return acc // no nest level, early exit

const group = acc.get(ditem) || []
const withoutState = { }
group.push({
...item,
// maybe you did not to modify existing item, up to you
date: (item.date.toString().slice(4)+item.date.toString().slice(0,2)).match(/.{1,2}/g).join("/")
})
return acc.set(ditem, group)
}, new Map())

const payloadAsInQuestion = [...stateToCities.entries()].map(([{state, long, lat}, cities]) => {
return {
state, long, lat,
data: cities
}
})
// if you need array
console.log('stringified' , JSON.stringify(payloadAsInQuestion, null, 2))

请注意,这有点自相矛盾:您想一次性“更新”项目,但在字典(一个数组)中查找。所以你可以使用字典的 HashMap 来代替

关于javascript - 如何分配和分组 obj by?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61402596/

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