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

139
Visualizações
How would I be able to return more than one object based on user input amount

Let's say PackInput is an array input.

What I'd like to do's return a dynamic amount of objects depending on how large the input array PackInput is.

For example: if PackInput is [4,5,6], then I'd like three objects returned for each ItemID.

For example: return {... ItemID: 4 ...}, return {... ItemID: 5 ...}, return {... ItemID: 6 ...}.

My current code below is only grabbing the first item of the array instead of all of them and I'm not sure why. I've turned my wheels on this for so long and now I've hit a wall. What am I doing wrong?

for(let i = 0; i < PackInput.length; i++) {      
        return {
            TimestampUTC: Date.now(),
            Payload: {
                ItemID : PackInput[i]
            }
        }
} 

Updated:

let array = PackInput.map((items) => ({
        TimestampUTC: Date.now(),
        Payload: {
            ItemID : items
        }
   })
);

let objects = array.reduce(function(target, key, index) {
    target[index] = key;
    return target;
}) 

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

0

You can use the map method to achieve what you want

return PackInput.map((element) => ({
  TimestampUTC: Date.now(),
  Payload: {
    ItemID : element
  }
}))

A return statement ends the execution of a function, and returns control to the calling function/upper scope.

Update on object:

const object = PackInput.reduce(function(previousValue, currentValue, index) {
    return {
      ...previousValue,
      [index]: currentValue
    }
}, {})

You need to provide an empty object as 2nd argument for the reduce function.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can return an array/object. The problem is that you can call return only once in a function and as soon as a function reaches return it would be returned to the caller scope. You can use the loop to create the array/object and then return the final value:

let array = [];
for(let i = 0; i < PackInput.length; i++) {      
        array.push({
            TimestampUTC: Date.now(),
            Payload: {
                ItemID : PackInput[i]
            }
        });
} 

return array;
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