0

am asking after many trials of achieving what am about to ask i have two arrays the first one is array of offers with is_checked status true or false like this

[
   {
      id:24,
      name:"Discount 50",
      discount_amount:"50.00",
      discount_type:"price",
      start_date:"2021-05-06",
      end_date:"2021-05-07",
      is_checked:true,
      enabled:1
   },
   {
      id:22,
      name:"Discount 40",
      discount_amount:"40.00",
      discount_type:"price",
      start_date:"2021-05-07",
      end_date:"2021-05-10",
      is_checked:false,
      enabled:1
   }
]

then i have a another array of objects that represent days according to specific period start_date and end_date

like the following

[
   {
      id:1,
      offer_id:24,
      date:"2021-05-06",
      date_name:"Thursday",
      discount_type:"price",
      discount_amount:"50.00"
   },
   {
      id:2,
      offer_id:22,
      date:"2021-05-07",
      date_name:"Friday",
      discount_type:"price",
      discount_amount:"50.00"
   },
   {
      id:3,
      offer_id:22,
      date:"2021-05-08",
      date_name:"Saturday",
      discount_type:"price",
      discount_amount:"50.00"
   }
]

What i need to achieve in simple words from the first array as you can see the offer with id 24 the is_checked status of it is set to true then in the second array which has an offer_id matches the checked id i need to attach another key called also is_checked so the result became like this

[
   {
      id:1,
      offer_id:24,
      date:"2021-05-06",
      date_name:"Thursday",
      discount_type:"price",
      discount_amount:"50.00",
      is_checked:true,

   },
   {
      id:2,
      offer_id:22,
      date:"2021-05-07",
      date_name:"Friday",
      discount_type:"price",
      discount_amount:"50.00",
      is_checked:false,
   },
   {
      id:3,
      offer_id:22,
      date:"2021-05-08",
      date_name:"Saturday",
      discount_type:"price",
      discount_amount:"50.00",
      is_checked:false,
   }
]

and sorry if the description was too long

  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available [`Object`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods) methods (both static and on prototype). – Sebastian Simon May 06 '21 at 09:05
  • Appreciate will have a look on that – Emad Rashad Muhammed May 06 '21 at 09:05

2 Answers2

2

One way to achieve this is to make a list of all the id values in offers that have is_checked == true, then iterate over the days array, setting the is_checked property according to whether the offer_id value is in that list:

const offers = [{
    "id": 24,
    "name": "Discount 50",
    "discount_amount": "50.00",
    "discount_type": "price",
    "start_date": "2021-05-06",
    "end_date": "2021-05-07",
    "is_checked": true,
    "enabled": 1
  },
  {
    "id": 22,
    "name": "Discount 40",
    "discount_amount": "40.00",
    "discount_type": "price",
    "start_date": "2021-05-07",
    "end_date": "2021-05-10",
    "is_checked": false,
    "enabled": 1
  }
];

const days = [{
    "id": 1,
    "offer_id": 24,
    "date": "2021-05-06",
    "date_name": "Thursday",
    "discount_type": "price",
    "discount_amount": "50.00"
  },
  {
    "id": 2,
    "offer_id": 22,
    "date": "2021-05-07",
    "date_name": "Friday",
    "discount_type": "price",
    "discount_amount": "50.00"
  },
  {
    "id": 3,
    "offer_id": 22,
    "date": "2021-05-08",
    "date_name": "Saturday",
    "discount_type": "price",
    "discount_amount": "50.00"
  }
];

const checked = offers
  .filter(({ is_checked }) => is_checked)
  .map(({ id }) => id);

days.forEach(d => d.is_checked = checked.includes(d.offer_id));

console.log(days)
Nick
  • 138,499
  • 22
  • 57
  • 95
1

Using Array#forEach and Map.

  • Using Map map the id to is_checked.
  • Then for every object in days get the value of is_checked from the Map.

const 
  offers=[{id:24,name:"Discount 50",discount_amount:"50.00",discount_type:"price",start_date:"2021-05-06",end_date:"2021-05-07",is_checked:!0,enabled:1},{id:22,name:"Discount 40",discount_amount:"40.00",discount_type:"price",start_date:"2021-05-07",end_date:"2021-05-10",is_checked:!1,enabled:1}],
  days=[{id:1,offer_id:24,date:"2021-05-06",date_name:"Thursday",discount_type:"price",discount_amount:"50.00"},{id:2,offer_id:22,date:"2021-05-07",date_name:"Friday",discount_type:"price",discount_amount:"50.00"},{id:3,offer_id:22,date:"2021-05-08",date_name:"Saturday",discount_type:"price",discount_amount:"50.00"}],
  
  map = new Map(offers.map(o => [o.id, o.is_checked]));

days.forEach(o => (o.is_checked = map.get(o.offer_id)));

console.log(days);
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28