Home > Blockchain >  JS / Array of Objects / cut by condition values in the object
JS / Array of Objects / cut by condition values in the object

Time:11-12

Hello and thank you for your time!

i have the array of objects:

[
  { word: 'T', start: 8.87085, end: 8.88011 },
  { word: 'A', start: 8.59176, end: 8.87085 },
  { word: 'C', start: 8.39242, end: 8.59176 },
  { word: 'A', start: 8.07347, end: 8.39242 },
  { word: 'A', start: 7.87412, end: 8.07347 },
  { word: 'T', start: 7.71465, end: 7.87412 },
  { word: 'A', start: 7.51531, end: 7.71465 },
  { word: 'T', start: 7.23622, end: 7.5153 },
  { word: 'A', start: 7.11661, end: 7.23622 },
  { word: 'C', start: 6.91727, end: 7.11661 },
  { word: 'C', start: 6.63818, end: 6.91727 },
  { word: 'A', start: 6.47871, end: 6.63818 },
  { word: 'C', start: 6.39897, end: 6.47871 },
  { word: 'T', start: 5.52185, end: 6.02185 },
  { word: 'T', start: 3.76762, end: 4.26762 },
  { word: 'A', start: 3.36893, end: 3.76762 },
  { word: 'A', start: 3.04997, end: 3.36893 },
  { word: 'C', start: 2.69115, end: 3.04997 },
  { word: 'A', start: 2.57155, end: 2.69115 },
  { word: 'T', start: 2.41207, end: 2.57155 },
  { word: 'A', start: 2.21272, end: 2.29246 },
  { word: 'A', start: 1.65456, end: 2.15456 },
  { word: 'A', start: 1.45521, end: 1.65456 },
  { word: 'T', start: 1.25587, end: 1.45521 },
  { word: 'A', start: 1.01665, end: 1.25587 },
  { word: 'A', start: 0.85718, end: 1.01665 },
  { word: 'A', start: 0.61796, end: 0.85718 }
]

I need to cut objects in period from word == 'C' to word == 'T'.

Output should be like this:

[
  { word: 'T', start: 8.87085, end: 8.88011 },
  { word: 'A', start: 8.59176, end: 8.87085 },
  { word: 'A', start: 7.51531, end: 7.71465 },
  { word: 'T', start: 7.23622, end: 7.5153 },
  { word: 'A', start: 7.11661, end: 7.23622 },
  { word: 'T', start: 3.76762, end: 4.26762 },
  { word: 'A', start: 3.36893, end: 3.76762 },
  { word: 'A', start: 3.04997, end: 3.36893 },
  { word: 'A', start: 2.21272, end: 2.29246 },
  { word: 'A', start: 1.65456, end: 2.15456 },
  { word: 'A', start: 1.45521, end: 1.65456 },
  { word: 'T', start: 1.25587, end: 1.45521 },
  { word: 'A', start: 1.01665, end: 1.25587 },
  { word: 'A', start: 0.85718, end: 1.01665 },
  { word: 'A', start: 0.61796, end: 0.85718 }
]

My idea is find indexes of objects 'C' and 'T' and cut this period. But i have a problem with realization (see comments in code)

function glue(data, t, c) {
  let dataReverse = data.reverse();

  for (let i = 0; i < dataReverse.length; i   ) {

    let indexT = dataReverse.map(e => e.word).indexOf(c)
    let indexC = dataReverse.map(e => e.word).indexOf(t)

    if (indexT < indexC) { // Here the condition does not came true , but indexT = 0 and indexC = 2 
      dataReverse = dataReverse.splice(indexT,1) // what method better to delete the period? 

    } else {
      dataReverse = dataReverse.splice(indexC,indexC-indexT)
    }
    
  }

Thanks for your idea and realization

CodePudding user response:

You can use Array.reduce() and a flag that holds the cutting status (isCutting in this example):

const data = [
  { word: 'T', start: 8.87085, end: 8.88011 },
  { word: 'A', start: 8.59176, end: 8.87085 },
  { word: 'C', start: 8.39242, end: 8.59176 },
  { word: 'A', start: 8.07347, end: 8.39242 },
  { word: 'A', start: 7.87412, end: 8.07347 },
  { word: 'T', start: 7.71465, end: 7.87412 },
  { word: 'A', start: 7.51531, end: 7.71465 },
  { word: 'T', start: 7.23622, end: 7.5153 },
  { word: 'A', start: 7.11661, end: 7.23622 },
  { word: 'C', start: 6.91727, end: 7.11661 },
  { word: 'C', start: 6.63818, end: 6.91727 },
  { word: 'A', start: 6.47871, end: 6.63818 },
  { word: 'C', start: 6.39897, end: 6.47871 },
  { word: 'T', start: 5.52185, end: 6.02185 },
  { word: 'T', start: 3.76762, end: 4.26762 },
  { word: 'A', start: 3.36893, end: 3.76762 },
  { word: 'A', start: 3.04997, end: 3.36893 },
  { word: 'C', start: 2.69115, end: 3.04997 },
  { word: 'A', start: 2.57155, end: 2.69115 },
  { word: 'T', start: 2.41207, end: 2.57155 },
  { word: 'A', start: 2.21272, end: 2.29246 },
  { word: 'A', start: 1.65456, end: 2.15456 },
  { word: 'A', start: 1.45521, end: 1.65456 },
  { word: 'T', start: 1.25587, end: 1.45521 },
  { word: 'A', start: 1.01665, end: 1.25587 },
  { word: 'A', start: 0.85718, end: 1.01665 },
  { word: 'A', start: 0.61796, end: 0.85718 }
]


let isCutting = false

const res = data.reduce((a,b) => {
  if (isCutting) {
    if (b.word == 'T') { isCutting = false }
  } else {
    if (b.word == 'C') { isCutting = true } 
    else { a.push(b) }
  } 
  return a
}, [])

console.log(res)

CodePudding user response:

Create an index string word-index, word-index etc, manipulate that string and generate the result from remaining indexes:

result = data
    .map((o, i) => o.word   i).join('')
    .replace(/C. ?T\d /g, '')
    .match(/\d /g)
    .map(n => data[n])
  • Related