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

165
Visualizações
Javascript nested map function return string

I have a set of data. I map through my data, if data is "HOME_DELIVERY then it will go to another function which will check is the order is valid or not. if the order is valid then it will return hello string. So far everything works as expected but I want my map function return string hello. currently it's returning ['hello']

const getRoundName = (orderId) => {
  if (orderId === "a4013438-926f-4fdc-8f6a-a7aa402b40ea") {
    return "hello";
  } else {
    retrun
  }
};

const orders = [
  {
    id: "a4013438-926f-4fdc-8f6a-a7aa402b40ea",
    modifiedAt: "2022-02-28T09:26:18+00:00",
    deliveryDate: "2022-02-28",
    pickupLocation: null,
    orderStatus: "MODIFIED",
    deliverySlotId: "2022-02-28:66ee337c-e252-4297-9aed-cafcef396f19",
    createdAt: "2022-02-26T06:38:46+00:00",
    deliveryTime: "22-00",
    storeId: "516079340",
    orderNumber: 28354107,
    paymentMethod: "ON_DELIVERY",
    cartItems: [[Object], [Object], [Object]],
    deliveryMethod: "HOME_DELIVERY",
    additionalInfo: null,
  },
];

const roundName = orders.map((order) => {
  return order.deliveryMethod === 'HOME_DELIVERY' ? getRoundName(order.id) : ''
});

console.log(roundName);

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

0

You can use filter before calling map

//if it has more than 1 items in the list, it will join them together like this `hellohellohello`
const orderIds = orders.filter((order) => order.deliveryMethod === 'HOME_DELIVERY').map(order => getRoundName(order.id)).join("")
about 4 years ago · Juan Pablo Isaza Relatório

0

Array.map returns an array as response. If you need a string as response, you have to modify the logic as

const getRoundName = (orderId) => {
  if (orderId === "a4013438-926f-4fdc-8f6a-a7aa402b40ea") {
    return "hello";
  } else {
    return;
  }
};

const orders = [
  {
    id: "a4013438-926f-4fdc-8f6a-a7aa402b40ea",
    modifiedAt: "2022-02-28T09:26:18+00:00",
    deliveryDate: "2022-02-28",
    pickupLocation: null,
    orderStatus: "MODIFIED",
    deliverySlotId: "2022-02-28:66ee337c-e252-4297-9aed-cafcef396f19",
    createdAt: "2022-02-26T06:38:46+00:00",
    deliveryTime: "22-00",
    storeId: "516079340",
    orderNumber: 28354107,
    paymentMethod: "ON_DELIVERY",
    cartItems: [[Object], [Object], [Object]],
    deliveryMethod: "HOME_DELIVERY",
    additionalInfo: null,
  },
  {
    id: "a4013438-926f-4fdc-8f6a-a7aa402b40ef",
    modifiedAt: "2022-02-28T09:26:18+00:00",
    deliveryDate: "2022-02-28",
    pickupLocation: null,
    orderStatus: "MODIFIED",
    deliverySlotId: "2022-02-28:66ee337c-e252-4297-9aed-cafcef396f19",
    createdAt: "2022-02-26T06:38:46+00:00",
    deliveryTime: "22-00",
    storeId: "516079340",
    orderNumber: 28354107,
    paymentMethod: "ON_DELIVERY",
    cartItems: [[Object], [Object], [Object]],
    deliveryMethod: "HOME_DELIVERY",
    additionalInfo: null,
  },
];

const roundName = orders.flatMap((order) => {
  return order.deliveryMethod === 'HOME_DELIVERY' ? getRoundName(order.id) : ''
});

console.log(roundName.join(''));

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