如何在JavaScript中透视数组对象?

时间:2016-11-10 08:44:50

标签: javascript arrays

我有一个对象数组,我需要转换为一个表(一种旋转数据)。说这个我需要获得另一个具有唯一标题的对象数组,这些标题具有嵌套的对象数组和值对。您能否请一看这个并帮助我实现我需要的东西?请参阅下面的原始和所需数组:

原始阵列:

[
{title: "Title1", value1: "value1", value2: "value2"},
{title: "Title2", value1: "value1", value2: "value2"},
{title: "Title1", value1: "value1", value2: "value2"},
{title: "Title3", value1: "value1", value2: "value2"},
{title: "Title2", value1: "value1", value2: "value2"},
{title: "Title1", value1: "value1", value2: "value2"},
{title: "Title3", value1: "value1", value2: "value2"},
{title: "Title1", value1: "value1", value2: "value2"},
]

期望的结果:

[
{title: "Title1", values: [{value1: "value1"}, {value2: "value2"}]},
{title: "Title2", values: [{value1: "value1"}, {value2: "value2"}]},
{title: "Title3", values: [{value1: "value1"}, {value2: "value2"}]},
]

如果有任何建议,请先谢谢。

3 个答案:

答案 0 :(得分:2)

简单的数组减少可能有帮助

let data = [{
  title: "Title1",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title2",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title1",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title3",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title2",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title1",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title3",
  value1: "value1",
  value2: "value2"
}, {
  title: "Title1",
  value1: "value1",
  value2: "value2"
}];

let pivoted = data.reduce((prev, cur) => {
  let existing = prev.find(x => x.title === cur.title);

  if (existing)
    existing.values.push(cur)
  else
    prev.push({
      title: cur.title,
      values: [cur]
    });

  return prev;
}, []);

console.log(pivoted);

API已使用

Reduce

Find

然后可以根据您想要的任何字段调整它,如果该字段不存在则需要添加错误处理:

let pivotBy = (key, data) => data.reduce((prev, cur) => {
  let existing = prev.find(x => x[key] === cur[key]);

  if (existing) {
    existing.values.push(cur);
  } else {
    let toInsert = {
        values: [cur]
    }

    toInsert[key] = cur[key];

    prev.push(toInsert);
  }

  return prev;
}, []);

修改 我误解了您想要的输出格式,但可以修改reduce方法以满足该要求。

答案 1 :(得分:1)

您可以使用哈希表将thisArg分组为Array#forEach



var data = [{ title: "Title1", value1: "value1", value2: "value2" }, { title: "Title2", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }, { title: "Title3", value1: "value1", value2: "value2" }, { title: "Title2", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }, { title: "Title3", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }],
    grouped = [];

data.forEach(function (a) {

    // check if title is not in hash table
    if (!this[a.title]) {

        // if not, create new object with title and values array
        // and assign it with the title as hash to the hash table
        this[a.title] = { title: a.title, values: [] };

        // add the new object to the result set, too
        grouped.push(this[a.title]);
    }

    // create a new object with the other values and push it
    // to the array of the object of the hash table
    this[a.title].values.push({ value1: a.value1, value2: a.value2 });
}, Object.create(null)); // Object.create creates an empty object without prototypes

console.log(grouped);

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




答案 2 :(得分:1)

    var originalArray = [
      {title: "Title1", value1: "value1", value2: "value2"},
      {title: "Title2", value1: "value1", value2: "value2"},
      {title: "Title1", value1: "value1", value2: "value2"},
      {title: "Title3", value1: "value1", value2: "value2"},
      {title: "Title2", value1: "value1", value2: "value2"},
      {title: "Title1", value1: "value1", value2: "value2"},
      {title: "Title3", value1: "value1", value2: "value2"},
      {title: "Title1", value1: "value1", value2: "value2"},
    ];
    var newArray = [];

    for(var i = 0; i<originalArray.length; i++){
      var object = {};
      object.title=originalArray[i].title;
      var values = [];
      values.push({value1: originalArray[i].value1});
      values.push({value2: originalArray[i].value2});
      object.values= values;
      newArray.push(object);
    }

这会有效,但老实说,我不知道你为什么会有一些重复的琐事。我认为这只是一个例子。如果没有,答案是另一个。