1

I need some help to convert JSON array to multidimensional array, I keep getting stuck.

I am also tried with _.groupBy in underscore.js but it's not working for the nested array.

If any possible to convert from the following

[{
  "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"
}]

Expected Result should be

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

Please provide me with a simple solution.

4 Answers 4

4

Looks like you want to change an array into an object. Array.prototype.reduce is your friend for this.

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)

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

0

You can solve this in a concise way via Array.reduce and some ES6 destructuring:

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)

Comments

0

You can use lodash's _.setWith() (to force object creation for numerical keys) with 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.