JavaScript Getting JSON values by order and keeping the corresponding names in order get json values,get_json_object,get_json_object hive,get_json_object spark,get_json_object sql,

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.

Answer:1

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);
Answer:2

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);
Answer:3

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']);
Answer:4

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.

Answer:5

For a very mysterious reason, I have to limit the scope of a css file only to an element: <html> <head> <link rel="stylesheet" type="text/css" href="one.css"> </...

For a very mysterious reason, I have to limit the scope of a css file only to an element: <html> <head> <link rel="stylesheet" type="text/css" href="one.css"> </...

  1. isolate scope angularjs
  2. isolate scope in angularjs directive example
  3. isolate scope in angularjs directive
  4. isolate scope angular
  5. isolate scope directive
  6. angularjs isolate scope
  7. isolate scope definition
  8. isolate scope definition for directive
  9. isolate scope in javascript
  10. isolate scope attribute directive
  11. isolate scope angularjs example
  12. isolate scope in ng-repeat
  13. isolate scope transclude
  14. isolate scope parent
  15. isolate_scope v8
  16. isolate scope in html
  17. isolate scope between directives
  18. ng-isolate-scope
  19. invalid isolate scope definition for directive
  20. ng-isolate-scope css

I'm setting up my model incorrectly here somehow and I can't see it. I've looked at this issue: Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of ...

I'm setting up my model incorrectly here somehow and I can't see it. I've looked at this issue: Node.js - Mongoose path validation failing - TypeError: Cannot call method 'validate' of ...

  1. cannot read property of undefined
  2. cannot read property 'length' of undefined
  3. cannot read property 'setstate' of undefined
  4. cannot read property 'push' of undefined
  5. cannot read property 'map' of undefined
  6. cannot read property 'addeventlistener' of null
  7. cannot read property 'match' of undefined
  8. cannot read property 'then' of undefined
  9. cannot read property '0' of undefined
  10. cannot read property 'props' of undefined
  11. cannot read property 'nativeelement' of undefined
  12. cannot read property 'tolowercase' of undefined
  13. cannot read property 'state' of undefined
  14. cannot read property 'foreach' of undefined
  15. cannot read property 'split' of undefined
  16. cannot read property of null
  17. cannot read property 'replace' of undefined
  18. cannot read property 'summary' of undefined
  19. cannot read property 'createelement' of undefined
  20. cannot read property 'subscribe' of undefined

I am beginning to connect my store to mobx. I wonder the difference between the use of observer(['store'],...) or the use of inject('store')(observer(...)) The main difference I see it that inject is ...

I am beginning to connect my store to mobx. I wonder the difference between the use of observer(['store'],...) or the use of inject('store')(observer(...)) The main difference I see it that inject is ...

  1. what main difference between prokaryotes and eukaryotes
  2. what main difference between cytokinesis in plants and animals
  3. what main difference between c and c++
  4. what main difference between fungi and plants
  5. what main difference between speed and velocity
  6. what main difference between lava and magma
  7. what is main difference between the sat-i and sat-ii
  8. what is main difference between male and female cockroach
  9. what is main difference between the sat-i and sat-ii brainly
  10. what the main difference between judaism and christianity
  11. what the main difference between mitosis and meiosis

Is there a way to add custom font in React Native in Atom editor and not using Xcode?

Is there a way to add custom font in React Native in Atom editor and not using Xcode?

  1. custom font react native
  2. custom font react native 0.60
  3. custom font react native expo
  4. custom font react native android
  5. custom font react native ios
  6. custom font react js
  7. custom font react native 0.61
  8. custom font react
  9. custom font react-native-vector-icons
  10. add custom font react native
  11. use custom font react native
  12. custom font family react native
  13. load custom font react native
  14. add custom font react native ios
  15. add custom font react native android
  16. install custom font react native
  17. import custom font react native
  18. add custom font react
  19. use custom font react
  20. import custom font react