This is an array,
total = [\"10%\", 1000, \"5%\", 2000] . how can i filter these into two array like, percentage = [\"10%\",\"5%\"] and absolute = [100
you can do it using
let total = ["10%", 1000, "5%", 2000]
let result1 = total.filter(function(element){
if(typeof element == "number"){
return 1;
}
return 0;
})
let result2 = total.filter(function(element){
if(typeof element == "string" && element.match(/\%$/)){
return 1;
}
return 0;
})
console.log(result1, result2);
the first array filter all the number
the 2nd array filter elements which are a string and have a "%" in the end