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

82
Views
return multiple numbers after entry point element in javascript

I want to search in text array,and find one of this element of entry array in text , and check that if the next element or elements are numbers, return the number or those numbers

But my code works just for 1 number element after entry element , as I mentioned before, I want multiple numbers after entry element, until it reaches an element of string type.

this is my code :

const entry = [
  'ENTRY', 'ZONE', 'ENTRI', 'ENTRE', 'ENTR', 'ZON', 'ZONI'
];

const text = ['HI', 'GOOD', 564, 'CLX', 'ENTRI', 'YYY', 'ENTRY', 657, 780, 34, 'XXX'];

const set = new Set(entry);

let result = [];
for (let i = 0; i < text.length; i++) {
  let curr = text[i],
    next = text[i + 1];
  if (set.has(curr) && typeof next == 'number') {
    result.push(next);
  }
}

console.log(result)

so this is output : //output : [ 657 ]

what i want : //output : [657 , 780 , 34]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can add a nested loop on the same loop variable and add array values for as long they are numeric.

const entry = [
  'ENTRY', 'ZONE', 'ENTRI', 'ENTRE', 'ENTR', 'ZON', 'ZONI'
];

const text = ['HI', 'GOOD', 564, 'CLX', 'ENTRI', 'YYY', 'ENTRY', 657, 780, 34, 'XXX'];

const set = new Set(entry);

let result = [];
for (let i = 0; i < text.length; i++) {
  if (set.has(text[i])) {
    while (typeof text[i + 1] == "number") {
      result.push(text[++i]);
    }
  }
}

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

your condition says that the valid element has to come right after the set member, which only applied to the first element not every element.

a different way to do it is to turn on a flag that a member was found and turn it off once you reach another string that's not a member.

 const entry = ['ENTRY' , 'ZONE' , 'ENTRI' , 'ENTRE' , 'ENTR' ,'ZON' , 'ZONI'];

const text = ['hi' , 'good' , 564 , 'clx' , 'entri' , 'yyyy' , 'ENTRY' ,657 , 780 , 34 , 'xxxx'];

const set = new Set(entry);

let result = [];
let isAfterMatch = false
for (let i = 0; i < text.length; i++) {
  const curr = text[i];
  if (isAfterMatch && typeof curr === 'number') {
    result.push(curr);
  } else {
    isAfterMatch = set.has(curr)
  }
}

console.log(result)

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!