Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda