-1

Can anyone tell me how can i achieve the following

activity = {}   // can have hotel , transport etc
day = [ {day: 'DAY01'} , {day: 'DAY2'}]

i want to add activity.hotel to day 01 object in day array?

1

1 Answer 1

0

First get the precise day you want, for instance with find.

Then add a new key to the day object, by using the bracket notation, and assign your activity object.

let activity = { hotel: 'TheHotel' }   // can have hotel , transport etc
let days = [ {day: 'DAY01'} , {day: 'DAY2'}]

// get the particular day to edit
let day = days.find(d => d.day === 'DAY01')
// attach the activity.hotel object you want. 
day['hotel'] = activity.hotel;

console.log(days)

or add directly the whole activity object, if you want :

let activity = { hotel: 'TheHotel' }   // can have hotel , transport etc
let days = [ {day: 'DAY01'} , {day: 'DAY2'}]

// get the particular day to edit
let day = days.find(d => d.day === 'DAY01')
// attach the activity object you want. 
day['activity'] = activity;

console.log(days)

Sign up to request clarification or add additional context in comments.

1 Comment

@mush I just edited my answer to show alternative, I read again the question and wanted to add only the "hotel" part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.