Skip to main content
Added ES6 version
Source Link
Rupert
  • 1.6k
  • 12
  • 24

Use Math functions and pluck out the values you want with map.

Here is the jsbin:

https://jsbin.com/necosu/1/edit?js,console

var myArray = [{ "ID": 1, "Cost": 200 }, { "ID": 2, "Cost": 1000 }, { "ID": 3, "Cost": 50 }, { "ID": 4, "Cost": 500 }], min = Math.min.apply(null, myArray.map(function(item) { return item.Cost; })), max = Math.max.apply(null, myArray.map(function(item) { return item.Cost; })); console.log('min', min);//50 console.log('max', max);//1000 

UPDATE:

If you want to use ES6:

var min = Math.min.apply(null, myArray.map(item => item.Cost)), max = Math.max.apply(null, myArray.map(item => item.Cost)); 

Use Math functions and pluck out the values you want with map.

Here is the jsbin:

https://jsbin.com/necosu/1/edit?js,console

var myArray = [{ "ID": 1, "Cost": 200 }, { "ID": 2, "Cost": 1000 }, { "ID": 3, "Cost": 50 }, { "ID": 4, "Cost": 500 }], min = Math.min.apply(null, myArray.map(function(item) { return item.Cost; })), max = Math.max.apply(null, myArray.map(function(item) { return item.Cost; })); console.log('min', min);//50 console.log('max', max);//1000 

Use Math functions and pluck out the values you want with map.

Here is the jsbin:

https://jsbin.com/necosu/1/edit?js,console

var myArray = [{ "ID": 1, "Cost": 200 }, { "ID": 2, "Cost": 1000 }, { "ID": 3, "Cost": 50 }, { "ID": 4, "Cost": 500 }], min = Math.min.apply(null, myArray.map(function(item) { return item.Cost; })), max = Math.max.apply(null, myArray.map(function(item) { return item.Cost; })); console.log('min', min);//50 console.log('max', max);//1000 

UPDATE:

If you want to use ES6:

var min = Math.min.apply(null, myArray.map(item => item.Cost)), max = Math.max.apply(null, myArray.map(item => item.Cost)); 
Source Link
Rupert
  • 1.6k
  • 12
  • 24

Use Math functions and pluck out the values you want with map.

Here is the jsbin:

https://jsbin.com/necosu/1/edit?js,console

var myArray = [{ "ID": 1, "Cost": 200 }, { "ID": 2, "Cost": 1000 }, { "ID": 3, "Cost": 50 }, { "ID": 4, "Cost": 500 }], min = Math.min.apply(null, myArray.map(function(item) { return item.Cost; })), max = Math.max.apply(null, myArray.map(function(item) { return item.Cost; })); console.log('min', min);//50 console.log('max', max);//1000