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

203
Visualizações
How can I make a new array from a event emitter ( gives bytes array), where new arrays first element will be '10' and last '13'?

I have incoming bytes of arrays once a time. It kind of looks like this

[10,12,34,58,62]

[83,33,23,44,13]

[10,12,34,58,62]

[34, 77,54,23,87]

[83,33,23,44,13]

[10,12,34,58,62]

[83,33,23,44,66]

[13] …continues.

So, it renders each array every time. I needed them to be rendered like

[10,12,34,58,62,83,33,23,44,13]

[10,12,34,58,62,34,77,54,23,87,83,33,23,44,13]

[10,12,34,58,62,83,33,23,44,66,13].

The first element will be 10 and the last 13. Can anyone help me with the code? Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If your incoming bytes is 2d array then try this.

let arr = [
  [10, 12, 34, 58, 62],
  [83, 33, 23, 44, 13],
  [10, 12, 34, 58, 62],
  [34, 77, 54, 23, 87],
  [83, 33, 23, 44, 13],
  [10, 12, 34, 58, 62],
  [83, 33, 23, 44, 66],
  [13]
]
let openedArr = []
for (let value of arr) {
  openedArr = [...openedArr, ...value]
}

let resultArr = []
let child = []

for (let value of openedArr) {
  child.push(value)
  if (value == 13) {
    resultArr.push(child)
    child = []
  }
}
console.log(resultArr)

Explaination

  1. Make an new array by spreading all incoming array.
  2. Create two arrays resultArr and Child Array
  3. Push every array elements to Child array
  4. If there is 13, then push child array to resultArr, and empty child array.
about 4 years ago · Juan Pablo Isaza Relatório

0

// @param {accumulator} is array of array of numbers
// @param {chunk} is the array of numbers
// @return {accumulator} 
const process_bytes = (accumulator, chunk) => {
    if (accumulator && accumulator.length) {
        if (chunk && chunk.length) {
            if (chunk[0] === 10) {
                return [...accumulator, chunk];
            } else {
                return [...accumulator.slice(0, accumulator.length - 1), [...accumulator[accumulator.length - 1], ...chunk]];
            }
        }
    }
    return chunk && chunk.length && [chunk] || [];
};

Assumption

The following code follows the provided input data and the expected output in the OP


RUN THE CODE

const process_bytes = (accumulator, chunk) => {
  if (accumulator && accumulator.length) {
    if (chunk && chunk.length) {
      if (chunk[0] === 10) {
        return [...accumulator, chunk];
      } else {
        return [...accumulator.slice(0, accumulator.length - 1), [...accumulator[accumulator.length - 1], ...chunk]];
      }
    }
  }
  return chunk && chunk.length && [chunk] || [];
};

const testData = [
  [10, 12, 34, 58, 62],
  [83, 33, 23, 44, 13],
  [10, 12, 34, 58, 62],
  [34, 77, 54, 23, 87],
  [83, 33, 23, 44, 13],
  [10, 12, 34, 58, 62],
  [83, 33, 23, 44, 66]
];

let processed_bytes = [];

testData.forEach(x => {
  processed_bytes = process_bytes(processed_bytes, x);
});
console.log(processed_bytes);



WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

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