4

I Have an array say

['(','C','1','2',')']

Now I want to trim data from array beginning from indexOf('C') + 2 ,If it is a digit I need to remove it from array.. So for the above example final array has to be

 ['(','C','1',')']

For example if I have ['(','C','1','2','3','*',')'] I want it to be trimmed to ['(','C','1','*',')'] , After element 'C' only one numeral is allowed.

I know I can traverse the array by getting the indexOf('C') and then checking each element for numeric.. but help me with some efficient and better way. like using splice or something.

000
  • 26,951
  • 10
  • 71
  • 101
Garry
  • 306
  • 1
  • 7
  • 24
  • Sounds like you're looking for [**Array.slice()**](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice) ?? – adeneo Mar 25 '13 at 05:35
  • 4
    Traversing the array is not anymore inefficient than using `.splice()` or something.... Since you would have to have used `.indexOf()` to get the indices to do the *splice*. Another method is *joining* the array back into a *String* and performing **regex** matching. – Amy Mar 25 '13 at 05:36
  • @sweetamylase—joining and using a regular expression may work in trivial cases (per the OP), but where the data is a random string it's very difficult. Regular expressions aren't good for parsing irregular data (even fairly ordered irregular data, like markup). – RobG Mar 25 '13 at 05:53

3 Answers3

3

If the position from where you want to 'trim' is known, you could use filter here, like.:

var a = ['(','C','1','2','3','*',')']
        .filter( function(a){
                   this.x += 1; 
                   return this.x<=2 ? a : isNaN(+a);}, {x:-1} 
         );

Which could lead to this Array extension:

Array.prototype.trimNumbersFrom = function(n){ 
    return this.filter( function(a){
                         this.x += 1; 
                         return this.x<=n ? a : isNaN(+a);
                        }, {x:-1} 
           );
};
//=> usage
var a = ['(','C','1','2','3','*',')'].trimNumbersFrom(2);
    //=> ["(", "C", "1", "*", ")"]

See also ...

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • i tried Array.prototype.trimNumbersFrom but its not workin fine for just one case ... ["(", "C", "1", "0"] ... – Garry Mar 25 '13 at 10:49
  • Yep, `!+a` returns `true` for '0'. `IsNaN(+a)` doesn"t, edited my answer. – KooiInc Mar 25 '13 at 11:02
2
var a = ['(','C', 'a', '8','2',')'].join("").split("C");
var nPos = a[1].search(/[0-9]/g);
var firstNumber = a[1][nPos];
var b = a[1].split(n);

// rebuild
a = a[0] + "C" + b[0] + firstNumber + b[1].replace(/[0-9]/g, "");

Not tested thoroughly but for your case it works.

gpasci
  • 1,420
  • 11
  • 16
1

You can use of isNaN() function which returns false if its a valid number or true if it's not a number

var str = ['(','C','1','2','3','4','*',')']; // Your Input
var temp = [],count = 0;

for(var i=0;i<str.length;i++)
{
   if(i<str.indexOf('C') + 2)
   {
      temp[count] = str[i];
      count++;
   }
   else
   {
      if(isNaN(str[i]) == true)
      {
        temp[count] = str[i];
        count++;
      }      
   }
 }
str = temp;
alert(str);

LIVE DEMO

Prasath K
  • 4,950
  • 7
  • 23
  • 35
  • ur solution is perfect .. even i was doin the same ... was jst lukin for some more efficient way.. thanks .... – Garry Mar 25 '13 at 06:15