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.

3
  • Sounds like you're looking for Array.slice() ?? Commented Mar 25, 2013 at 5: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. Commented Mar 25, 2013 at 5: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). Commented Mar 25, 2013 at 5:53

3 Answers 3

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 ...

Sign up to request clarification or add additional context in comments.

2 Comments

i tried Array.prototype.trimNumbersFrom but its not workin fine for just one case ... ["(", "C", "1", "0"] ...
Yep, !+a returns true for '0'. IsNaN(+a) doesn"t, edited my answer.
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.

Comments

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

1 Comment

ur solution is perfect .. even i was doin the same ... was jst lukin for some more efficient way.. thanks ....

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.