0

I want to make this code shorter.

 const coupons = rets.map((ret) => ({
  coupon_id: ret.coupon_id,
  bot_id: ret.bot_id,
  lang_cd: ret.lang_cd,
  coupon_name: ret.coupon_name,
  start_date: ret.start_date,
  end_date: ret.end_date,
  coupon_data: ret.coupon_data,
  title: ret.title,
  sub_title: ret.sub_title,
  image: ret.image,
  style: ret.style,
  section: ret.section,
  button_pc: ret.button_pc,
}));

I get rets from DB.

I tried like below by myself but it didn't work well. error says like [column is string].

const couponColumns = [
  'coupon_id', 'bot_id', 'lang_cd', 'coupon_name', 'start_date', 'end_date', 
  'coupon_data', 'title', 'sub_title',' image', 'style', 'section', 'button_pc'
]

const coupons = rets.map((ret) => ({
   couponColumns.map((column) => {
     column: ret.column
   }
}));
Yuzu
  • 59
  • 1
  • 8
  • You need bracket notation for the variable property name, and you also need to return an object instead of starting a function block in the nested map – CertainPerformance May 30 '22 at 03:16
  • `const coupons = rets.map((ret) => ({ couponColumns.map((column) => { [column]: ret.column } })); ` – RiTeSh May 30 '22 at 03:22
  • If you have an array of objects with those property names why are you mapping over it to create an array of objects with those same property names? – Andy May 30 '22 at 03:33
  • Thank you for your answer. I tried const coupons = rets.map((ret) => ({ couponColumns.map((column) => { [column]: ret[column] }) })); but it didn't work – Yuzu May 30 '22 at 03:34
  • 1
    const coupons = rets.map((ret) => ({...ret})); – SHAYAK May 30 '22 at 04:31

0 Answers0