Home > OS >  Node.js : Return the elements that are between two specific elements in array
Node.js : Return the elements that are between two specific elements in array

Time:10-24

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

CodePudding user response:

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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related