Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda