0

i had seen lots of another examples like

Math.max(...Array1) or Math.max(null,num) or Math.max.apply(null,num)
but it's not working by my code
my data size is 255 and This is what the data looks like when i print it by console.log

0: 55.47999954223633
1: 56.040000915527344
2: 57.52000045776367
3: 57.119998931884766
...

Data was extracted from the json file and then put into the array through push.
code is look like this

let Array =[]
jQuery.getJSON( "price.json",function(data){
        for(let i=0;i<data.length;i++){
             Array.push(data[i].price)
        }    
let maximum = Math.max(...Array) // not working 

Thank you for reading this.
J.K
  • 65
  • 7
  • It does work, trying putting `Math.max(...[55.47999954223633, 56.040000915527344, 57.52000045776367, 57.119998931884766])` in your console. Check your data is what you expect it to be. Also don't call something `Array`, that's a reserved word and not a good name anyway, `prices` would be better – Dominic Dec 09 '22 at 02:01
  • 1
    Does this answer your question? [Find the min/max element of an array in JavaScript](https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript) – ace1234 Dec 09 '22 at 02:01
  • @ace1234 i tried that one before, but it's not working – J.K Dec 09 '22 at 02:02
  • @Dominic not like Math.max(...array)? – J.K Dec 09 '22 at 02:04
  • Math.max(...array) is correct, I am just creating an array inside the parentheses i.e. `Math.max(...[1, 6, 3])` – Dominic Dec 09 '22 at 02:08
  • @Dominic as i tried it works fine with that short data but as i said, it's not working with my data, i mean array size 255 (length:255 array) – J.K Dec 09 '22 at 02:10
  • @J.K plz upload `price.json` somewhere, it should work with a larger array too – Dominic Dec 09 '22 at 03:19
  • Your array might have too many elements for jamming them all into Math.max at once. See the reduce example [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max) – James Dec 09 '22 at 03:30
  • What's the actual error? Or output if there is no error. It's not very clear what "not working" means. – LawrenceWebDev Dec 09 '22 at 03:55
  • @LawrenceWebDev there is no error just result with infinity – J.K Dec 09 '22 at 04:04
  • @Dominic just a second as i check my data i mean after put data in array, and check the length, it print out 0 any reason? – J.K Dec 09 '22 at 04:26
  • @J.K depends what is in `prices.json`, I imagine it's an object with an array in it or something – Dominic Dec 09 '22 at 04:43
  • @Dominic exactly. like example: products number, price etc – J.K Dec 09 '22 at 05:16

1 Answers1

0

Math.max(...[]) is ES6 syntax. Maybe you are using an older JavaScript engine? Here are two versions using your data as input, one for newer ES6, one for older ES5:

const dataFromJson = [
  { name: "A", price: 55.47999954223633 },
  { name: "A", price: 56.040000915527344 },
  { name: "A", price: 57.52000045776367 },
  { name: "A", price: 57.119998931884766 }
];

// ES6:
let arr1 = dataFromJson.map(obj => obj.price);
let max1 = Math.max(...arr1);
console.log('ES6 max: ' + max1);

// ES5:
let arr2 = dataFromJson.map(function(obj) {
  return obj.price;
});
let max2 = Math.max.apply(null, arr2);
console.log('ES5 max: ' + max2);

Output:

ES6 max: 57.52000045776367
ES5 max: 57.52000045776367
Peter Thoeny
  • 7,379
  • 1
  • 10
  • 20
  • actually when i use Math.max.apply or that similar one , it print out with :- Infinity – J.K Dec 09 '22 at 02:34
  • You get infinity if the first parameter to `Math.max.apply` is the array, hence `Math.max.apply(null, array)`. See docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply – Peter Thoeny Dec 09 '22 at 02:42
  • as i tried that Math.max.apply(null, array) , it also return infinity – J.K Dec 09 '22 at 02:56
  • Try `Math.max.apply(null, [1, 3, 2])` to test – Peter Thoeny Dec 09 '22 at 03:33
  • seems like my array is wrong i mean after trying to push data and check the length, it said 0 idk why and what u suggest Math.max.apply(null,[1,3,2] is work fine – J.K Dec 09 '22 at 04:29
  • Yup, do a console print of the data you get to find out. – Peter Thoeny Dec 09 '22 at 06:10
  • can i ask u something? as i get data from jQuery.getJSON and push in array, and after end of jQuery.getJSON , i can't get data from that array, do u know why it is? – J.K Dec 09 '22 at 10:29
  • This is a new question. Best to open a new question, and specify input and expected output. – Peter Thoeny Dec 09 '22 at 19:28