1

I am using asp.net mvc framework and i have json data which i want to in 2d array. Here is my data.

Json data(Input data)

var data=[{"ID":1,"Min":80,"Max":175},{"ID":2,"Min":90,"Max":240},{"ID":3,"Min":90,"Max":215},{"ID":4,"Min":50,"Max":120},{"ID":5,"Min":70,"Max":190},{"ID":6,"Min":50,"Max":120},{"ID":7,"Min":70,"Max":140},{"ID":8,"Min":80,"Max":160}];

I need to get Min and Max values from Json data.

Required output:

data=[[80,175],[90,240],[90,215],[50,120],[70,190],[50,120],[70,140],[80,160]]

Waiting for your response

4
  • Just a note, there are some SO Elite's, that will hammer you down for saying that's JSON data. It's not, it's a javascript object, if you left out the var data= and the trailing ;, then I would say it's JSON. :) Commented Oct 27, 2016 at 11:49
  • json is a string. always. Commented Oct 27, 2016 at 11:50
  • Indeed, take out the bits I just said, and it's a string. Commented Oct 27, 2016 at 11:54
  • @PrateekJairath Could you please respond to the answers below? Thanks! Commented Oct 30, 2016 at 2:19

4 Answers 4

1

You can do this with map()

var data=[{"ID":1,"Min":80,"Max":175},{"ID":2,"Min":90,"Max":240},{"ID":3,"Min":90,"Max":215},{"ID":4,"Min":50,"Max":120},{"ID":5,"Min":70,"Max":190},{"ID":6,"Min":50,"Max":120},{"ID":7,"Min":70,"Max":140},{"ID":8,"Min":80,"Max":160}];

var result = data.map(function(e) {
  return [e.Min, e.Max];
})

console.log(result)

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

Comments

0

you can use map (http://api.jquery.com/jquery.map/ )

var o = {"0":"1","1":"2","2":"3","3":"4"};

var arr = Object.keys(o).map(function(k) { return o[k] });

console.log(arr)

here an example on fiddle http://jsfiddle.net/8TT4p/67/

Comments

0

Another solution using Array.prototype.reduce to create the resulting array of arrays:

var data=[{"ID":1,"Min":80,"Max":175},{"ID":2,"Min":90,"Max":240},{"ID":3,"Min":90,"Max":215},{"ID":4,"Min":50,"Max":120},{"ID":5,"Min":70,"Max":190},{"ID":6,"Min":50,"Max":120},{"ID":7,"Min":70,"Max":140},{"ID":8,"Min":80,"Max":160}];

var result = data.reduce(function(prev, curr) {
  prev.push([curr.Min, curr.Max]);
  return prev;
}, []);

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

Comments

0
var data=[{"ID":1,"Min":80,"Max":175},{"ID":2,"Min":90,"Max":240},{"ID":3,"Min":90,"Max":215},{"ID":4,"Min":50,"Max":120},{"ID":5,"Min":70,"Max":190},{"ID":6,"Min":50,"Max":120},{"ID":7,"Min":70,"Max":140},{"ID":8,"Min":80,"Max":160}];

var data1 = [];
for(var i=0;i<data.length;i++){
  var temp = [];
  temp.push(data[i].Min);
  temp.push(data[i].Max);
  data1.push(temp);
}

Now data1 is your required array.

Comments

Your Answer

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