使用map函数组合javascript对象

时间:2017-03-18 20:47:23

标签: javascript arrays javascript-objects

我需要在网格上显示一些数据,但格式与我提取的格式不同,并且很难弄清楚如何重构它。我正在抓取一组javascript对象,其中包含订阅长度和价格以及其他详细信息。其他列中的值匹配的所有对象应合并为1个对象。除了订阅长度和价格之外,新对象应具有与旧项目相同的列。这些应该成为新对象的键值对。一个例子:

[
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:3,
price:10
},
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:6,
price:18
},
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:12,
price:32
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:3,
price:8
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:6,
price:15
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:12,
price:28
}
]

会变成:

[
{
col_a:"value",
col_b:"foo",
col_c: true,
3:10,
6:18,
12:32
},
{
col_a:"something",
col_b:"bar",
col_c: false,
3:8,
6:15,
12:28
}
]

我假设我可以使用带有reduce的map函数,但我不知道从哪里开始。感谢任何和所有指针!

3 个答案:

答案 0 :(得分:1)

你走在正确的轨道上;我使用了Array#reduceArray#mapObject.keys

的组合



var data = [{
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 3,
    price: 10
  },
  {
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 6,
    price: 18
  },
  {
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 12,
    price: 32
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 3,
    price: 8
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 6,
    price: 15
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 12,
    price: 28
  }
]

var map = data.reduce(function(map, e) {
  var k = [e.col_a, e.col_b, e.col_c].join()
  if (!map[k]) map[k] = {
    col_a: e.col_a,
    col_b: e.col_b,
    col_c: e.col_c
  }
  map[k][e.subscription_length] = e.price
  return map
}, {})

var result = Object.keys(map).map(function(k) { return this[k] }, map)

console.log(result)




答案 1 :(得分:1)

您可以使用Array.prototype.filter()Array.prototype.reduce()Object.assign()

var data = [{
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 3,
    price: 10
  },
  {
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 6,
    price: 18
  },
  {
    col_a: "value",
    col_b: "foo",
    col_c: true,
    subscription_length: 12,
    price: 32
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 3,
    price: 8
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 6,
    price: 15
  },
  {
    col_a: "something",
    col_b: "bar",
    col_c: false,
    subscription_length: 12,
    price: 28
  }
]

let fn = (arr, prop) => arr.filter(({col_a}) => col_a === prop);

let props = curr => curr.reduce((o, prop) => Object.assign(o, {
  [prop.subscription_length]: prop.price,
  col_a: prop.col_a,
  col_b: prop.col_b,
  col_c: prop.col_c
}), {});

let res = Array.of(props(fn(data, "value")), props(fn(data, "something")));

console.log(res);

答案 2 :(得分:0)

花了一段时间,但我遇到了一些错误。我知道它不如之前的答案好,但它仍然可以正常工作。

var data = [{ col_a: "value", col_b: "foo", col_c: true, subscription_length: 3, price: 10 }, { col_a: "value", col_b: "foo", col_c: true, subscription_length: 6, price: 18 }, { col_a: "value", col_b: "foo", col_c: true, subscription_length: 12, price: 32 }, { col_a: "something", col_b: "bar", col_c: false, subscription_length: 3, price: 8 }, { col_a: "something", col_b: "bar", col_c: false, subscription_length: 6, price: 15 }, { col_a: "something", col_b: "bar", col_c: false, subscription_length: 12, price: 28 }], 
    result = [], 
    ss = [...new Set(data.map(v => v.col_a))];

    ss.forEach(function(v){
      var obj = {};
      obj.col_a = v;
      obj.col_b = data.find(x => x.col_a == v).col_b;
      obj.col_c = data.find(z => z.col_a == v).col_c;
      data.forEach(function(c){
        if (c.col_a == v){
          obj[c.subscription_length] = c.price;
        }
      });
      result.push(obj);
    });
    
    console.log(result);