我遇到了这样的问题,对象数组被故意放入这种结构中:
const productArray = [
{ 'Product A': 5000 },
{ 'Product B': 2500 },
{ 'Product C': 3500 },
{ 'Product D': 2750 },
{ 'Product E': 1500 },
];
如何确定这类数组的最大值和最小值?非常感谢。
对数组进行排序,最小的在最前面,最大的在最后
const productArray = [
{ 'Product A': 5000 },
{ 'Product B': 2500 },
{ 'Product C': 3500 },
{ 'Product D': 2750 },
{ 'Product E': 1500 },
];
const sortedProduct = productArray.sort((a, b) => Object.values(a)[0] - Object.values(b)[0]);
console.log(sortedProduct);
console.log('Lowest value ', sortedProduct[0]);
console.log('Highest value ', sortedProduct[sortedProduct.length - 1]);