如何通过分配和分组obj?

我正在运行下面的程序。

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("/")

因为我们收到的日期是这样的 date: 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))

给TA打赏
共{{data.count}}人
人已打赏
未分类

基于另一个javascript文件定义一个带参数的函数。

2022-9-8 18:32:04

未分类

glVertexAttribPointer似乎不工作,但没有错误。

2022-9-8 18:58:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索