Home > Net >  Javascript nested map function return string
Javascript nested map function return string

Time:03-08

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);

CodePudding user response:

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(''));

CodePudding user response:

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("")
  • Related