How to filter an array in javascript?

前端 未结 7 1655
清酒与你
清酒与你 2020-11-28 16:38

This is an array,

total = [\"10%\", 1000, \"5%\", 2000] . how can i filter these into two array like, percentage = [\"10%\",\"5%\"] and absolute = [100

7条回答
  •  眼角桃花
    2020-11-28 17:31

    You can use regular expressions since you have only strings in your array.

    For % :

    total.filter(function(element){
        return /^[0-9]+\%$/gi.test(element);
    });
    

    For absolute :

    total.filter(function(element){
        return /^[0-9]+$/gi.test(element);
    });
    

提交回复
热议问题