如何在angularjs中转换为多维数组

时间:2018-12-10 13:33:19

标签: javascript jquery angularjs underscore.js lodash

我需要一些帮助将JSON数组转换为多维数组,但我一直陷入困境。

我也在underscore.js中尝试使用_.groupBy,但不适用于嵌套数组。

是否有可能从以下内容进行转换

df[col_name] = pd.to_numeric(df[col_name], errors = 'coerce')
df.drop_duplicates(keep="first",inplace=True)

预期结果应该是

[{
  "subject": "physics",
  "student_id": "2569",
  "values": "0.152,0.228,0.218"
}, {
  "subject": "maths",
  "student_id": "1236",
  "values": "0.146,0.22,0.212"
}, {
  "subject": "chemistry",
  "student_id": "4569",
  "values": "0.159,0.234,0.224"
}, {
  "subject": "physics",
  "student_id": "1478",
  "values": "0.16,0.235,0.225"
}]

请为我提供一个简单的解决方案。

4 个答案:

答案 0 :(得分:4)

好像您想将数组更改为对象。 Array.prototype.reduce是您的朋友。

const list = [{
  "subject": "physics",
  "student_id": "2569",
  "values": "0.152,0.228,0.218"
}, {
  "subject": "maths",
  "student_id": "1236",
  "values": "0.146,0.22,0.212"
}, {
  "subject": "chemistry",
  "student_id": "4569",
  "values": "0.159,0.234,0.224"
}, {
  "subject": "physics",
  "student_id": "1478",
  "values": "0.16,0.235,0.225"
}]

const data = list.reduce((reduction, item) => {
  if (reduction[item.subject]) {
    reduction[item.subject][item.student_id] = item.values 
  } else {
    reduction[item.subject] = {
      [item.student_id]: item.values 
    }
  }
  return reduction
}, {})
console.log(data)

答案 1 :(得分:0)

您可以将lodash的_.setWith()Array.reduce()配合使用(强制为数字键创建对象):

const data = [{"subject":"physics","student_id":"2569","values":"0.152,0.228,0.218"},{"subject":"maths","student_id":"1236","values":"0.146,0.22,0.212"},{"subject":"chemistry","student_id":"4569","values":"0.159,0.234,0.224"},{"subject":"physics","student_id":"1478","values":"0.16,0.235,0.225"}];

const fn = arr =>
  arr.reduce((r, o) => 
    _.setWith(r, [o.subject, o.student_id], o.values, Object)
  , {});

const result = fn(data);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 2 :(得分:0)

const list = [{
  "subject": "physics",
  "student_id": "2569",
  "values": "0.152,0.228,0.218"
}, {
  "subject": "mathss",
  "student_id": "1236",
  "values": "0.146,0.22,0.212"
}, {
  "subject": "chemistry",
  "student_id": "4569",
  "values": "0.159,0.234,0.224"
}, {
  "subject": "physics",
  "student_id": "1478",
  "values": "0.16,0.235,0.225"
}]
reduction  = {}
list.map((a, b) => {
	if(reduction[a.subject])
		reduction[a.subject][a.student_id] = a.values;
	else
		reduction[a.subject] = {[a.student_id] : a.values};
	return reduction;
})
console.log(reduction);

答案 3 :(得分:0)

您可以通过Array.reduce和一些 ES6 解构以简洁的方式解决此问题:

const data = [{ "subject": "physics", "student_id": "2569", "values": "0.152,0.228,0.218" }, { "subject": "maths", "student_id": "1236", "values": "0.146,0.22,0.212" }, { "subject": "chemistry", "student_id": "4569", "values": "0.159,0.234,0.224" }, { "subject": "physics", "student_id": "1478", "values": "0.16,0.235,0.225" }]

const result = data.reduce((a, {subject, student_id, values}) => {
  a[subject] = a[subject] || {}
  a[subject][student_id] = values
  return a
}, {})

console.log(result)