How do you add a gap through every four symbols in a set of objects?



  • There's a mass consisting of n objects, in my case the credit card number. How to add the gaps in facilities to replace the original [XXXXXXXXXXXXXXXXXXX] has become [XXXX XXXX XXXX XXXX]? Massive is not permanent, is filled with several objects using the match(s) method and the regular expression, then a new set is created by the filter(s) method, where each facility passes through the Moon algorithm. So, in the end, I need to add the gaps in the map numbers. The Massive is as follows: Array(6) 0: "5363456787653454" 1: "5674234689450012" 2: "5192722517688913" 3: "4556796335044346" 4: "4916849417542904" 5: "5213920324755355" length: 6



  • const creditsCard = [
      {code: 'XXXXXXXXXXXXXXXX'}, 
      {code: '1111444455556666'}, 
      {code: 'XXXXXXXXXXXXXXXX'}, 
    ] 
    

    //Где % 4 - тот отступ на который разделяем
    const newCodeCard = creditsCard.map(({code}) => {
    return [...code].map((e, i) => (i) % 4 === 0 ? ' ' + e : e).join('').trim()
    })

    console.log(newCodeCard)

    //Код для работы который возвращает массив-массивов со строкой.
    const newCodeCard = creditsCard.map((array) => {
    return array.map((element, i) =>
    [...element]
    .map((e, i) => (i % 4 === 0 ? ' ' + e : e))
    .join('')
    .trim()
    );
    });


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2