合并基于

时间:2017-02-28 13:58:06

标签: javascript jquery json sorting grouping

我无法找到解决方案:

我想基于一列(日期分组 json数组,并使用 Javascript / Jquery ? :

我一直在努力寻找解决方案,但我无法弄明白。

[
    {
    "date" : "2010-01-01",
    "price" : 30
    },
    {
    "date" : "2010-02-01",
    "price" : 40
    },
    {
    "date" : "2010-03-01",
    "price" : 50
    },
    {
    "date" : "2010-01-01",
    "price2" : 45
    },
    {
    "date" : "2010-05-01",
    "price2" : 40
    },
    {
    "date" : "2010-10-01",
    "price2" : 50
    }
]

我想要这个:

[
    {
        "date" : "2010-01-01",
        "price" : 30,
        "price2" : 45
    },
    {
        "date" : "2010-02-01",
        "price" : 40,
        "price2" : null
    },
    {
        "date" : "2010-03-01",
        "price" : 50,
        "price2" : null
    },
    {
        "date" : "2010-05-01",
        "price" : null,
        "price2" : 40
    },
    {
        "date" : "2010-10-01",
        "price" : null
        "price2" : 50
    }
]

提前谢谢。

4 个答案:

答案 0 :(得分:1)

您可以先使用sort()方法按日期对元素进行排序,然后使用forEach()按日期对元素进行分组。



var data = [{"date":"2010-01-01","price":30},{"date":"2010-02-01","price":40},{"date":"2010-03-01","price":50},{"date":"2010-01-01","price2":45},{"date":"2010-05-01","price2":40},{"date":"2010-10-01","price2":50}]
data.sort((a, b) => new Date(a.date) - new Date(b.date))

var result = []
data.forEach(function(e) {
  if(!this[e.date]) {
    this[e.date] = {date: e.date, price: null, price2: null}
    result.push(this[e.date])
  }
  this[e.date] = Object.assign(this[e.date], e)
})

console.log(JSON.stringify(result, 0, 4))




答案 1 :(得分:0)

使用Array.reduce()Array.find()

var prices = [{"date":"2010-01-01","price":30},{"date":"2010-02-01","price":40},{"date":"2010-03-01","price":50},{"date":"2010-01-01","price2":45},{"date":"2010-05-01","price2":40},{"date":"2010-10-01","price2":50}];

var prices2 = prices.reduce(function(acc, val) {
  var dateElement = acc.find(function(element) {
    return val.date === element.date;
  });

  if (!dateElement) {
    dateElement = {
      date: val.date,
      price: null,
      price2: null,
    };
    acc.push(dateElement);
  }
  dateElement.price = dateElement.price || val.price;
  dateElement.price2 = dateElement.price2 || val.price2;

  return acc;
}, []);

console.log(prices2);

答案 2 :(得分:0)

您可以尝试Array.reduceArray.map循环并Object.assign合并

var data = [{"date":"2010-01-01","price":30},{"date":"2010-02-01","price":40},{"date":"2010-03-01","price":50},{"date":"2010-01-01","price2":45},{"date":"2010-05-01","price2":40},{"date":"2010-10-01","price2":50}]

var result = data.reduce((p, c) => 
  (p[c.date] = Object.assign({},{price: null, price2:null}, p[c.date], c)) && p
  , {});

var final = Object.keys(result).map(x=>result[x])

console.log(final)

答案 3 :(得分:0)

您可以使用对象作为同一日期的参考。

var data = [{ date: "2010-01-01", price: 30 }, { date: "2010-02-01", price: 40 }, { date: "2010-03-01", price: 50 }, { date: "2010-01-01", price2: 45 }, { date: "2010-05-01", price2: 40 }, { date: "2010-10-01", price2: 50 }],
    grouped = [];

data.forEach(function (hash) {
    return function (o) {
        if (!hash[o.date]) {
            hash[o.date] = { date: o.date, price: null, price2: null };
            grouped.push(hash[o.date]);
        }
        Object.keys(o).forEach(function (k) {
            if (k === 'date') {
                return;
            }
            hash[o.date][k] = o[k];
        });
    };
}(Object.create(null)));

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