Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
Node.js : Return the elements that are between two specific elements in array

I Have an text array and an object, And the object keys has these array values:

text = ['CALX' , 'ENTRY' , 43 , 44 , 'TAR' , 50 , 51 , 52 , 'OK', 'XX' , 'SL' , 12 , YYY]

obj = {
  entry : ['ENTRY' , 'ENTER' ,  'ENT'],
  target :['TARGET' , 'TP' , 'TAR' , 'TARGETS'],
  sl : ['STOP' , 'SLOSS' , 'SL' , 'SELL'],
}

To simplify :

word = text array element(like 'CALX')

key = object values array element(like 'ENTRY' or 'TP'

So I want to search in text array and if word was equal with key , push the elements after word to key name array in result object, until the later element in text array was another key or current key

for example , from the text array and obj , I want this output :

result = {
    entry: [43 , 44],
    target: [50 , 51 , 52 , 'OK' , 'XX'],
    sl: [12 , 'YYY']
}

This is my code and I don't know how returns words after current word :

text = ['CALX' , 'ENTRY' , 43 , 44 , 'TAR' , 50 , 51 , 52 , 'OK', 'XX' , 'SL' , 12 , YYY]

obj = {
  entry : ['ENTRY' , 'ENTER' ,  'ENT'],
  target :['TARGET' , 'TP' , 'TAR' , 'TARGETS'],
  sl : ['STOP' , 'SLOSS' , 'SL' , 'SELL'],
}

result = {
    entry: [43 , 44],
    target: [50 , 51 , 52 , 'OK' , 'XX'],
    sl: [12 , 'YYY']
}

  for (var i = 0; i < text.length; i++) { 
    var word = text[i];
    for (var j = 0; j < Object.keys(obj).length; j++) {
      var objKeys = Object.keys(obj); 
      var a = obj[objKeys[j]]; 
      for (var k = 0; k < a.length; k++) {
        if (word == a[k]) {
           

        }
        }
      }
    }
  console.log(result);

Thank you for your help

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could store the latest found type.

const
    text = ['CALX' , 'ENTRY' , 43 , 44 , 'TAR' , 50 , 51 , 52 , 'OK', 'XX' , 'SL' , 12 , 'YYY'],
    types = { entry : ['ENTRY' , 'ENTER' ,  'ENT'], target :['TARGET' , 'TP' , 'TAR' , 'TARGETS'], sl : ['STOP' , 'SLOSS' , 'SL' , 'SELL'] },
    result = {};
    
let type;

for (const item of text) {
    let temp = Object.keys(types).find(k => types[k].includes(item));
    if (temp) {
        type = temp;
        result[type] = result[type] || []; // newer: result[type] ??= [];
        continue;
    }
    if (type) result[type].push(item);
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

I have updated the loop for your use.

let text = ['CALX' , 'ENTRY' , 43 , 44 , 'TAR' , 50 , 51 , 52 , 'OK', 'XX' , 'SL' , 12 , 'YYY']

let obj = {
  entry : ['ENTRY' , 'ENTER' ,  'ENT'],
  target :['TARGET' , 'TP' , 'TAR' , 'TARGETS'],
  sl : ['STOP' , 'SLOSS' , 'SL' , 'SELL'],
}

let expectedResult = {
    entry: [43 , 44],
    target: [50 , 51 , 52 , 'OK' , 'XX'],
    sl: [12 , 'YYY']
}
let result = {}

//to reduce loops intially creating a temp array
let tempArr = []
for(key in obj) {
 tempArr =  [...tempArr, ...obj[key]] 
}

for(key in obj) {
  result[key] = []
  for(let i=0; i< obj[key].length; i++) {
    let matchFound = false
    for(let j =0; j<text.length; j++) {
      if(text[j] == obj[key][i]) {
        matchFound = true
      }
      if(matchFound && tempArr.indexOf(text[j]) == -1) {
        result[key].push(text[j])
      }
      if(matchFound && tempArr.indexOf(text[j+1]) != -1) {
        break;
      }
    }
  }
}
console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda