skip to Main Content

I have an array of items like this:

const items = ['98 ab','dv 67','96 bh',' 95 df'];

What I am trying is that: to loop over to these items and call a specific functions if the first occurances of the items starts with a number. For example:
if items starts with (98) and then call functionA and if items starts with (dv) then call functionB.

const result = items.map(el=>
 if(el==='items that starts with number') {
  functionA;
  } else {
   functionB
  })

So according to the items array, I should call the functionA three times as there three values that starts with a number in it.

Any ideas how I can achieve it.I need to use Javascript for this. thanks is advance.

4

Answers


  1. If you extract the number from the string, you can use a simple object to map the found number to a function of your liking.

    If you only want to search for numbers at the beginning of the string, use the replace solution from this answer:

    let n = item.replace(/(^d+)(.+$)/i, '$1');
    

    const functionMap = {
      98:         (n) => console.log('Function #1;', n),
      67:         (n) => console.log('Function #2;', n),
      "default":  (n) => console.log('Not Found', n)
    };
    
    const items = ['98 ab','dv 67','96 bh',' 95 df'];
    
    items.forEach(item => {
    //  let n = item.match(/d+/)[0];                 // Everywhere
        let n = item.replace(/(^d+)(.+$)/i, '$1');   // Start only
    
        let k = (n in functionMap) ? n : 'default';
        functionMap[k](n);
    });

    Which outputs:

    Function #1; 98
    Function #2; 67
    Not Found 96
    Not Found 95
    

    Since I’ve only provided a number in functionMap for 98 and 67.

    Login or Signup to reply.
  2. You can use a regular expression to check if each element in the array starts with a number. If it does, you call functionA; otherwise, you call functionB.

    const items = ['98 ab', 'dv 67', '96 bh', '95 df'];
    
    function functionA(item) {
      console.log('Called functionA with item:', item);
    }
    
    function functionB(item) {
      console.log('Called functionB with item:', item);
    }
    
    const result = items.map(el => {
      if (/^d/.test(el)) {
        return functionA(el);
      } else {
        return functionB(el);
      }
    });
    

    The ^d checks if the string starts with a digit.

    I just create a small example, but since you are not transforming the array into a new array, a simple forEach could be more appropriate if you don’t need the result array for other purposes.

    Login or Signup to reply.
  3. You could trim the string and check with a regular expression.

    const
        fnA = (v) => console.log('fnA:', v),
        fnB = (v) => console.log('fnB:', v),
        items = ['98 ab','dv 67','96 bh',' 95 df'];
    
    items.forEach(el => {
       if (/^d/.test(el.trim())) fnA(el);
       else if (/^[a-z]/i.test(el.trim())) fnB(el);
    });
    Login or Signup to reply.
  4. Dont know if it will help. But maybe you can try something like this.

    const items = ['98 ab','dv 67','96 bh',' 95 df'];
    
    items.forEach(item=> {
        
        const firstChar= [...item.trim()][0]; // 9,d,9,9
        
        isNaN(firstChar)? B():A(); 
        
    })
    
    function A(){
        console.log('This is function A. Its a Number')
    }
    
    function B(){
        console.log('This is function B. Its a String')
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search