简体   繁体   中英

Checking First Character of Array Element

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

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);

The problem with iterating forwards over an array (ie from 0 to length) is that if you splice a member, you'll skip the following member (eg 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.

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/

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

Maybe using .filter method could be good idea:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM