0

I have an array whose elements are made up of various character (numbers and letters). How could I check whether the first character for a given element is a number? I was trying to use splice/slice but for some reason I was unable to get the solution. I had something like this:

if (!isNaN(array[i].substr(0, 1))) {

  array.slice(0,0); //here I want to remove the element whose first character is a number and not a letter - should I use pop?

}

Can someone please advise? Thanks in advance.

Here is an example:

array[0] ="adkfjdkjfdkj"
array[1] = "12fjkdjfkd"
array[2] = "kcnvmcvn"

So for those elements of array[], I would want to check all three and remove array[1] since the first character is an integer; and then array[2] would become array[1]. I would think a method similar to pop() would be useful here

3
  • Can you show an example, with sample input and expected output? Commented Apr 16, 2015 at 6:06
  • typeof array[0] return number as a string if first element is a number Commented Apr 16, 2015 at 6:09
  • Try with array.splice(0, 1); Commented Apr 16, 2015 at 6:10

5 Answers 5

3

Hey below piece of code will work for you

var myArray = ['1murli', 'krishna', 'cat', 'dog'];
function checkNumber(array)
{
for (var i = 0; i < array.length; i += 1) {
if (!isNaN(array[i].charAt(0))) {

  array.splice(i,1); //removes element which has number at 1st place in array

}
}
console.log(array);
}

checkNumber(myArray);
Sign up to request clarification or add additional context in comments.

Comments

1

The problem with iterating forwards over an array (i.e. from 0 to length) is that if you splice a member, you'll skip the following member (e.g. if you splice index 5, then 6 becomes 5 and the next member you'll visit is the new 6, which was 7, and so on).

To fix that, iterate backwards over the array using a while loop:

var i = arr.length;
var re = /^\d/;
while (i--) {
  if (re.test(arr[i])) arr.splice(i, 1);
}

You could also use ES5 filter, but it creates a new array.

Comments

0

Something like this might work for you.

var dataArray = ["test", "1test", "something", "2something"];

// Loop the characters.
for (index in dataArray) {
    // Get the first character of the current element.
    var character = dataArray[index].substr(0, 1);
    // Output "yes" if it's a number, otherwise "no"
    console.log(isNaN(character) ? 'yes' : 'no');
}

Here's a JSFiddle demonstrating the concept. https://jsfiddle.net/okeujerx/

3 Comments

Can you show how to remove that element whose first character is an integer?
for..in over Array with no hasOwnProperty guard or check for numeric properties is not robust, particularly when polyfills for ES5 methods are likely to abound. ;-)
I didn't see that you were trying to remove the element from your array. That being the case, you should go with @RobG's answer.
0

For forward deletion,you can use this

var array=[];
array[0] ="adkfjdkjfdkj"
array[1] = "12fjkdjfkd"
array[2] = "kcnvmcvn";
array[3] = "kcnvmcvn2";
array[4] = "43kcnvmcvn";
array[5] = "75kcnvmcvn";
array[6] = "cvn";
array[7] = "cvn2";
array[8] = "2cvn";
array[9] = "cvn3";

var indexes=[];
for(var i in array){
    var val  = array[i].charAt(0);
    if(/^\d+$/.test(val)){
        indexes.push(i);
    }
 }
 console.log("Indexes to be removed :"+indexes);
 console.log("Array before:  "+array);

 var count =0;
 for(var i in indexes){
     var indexToBeRemoved = indexes[i]-count;
     array.splice(indexToBeRemoved, 1);
     count++;;
 }

 console.log("Array after:  "+array);

Output

 Indexes to be removed :1,4,5,8
 Array before:  adkfjdkjfdkj,12fjkdjfkd,kcnvmcvn,kcnvmcvn2,43kcnvmcvn,75kcnvmcvn,cvn,cvn2,2cvn,cvn3
 Array after:  adkfjdkjfdkj,kcnvmcvn,kcnvmcvn2,cvn,cvn2,cvn3

Comments

0

Maybe using .filter method could be good idea:

var filtered = array.filter(function(item){
  var re = /^\d/;
  return !re.test(item);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.