Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!