Reputation: 1787
I currently have some JSON data like the following:
{"data": [
{"student_id": "bobby", "solved": 19},
{"student_id": "smith", "solved": 20}
]}
Now what I am doing right now is pushing the solved values into an array called newData
in a for loop like the following:
newData.push(data['data'][i].solved);
Now I sort these by values by using this function:
newData.sort(function(a,b){
return a-b;
});
Great, now I have an array of values in order. But how can I retrieve the names which correspond to these values which I sorted in order? It would be nice if I could have the names and solved in two arrays, but each index corresponds to the same in the original JSON data. One huge array with objects and values sorted is ok too I guess.
Upvotes: 2
Views: 67
Reputation: 2796
With small reusable one-liners
const {data} = {"data": [
{"student_id": "bobby", "solved": 19},
{"student_id": "smith", "solved": 20}
]};
const sortBy = (by,arr) => arr.slice().sort((a,b) => b[by] - a[by]);
const getProp = (prop,obj) => obj[prop];
const sortById = sortBy.bind(null, 'student_id');
const sortBySolved = sortBy.bind(null, 'solved');
const getId = getProp.bind(null, 'student_id');
const getSolved = getProp.bind(null, 'solved');
const sortedIds = sortById(data).map(getId);
const sortedSolutions = sortBySolved(data).map(getSolved);
console.log(sortedIds);
console.log(sortedSolutions);
Upvotes: 1
Reputation: 122087
You can first sort data by solved
key and then use map()
to return two separate arrays of results in sorted order.
var obj = {"data": [{"student_id": "bobby", "solved": 19}, {"student_id": "smith", "solved": 20}]}
var newData = obj.data.sort(function(a, b) {
return b.solved - a.solved;
});
var numbers = newData.map(e => e.solved);
var names = newData.map(e => e.student_id);
console.log(numbers);
console.log(names);
Upvotes: 2
Reputation: 1123
To keep all the data sorted and kept in a single array, you could create an object and put that into the array by doing something like this in the for loop:
newData.push({
student_id: data['data'][i].student_id,
solved: data['data'][i].solved
});
And when you go to sort it, you just need to change the sort function to do:
newData.sort(function(a,b){
return a.solved - b.solved;
});
When you go to access the array later instead of using, for example, newData[3]
, just change the line to newData[3].solved
or newData[3]['solved']
.
Keeping your data in a single array like this will help make it easier to keep your student_id
and solved
values paired with each other. When you use multiple arrays, it is easier to have your indices mismatch with each other. But there's nothing stopping you from using more than one.
Upvotes: 1
Reputation: 9365
You can also use "lodash" or "underscore" to sort your data by "solved", in just one line of code:
var data = {"data": [{"student_id": "bobby", "solved": 19}, {"student_id": "smith", "solved": 20}]}
var sorted = _.sortBy(data["data"],['solved']);
see https://lodash.com/docs#sortBy
Upvotes: 1
Reputation: 22911
Why don't you just sort the actual objects?
json.data.sort(function(a, b) {
return a.solved - b.solved;
});
Now you have a sorted array of objects. If you want the lowest student, just use sorted[0].student_id
. You can see how this works below:
var obj = {"data": [{"student_id": "bobby", "solved": 19}, {"student_id": "smith", "solved": 18}]};
console.log('Raw Data: ', obj.data);
var sorted = obj.data.sort(function(a,b){
return a.solved-b.solved;
});
console.log('Sorted: ', sorted);
Upvotes: 1