如何转换阵列货币字符串 [英] How to convert currency Strings from Array

查看:119
本文介绍了如何转换阵列货币字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有保存的数组列表的阵列和值是'美元'。
我需要采取最高价格和最低价格从该列表。

I have an array which holds a list of array and the value is in 'dollars'. I need to take the max price and min price from that list.

这是什么都试过了,

var _array = [$1.09,$3.07,$2.223];
var number = Number(_array.replace(/[^0-9\.]+/g,""));
a= Math.max.apply(Math,number); // $3
b= Math.min.apply(Math,number); // $1

但它没有工作,任何人都可以请帮助我。

But its not working, can anyone please help me out.

演示链接

推荐答案

问题是你有你的阵列中的非法字符。让他们像串在包装。

Problem is you are having illegal characters in your array. Have them like strings by wrapping in ''.

做到像波纹管

var _array = ['$1.09','$3.07','$2.223'];
var numbers = _array.map(function(curr){
    return Number(curr.replace(/[^0-9\.]+/g,""))
})
a= Math.max.apply(Math,numbers); // 3
b= Math.min.apply(Math,numbers); // 1
console.log(a,b)

DEMO

注: - .MAP 功能将无法在IE8工作,所以你可以使用循环出现。像波纹管

NOTE:- The .map function won't work on IE8 so you can use for loop there. Like bellow

var _array = ['$1.09','$3.07','$2.223'];
var numbers = [];

for(var i=0;i<_array.length;i++){
    numbers.push(_array[i].replace(/[^0-9\.]+/g,""));
}

a= Math.max.apply(Math,numbers); // 3
b= Math.min.apply(Math,numbers); // 1
console.log(a,b)

DEMO

这篇关于如何转换阵列货币字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆