Javascript — How to assign multiple elements to an object?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Javascript — How to assign multiple elements to an object?



I am trying to assign multiple elements to an object without success. The data is being overridden rather than added. I am new to this, therefore I am unsure if Object.assing() is the right option. How can I add multiple sets of data to an object?


Object.assing()



Pseudo-code



Code


_selectDate = obj =>
let selection =
const startDate, endDate = this.state

if (!startDate)
Object.assign(selection,
[obj.dateString]: startingDay: true, color: 'black', textColor: 'white' ,
)
else
Object.assign(selection,
[obj.dateString]: endingDay: true, color: 'black', textColor: 'white' ,
)


this.setState(
startDate: !startDate,
test: selection,
)



Outcome desired



'2018-08-20': startingDay: true, color: 'black', textColor: 'white' ,
'2018-08-21': selected: true, color: 'black', textColor: 'white' ,
'2018-08-22': selected: true, color: 'black', textColor: 'white' ,
'2018-08-04': endingDay: true, color: 'black', textColor: 'white' ,





Can you please post what is the input for the desired outcome?
– DSCH
Aug 12 at 11:48





DRY. And is endingDate before startingDay?
– mplungjan
Aug 12 at 11:49





Sorry @DSCH but what do you mean by input? The data comes from a click in a calendar (react-native-calendars). Based on the click, I am structuring the data in the way required by the module [obj.dateString]: startingDay: true, color: 'black', textColor: 'white' . That gets passed to the calendar and the day gets highlighted. Unfortunately everytime I click, the data gets overwritten instead added. @mplungjan No, startingDay should be the first, followed by endingDay.
– Diego Oriani
Aug 12 at 12:01


[obj.dateString]: startingDay: true, color: 'black', textColor: 'white'




1 Answer
1



The problem that you create new object on each call:


let selection =



That is why you cannot 'accumulate' this data.



For fix this you should get it from your state, like this:


let selection = this.state.test



And then (in if statement) make assignment:


selection =
...selection,
[obj.dateString]:
endingDay: true,
color: 'black',
textColor: 'white'
,





Let me try this. Brb.
– Diego Oriani
Aug 12 at 12:14





it works. It fixes the issue. Now I need to make it DRY and figure it out a way to get the in-between dates. One last thing, why the ...selection? What does it do? Thank you for your help by the way.
– Diego Oriani
Aug 12 at 12:25


...selection





...selection gets all existed fields of selection. And then we construct a new object adding to existed fields new one.
– Max Farsikov
Aug 12 at 12:32


...selection


selection






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard