Array Input as
var a =[1,'a',2,3,'e',4,'r'];
I have tried to convert this array to string and by checking the length of the string as below
I want to convert the array to
['a','e','r',1,2,3,4]
Is this a correct way of doing?
var a = [1, 'a', 2, 3, 'e', 4, 'r'];
var b = a.toString();
console.log(b);
var num, alpha;
for (var i = 0; i < b.length; i++) {
var letters = /[A-Za-z]+$/;
var numbers = /[0-9]/;
if (b.charAt(i).match(numbers))
num.append(b.charAt(i));
else if (b.charAt(i).match(letters))
alpha.append(b.charAt(i));
}
console.log(alpha);
console.log(num);
You could sort the array by taking the delta of the check for number.
var array = [1, 'a', 2, 3, 'e', 4, 'r'];
array.sort((a, b) => (typeof a === 'number') - (typeof b === 'number'));
console.log(array);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With