Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Javascript foreach question: how to pick out the number element to form new array

I have some tasks to handle in my daily jobs, so I need to do it in a automatic way. My task is:

  1. there will be some messages sent to my IM, and I need to append the first, second & third number to each links with a "|".
  2. if there only 2 numbers in the number line, a 0 is needed in the first place.

For example, in the cleanResult example, I need it to be done like:

finalResult = ["https://www.example.com/firstlink|500",
"https://www.example.com/firstlink|150",
"https://www.example.com/firstlink|30",
"https://www.exmaple.com/secondlink|600",
"https://www.exmaple.com/secondlink|150",
"https://www.exmaple.com/secondlink|30",
"https://www.example.com/thirdlink|500",
"https://www.example.com/thirdlink|150",
"https://www.example.com/thirdlink|30",
"https://www.example.com/forthlink|600",
"https://www.example.com/forthlink|100",
"https://www.example.com/forthlink|20",
"https://www.example.com/fithlink|0",
"https://www.example.com/fithlink|200",
"https://www.example.com/fithlink|50"
]

Here's the codes I had done so far:

const urlRegex = /(https?\:\/\/)?([^\.\s]+)?[^\.\s]+\.[^\s]+/gi;
const digitRegex = /^(?=.*\d)[\d ]+$/;

cleanResult = ["https://www.example.com/firstlink",
"https://www.exmaple.com/secondlink",
"https://www.example.com/thirdlink",
"500    150    30",
"https://www.example.com/forthlink",
"600    100     20",
"https://www.example.com/fithlink",
"200   50"
]

cleanResult.forEach((item, index) => {
       if (item.match(digitRegex)) {
              //codes I don't know how to do...
      }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Are elements in cleanResult always either a URL or a number? In that case, you could just check the first character of the string to see if it's a number (basically a non-url). If it's not a URL, then we know it's numbers, and we can do something with the URL, which should the the previous element:

// If it's a URL, we will store it here for future use
let currentURL = ''

cleanResult.forEach((item, index) => {
  // Get the first character of this string
  const first = item[0]

  if (!Number.isInteger(first)) {
    // This is NOT a number, so must be a URL, 
    // let's store it in our variable to use in the next loop
    currentURL = item

  } else {
    // This IS a number, which means we need to do a few things:
    // 1. Split into separate numbers
    // 2. Create a new URL pattern from each number
    // 3. Push to finalResult

    // 1. Split by tab delimiter (?)

    // splits by tab, and returns an array
    const numbers = item.split('\t')

    // 2. Create a new URL pattern from each number
    numbers.forEach((n) {
      // This should now give you the URL + | + the number:
      // ex: https://example.com/firstlink|500
      const newURL = currentURL + '|' + n

      // 3. push to the finalResult array
      finalResult.push(newURL)
    })
  }
})

I haven't tested it, but this is the process that I generally use: break it into smaller tasks and take it one step at a time. I also didn't use regex, just to make it easier. We're assuming that you will receive either a URL or a list of numbers separated by a tab. This means you can afford to keep it a bit simple.

I'm sure there are way more efficient ways to do it and in a lot fewer lines, but if you're just learning JS or programming, there is nothing wrong with being extra verbose so that you can understand early concepts.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!