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

187
Visualizações
Function on a nested array in js

Here is my Function which turns timestamps to seconds,

   const convertor = (x) => {
      const result = x.split(':');
      const final =
        parseInt(result[0] * 60) +
        parseInt(result[1] * 60) +
        parseInt(result[2]) -
        1.2;
      return final;
    };

input = 00:01:02.330 result: 62.330

How can I implement it on a nested array like this?

array = [ [ 00:01:02.330],[00:01:04.550],[01:11:02.330] ], [ [00:01:02.330],[00:01:02.330] ] , [ [00:01:02.330],[00:01:02.330],[00:01:02.330] ] ]

I want to preserve its nested structure so the result should be:

[ [ 22.3],[28.5],[903.50] ], [ [1252.3],[62.2] ] , [ [654.25],[965.25],[1254.32] ] ]

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

0

Not sure why you're keeping the timestamps in an array when it's the only element, but you can get the desired output by chaining map()

const convertor = (x) => {
  const result = x[0].split(':');
  const final =
    parseInt(result[0] * 60) +
    parseInt(result[1] * 60) +
    parseInt(result[2]) -
    1.2;
  return final;
};

const array = [
  [ [ '00:01:02.330' ], [ '00:01:04.550' ], [ '01:11:02.330' ] ], 
  [ [ '00:01:02.330' ], [ '00:01:02.330' ] ], 
  [ [ '00:01:02.330' ], [ '00:01:02.330' ], [ '00:01:02.330' ] ] 
];

const output = array.map(a => a.map(b => convertor(b)));
console.log(output);

[
  [
    60.8,
    62.8,
    720.8
  ],
  [
    60.8,
    60.8
  ],
  [
    60.8,
    60.8,
    60.8
  ]
]
about 4 years ago · Juan Pablo Isaza Relatório

0

You can write a wrapper that accepts a function and an array, and maps over the array-- if the item is an array, it recurses; otherwise, it passes the item into the passed function-- this allows you to process nested arrays of any arbitrary depth and retain the desired structure:

const convertor = (x) => {
  const result = x.split(':');
  const final =
    parseInt(result[0] * 60) +
    parseInt(result[1] * 60) +
    parseInt(result[2]) -
    1.2;
  return final;
};

const recursiveNestedMapper = (fn, arr) => {
  const output = arr.map((item) => {
    if (Array.isArray(item)) {
      return recursiveNestedMapper(fn, item);
    } else {
      return fn(item);
    }
  });

  return output;
}

const inputArray = [
  [
    ['00:01:02.330'],
    ['00:01:04.550'],
    ['01:11:02.330']
  ],
  [
    ['00:01:02.330'],
    ['00:01:02.330']
  ],
  [
    ['00:01:02.330'],
    ['00:01:02.330'],
    ['00:01:02.330']
  ]
];

const outArray = recursiveNestedMapper(convertor, inputArray);

console.log(outArray);

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