如何用Javascript将对象分组年份?

时间:2019-05-16 04:24:13

标签: javascript arrays object

我想在JavaScript中创建一个新对象以对year进行分组。它将数据存储到年份组中。

我现在拥有的数据:

{
    "8": {
        "date": "2016-02-26",
        "value": "0.89000"
    },
    "7": {
        "date": "2017-02-27",
        "value": "0.94000"
    },
    "6": {
        "date": "2017-06-01",
        "value": "0.94000"
    },
    "5": {
        "date": "2017-08-30",
        "value": "0.94000"
    },
    "4": {
        "date": "2017-11-30",
        "value": "1.01000"
    },
    "3": {
        "date": "2018-02-28",
        "value": "1.01000"
    },
    "2": {
        "date": "2018-06-01",
        "value": "1.01000"
    },
    "1": {
        "date": "2018-08-31",
        "value": "1.01000"
    },
    "0": {
        "date": "2018-11-30",
        "value": "1.16000"
    }
}

我想转换为以下内容:

{
    2016: [
            {
                "date": "2016-02-26",
                "value": "0.89000"
            }
    ],
    2017: [
            {
                "date": "2017-02-27",
                "value": "0.94000"
            },
            {
                "date": "2017-06-01",
                "value": "0.94000"
            },
            {
                "date": "2017-06-01",
                "value": "0.94000"
            }, 
            {
                "date": "2017-08-30",
                "value": "0.94000"
            }
    ],
    2018: [
            {
                "date": "2018-02-27",
                "value": "0.104000"
            },
            {
                "date": "2018-06-01",
                "value": "1.01000"
            },
            {
                "date": "2018-08-31",
                "value": "1.01000"
            }, 
            {
                "date": "2018-11-30",
                "value": "1.16000"
            }
    ]
}

更多内容旨在在此处发布问题。更多内容,目的是在此处发布问题。更多内容,目的是在此处发布问题。更多内容,目的是在此处发布问题。一些其他内容,目的是在此处发布问题。

4 个答案:

答案 0 :(得分:2)

您可以使用Object.values将对象转换为数组。使用reduce遍历数组并将其汇总为一个对象。使用substring(0, 4)获取日期字符串的前4个字符。

var obj = {"0":{"date":"2018-11-30","value":"1.16000"},"1":{"date":"2018-08-31","value":"1.01000"},"2":{"date":"2018-06-01","value":"1.01000"},"3":{"date":"2018-02-28","value":"1.01000"},"4":{"date":"2017-11-30","value":"1.01000"},"5":{"date":"2017-08-30","value":"0.94000"},"6":{"date":"2017-06-01","value":"0.94000"},"7":{"date":"2017-02-27","value":"0.94000"},"8":{"date":"2016-02-26","value":"0.89000"}}

var result = Object.values(obj).reduce((c, v) => {
  var y = v.date.substring(0, 4);
  (c[y] = c[y] || []).push(v);
  return c;
}, {});

console.log(result);

答案 1 :(得分:2)

您可以使用Array.prototype.reduce()

代码:

const data = {"8": {"date": "2016-02-26","value": "0.89000"},"7": {"date": "2017-02-27","value": "0.94000"},"6": {"date": "2017-06-01","value": "0.94000"},"5": {"date": "2017-08-30","value": "0.94000"},"4": {"date": "2017-11-30","value": "1.01000"},"3": {"date": "2018-02-28","value": "1.01000"},"2": {"date": "2018-06-01","value": "1.01000"},"1": {"date": "2018-08-31","value": "1.01000"},"0": {"date": "2018-11-30","value": "1.16000"}};
const result = Object.values(data).reduce((a, {date, value}) => {
  const year = date.slice(0, 4);
  a[year] = a[year] || [];
  a[year].push({date, value});
  return a;
}, {});

console.log(result);

答案 2 :(得分:1)

reduceObject.values一起使用:

const data = {"0":{"date":"2018-11-30","value":"1.16000"},"1":{"date":"2018-08-31","value":"1.01000"},"2":{"date":"2018-06-01","value":"1.01000"},"3":{"date":"2018-02-28","value":"1.01000"},"4":{"date":"2017-11-30","value":"1.01000"},"5":{"date":"2017-08-30","value":"0.94000"},"6":{"date":"2017-06-01","value":"0.94000"},"7":{"date":"2017-02-27","value":"0.94000"},"8":{"date":"2016-02-26","value":"0.89000"}};

const res = Object.values(data).reduce((acc, { date, value }) => {
  let [year] = date.split("-");
  (acc[year] = acc[year] || []).push({ date, value });
  return acc;
}, {});

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

ES5语法:

var data = {"0":{"date":"2018-11-30","value":"1.16000"},"1":{"date":"2018-08-31","value":"1.01000"},"2":{"date":"2018-06-01","value":"1.01000"},"3":{"date":"2018-02-28","value":"1.01000"},"4":{"date":"2017-11-30","value":"1.01000"},"5":{"date":"2017-08-30","value":"0.94000"},"6":{"date":"2017-06-01","value":"0.94000"},"7":{"date":"2017-02-27","value":"0.94000"},"8":{"date":"2016-02-26","value":"0.89000"}};

var res = Object.keys(data).map(function(key) {
  return data[key];
}).reduce(function(acc, curr) {
  var year = curr.date.split("-")[0];
  (acc[year] = acc[year] || []).push(year);
  return acc;
}, {});

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

答案 3 :(得分:1)

您可以使用一个Array.reduce来执行此操作,并且可以在reduce之前Array.sort来获得所需的排序输出:

var data = { "8": { "date": "2016-02-26", "value": "0.89000" }, "7": { "date": "2017-02-27", "value": "0.94000" }, "6": { "date": "2017-06-01", "value": "0.94000" }, "5": { "date": "2017-08-30", "value": "0.94000" }, "4": { "date": "2017-11-30", "value": "1.01000" }, "3": { "date": "2018-02-28", "value": "1.01000" }, "2": { "date": "2018-06-01", "value": "1.01000" }, "1": { "date": "2018-08-31", "value": "1.01000" }, "0": { "date": "2018-11-30", "value": "1.16000" } }

let sortedInput = Object.values(data).sort((a,b) => new Date(a.date) - new Date(b.date))

let result = sortedInput.reduce((r, {date, value}, i, a) => {
  let key = date.split('-')[0]
  r[key] = r[key] || []
  r[key].push({date, value})
  return r
}, {})

console.log(result)