user1382306
user1382306

Reputation:

set property of Object in Array

I have an Array of Objects like so:

manualInputHeaders = [
    {text: 'Store No', title: 'Enter Destination Store Number'},
    {text: 'Promise Date', title: 'Enter promise ship data', classes: {classAdditions: 'tooltipInput'}}
]

I'm trying to set with

manualInputHeaders[0].classes.classAdditions = 'setMe'

However, chrome console gives:

Uncaught TypeError: Cannot set property 'classAdditions' of undefined

How can a property of an Object within an Array be set?

Upvotes: 0

Views: 72

Answers (2)

SteveP
SteveP

Reputation: 19093

Element zero doesn't have a classes attribute, which is why classes.classAdditions gives the an undefined error. Create the classes element and all should be well.

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71918

You have to create the object first (if it doesn't already exist):

manualInputHeaders[0].classes = manualInputHeaders[0].classes || {};
manualInputHeaders[0].classes.classAdditions = 'setMe';

Upvotes: 3

Related Questions