将特定的数组值合并为单个值

时间:2019-07-02 06:49:41

标签: javascript arrays csv

我已经解析了一个csv文件,该文件给了我这样的数组:

[{
"year": 2019,
"month": 6,
"day": 25,
"hour": 4,
"minute": 0,
"temperature": 26.52
},
{
"year": 2019,
"month": 6,
"day": 25,
"hour": 4,
"minute": 0,
"temperature": 26.52
}]

我想将minutehourdaymonthyear合并到单个密钥。像这样:

"time": "2019-07-02 09:57:35"

因此我可以将其用作API上的datetime对象。

我当前获取数据的方式是:

 const cleanKeys = [
         'year',
         'month',
         'day',
         'hour',
         'minute',
         'temperature',

     ];

    const dataAsObject = totalData.map(function (values) {
        return cleanKeys.reduce(function (o, k, i) {
            o[k] = values[i];
            return o;
        }, {})
    });

这基本上是向所有数据添加标头键。我只对合并minutehourdaymonthyear列感兴趣。

2 个答案:

答案 0 :(得分:1)

我建议您使用内置的Date constructor

const importedPrvKey = await crypto.subtle.importKey(
  'raw',
  hexToUintArray('4bd22700ec3450b5f27e47ba70c233a680c981ab02c1432a859ae23111bef377'),
  {
    name: 'ECDH',
    namedCurve: 'P-256'
  },
  true,
  []
);

编辑:

请使用循环日期在下面找到更新的答案:

var obj = {"year": 2019,
"month": 6,
"day": 25,
"hour": 4,
"minute": 0,
"temperature": 26.52};

const date = new Date(obj.year, obj.month - 1, obj.day, obj.hour, obj.minute);

const newObj = {date, temperature: obj.temperature};

console.log(JSON.stringify(newObj));

答案 1 :(得分:0)

您可以自己创建字符串,例如:

[
    {
        "title": "Title",
        "location": "USA",
        "date": "12 October 2018",
        "content": "SOme content",
        "apply_link": "https://somelink.com"
    }
]
相关问题