Get max and min value from array in JavaScript

RJB :

I am creating the following array from data attributes and I need to be able to grab the highest and lowest value from it so I can pass it to another function later on.

var allProducts = $(products).children("li");
prices = []
$(allProducts).each(function () {
    var price = parseFloat($(this).data('price'));
    prices[price] = price;
});
console.log(prices[0]) <!-- this returns undefined

My list items look like this (I have cut down for readability):

<li data-price="29.97"><a href="#">Product</a></li>
<li data-price="31.00"><a href="#">Product</a></li>
<li data-price="19.38"><a href="#">Product</a></li>
<li data-price="20.00"><a href="#">Product</a></li>

A quick console.log on prices shows me my array which appears to be sorted so I could grab the first and last element I assume, but presently the names and values in the array are the same so whenever I try and do a prices[0], I get undefined

[]
19.38   19.38
20.00   20.00
29.97   29.97
31.00   31.00

Got a feeling this is a stupidly easy question, so please be kind :)

Serg Hospodarets :

To get min/max value in array, you can use:

var _array = [1,3,2];
Math.max.apply(Math,_array); // 3
Math.min.apply(Math,_array); // 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get max and min value from Array in Javascript?

Get min and max value of an array of strings in Javascript

PHP - Get min() / max() value from array

Get max / min from array of strings (javascript)

Get min/max value from array based on condition

Get min and max group value from multi dimensional array in php

Getting the min and max value in JavaScript, but from a 2D array

How to get max and min dates from array using javascript/jquery?

How to get the max/min value of a String[] array?

Compare JavaScript Array of Objects to Get Min / Max

Get max and min value from a geojson

Get the MAX datetime value from the MIN date

Get max and min dates from an array

Get Min and Max label in from the array loop

get max an min values from php array

VBA get min/max from multidimensional array

Max and Min value in array

Javascript get object from array having min value and min value is needs to find and then find object

Get max value from multidimensional array using Javascript

React/Javascript get max value and respective label from array and render

how to get min and max value from Array of Model class excluding zero

min and max value from array, gets always the same value

Get MAX value from one column and MIN from another column

Get min / max value of a static constexpr array at compile time

get min and max position in array where value exist

BitGO - Get MIN value from Array of Objects

jq select record from array with min or max value of field

How can i get the max and min intensity value from an image?

Can you get a different column from a row with a MIN or MAX value?