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

209
Vistas
Node.js: devuelve los elementos que están entre dos elementos específicos en la matriz

Tengo una matriz de texto y un objeto, y las claves del objeto tienen estos valores de matriz:

 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'], }

Simplificar :

palabra = elemento de matriz de texto (como 'CALX' )

clave = elemento de matriz de valores de objeto (como 'ENTRY' o 'TP'

Entonces, quiero buscar en la matriz de texto y si la palabra era igual a la clave , empujar los elementos después de la palabra a la matriz de nombre clave en el objeto de resultado , hasta que el último elemento en la matriz de texto fuera otra clave o clave actual

por ejemplo, de la matriz de texto y obj, quiero esta salida:

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

Este es mi código y no sé cómo devuelve palabras después de la palabra actual:

 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);

Gracias por tu ayuda

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

0

Puede almacenar el último tipo encontrado.

 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

He actualizado el bucle para su uso.

 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