重组Javascript数组

时间:2019-03-14 19:57:18

标签: javascript arrays

编辑:感谢您的答复!

我对使用Java有点陌生(很抱歉,可能是错误的代码)。我主要只使用HTML和CSS。我一直在尝试使用Javascript,但是一直以来我一直坚持下去。结果不是我想要的那样。

我的代码:

我想通过将值放在同一类别下来使数组更易于使用,但是我的代码输出并不完全符合我的期望。

var data = {
  idName: "idName",
  valueRanges: [{
    range: "range",
    majorDimension: "ROWS",
    values: [
      [
        "ID",
        "Category",
        "Name"
      ],
      [
        "1",
        "Category1",
        "Name1"
      ],
      [
        "2",
        "Category1",
        "Name2"
      ],
      [
        "3",
        "Category2",
        "Name3"
      ],
      [
        "4",
        "Category1",
        "Name4"
      ]
    ]
  }]
}

var rows = [];

let batchRowValues = data.valueRanges[0].values
for (let i = 1; i < batchRowValues.length; i++) {
  let rowObject = {};
  for (let j = 0; j < batchRowValues[i].length; j++) {
    rowObject[batchRowValues[0][j]] = batchRowValues[i][j];
  }
  rows.push(rowObject);
}

var newArray = rows.reduce(function(acc, curr) {
  var findIfNameExist = acc.findIndex(function(item) {
    return item.Category === curr.Category;
  })
  if (findIfNameExist === -1) {
    let obj = {
      'Category': curr.Category,
      'value': [curr]
    }
    acc.push(obj)
  } else {
    acc[findIfNameExist].value.push(curr)
  }
  return acc;
}, []);

console.log('------------')
console.log('input: ' + JSON.stringify(data, null, 2))
console.log('------------')
console.log('output: ' + JSON.stringify(newArray, null, 2))

我的代码输出:

[
  {
    Category: "Category1",
    value: [
      {
        Category: "Category1",
        ID: "1",
        Name: "Name1"
      }, 
      {
        Category: "Category1",
        ID: "2",
        Name: "Name2"
      },
      {
        Category: "Category1",
        ID: "4",
        Name: "Name4"
      }
    ]
  },
  {
    Category: "Category2",
    value: [
      {
        Category: "Category2",
        ID: "3",
        Name: "Name3"
      }
    ]
  }
]

我希望它的外观:

[
  {
    Category: "Category1",
    values: [
      {
        ID: "1",
        Name: "Name1"
      },
      {
        ID: "2",
        Name: "Name2"
      },
      {
        ID: "4",
        Name: "Name4"
      }
    ]
  },
  {
    Category: "Category2",
    values: [
      {
        ID: "3",
        Name: "Name3"
      },
    ]
  },
]

我想学习!感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用reduce。

这里的想法是

  • 创建对象并将类别用作键
  • 如果已找到类别,则将所需的对象推送到其value属性中。如果不是这样,我们将使用合适的数据创建一个新的数据。

我在这里直接使用对象而不是数组是因为我可以使用键直接访问元素,就像每次在数组中我都需要循环遍历并检查值的存在

var data = {idName: "idName",valueRanges: [{range: "range",majorDimension: "ROWS",values: [["ID","Category","Name"],["1","Category1","Name1"],["2","Category1","Name2"],["3","Category2","Name3"],["4","Category1","Name4"]]}]}

var rows = [];
let batchRowValues = data.valueRanges[0].values.slice(1,)

let op = batchRowValues.reduce((op,[ID,Category,Name]) => {
  if( op[Category] ){
    op[Category].value.push({ID,Name})
  } else {
    op[Category] = {
      Category,
      value: [{ID,Name}]
    }
  }
  return op
},{})

console.log(Object.values(op))

答案 1 :(得分:-1)

尝试(t = {},得到r)

data.valueRanges[0].values.slice(1).map( ([i,c,n])=> 
    (t[c]=t[c]||{Category:c,values:[]}, t[c].values.push({ID:i, Name:n})) );
let r= Object.values(t);

var data = 
  {
    idName: "idName",
    valueRanges: [
      {
        range: "range",
        majorDimension: "ROWS",
        values: [
          [
            "ID",
            "Category",
            "Name"
          ],
          [
            "1",
            "Category1",
            "Name1"
          ],
          [
            "2",
            "Category1",
            "Name2"
          ],
          [
            "3",
            "Category2",
            "Name3"
          ],
          [
            "4",
            "Category1",
            "Name4"
          ]
        ]
      }
    ]
  }
 
let t={};
data.valueRanges[0].values.slice(1).map( ([i,c,n])=> 
    (t[c]=t[c]||{Category:c,values:[]}, t[c].values.push({ID:i, Name:n})) );
let r= Object.values(t);

console.log(r);