0

I'm very new to JavaScript. I was trying pushing new values to an object. But when I tried to push an array that doesn't exist in myObj, I got Cannot read property 'push' of undefined error. My code is below:

var myObj = {
    first: "first value",
    second: "second value",
}

myObj.third.push(123)

console.log(myObj)

But when I check if it doesn't exist and create an empty array, then push it, it works. =>

var myObj = {
    first: "first value",
    second: "second value",
}

myObj['third'] = myObj.third || []

myObj.third.push(123)

console.log(myObj)

I wonder if bottom code is best practice and if there is a better way to do it.

8
  • 3
    Looks like you solved your own problem, you can't call push on a array until you initialize it. Commented Feb 6, 2020 at 22:35
  • 1
    You can also initialize the property with the initial value that you want to push, like so: myObj.third = [123], from there, you can use .push() to add values Commented Feb 6, 2020 at 22:35
  • push is a method on Array. To create a property on an object you simply assign something to it. Commented Feb 6, 2020 at 22:36
  • I was expecting it to do in single comment ( creating a property that doesn't exist and pushing the value ). But it's seems my bottom code is way to go. @WilliamLohan Commented Feb 6, 2020 at 22:40
  • @Teyyihan Aksu ooooh you want myObj.third = [123]; Commented Feb 6, 2020 at 22:41

2 Answers 2

1

The push method is part of the Array prototype in your particular case you are trying to access a not defined property which in JavaScript is consider undefined so you are trying to do transalates into something like:

myObj.third.push => myObj.undefined.push

Because myObj does not have a third key you can try this in the console doing console.log(myObje.third );

So the engine returns an error saying that undefined does not have a property push that is only available in Array.

In this case you need to set assign a value to myObj.third before using it as an array, for that there are multiple ways to set the value as an array your answer above is one of those ways:

myObj['third'] = myObj.third || []

That line works however if for example myObj.third has a value will use that as a value as you are assigning the result of the operation myObj.third || [] which in this scenario anything that evaluates as truthy will be returned if for instance you have:

myObj.third = true; 

And then you do:

myObj.third.push(123);

An error will be displayed Uncaught TypeError: myObj.third.push is not a function as currently myObj.third is not an array.

So you need to make sure you are operating over an Array when using push, or at least when you initialize the value of your property is set as an Array, you can find more details on Array construction on this page.

Alternatives you can do to check before pushing are:

if ( ! Array.isArray(myObj.third) ) {
   myObj.third = [];
}

myObj.third.push(123)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes i tested the true error. But we will overcome this issue with a if(myObj.third === Array) condition. Then we will be sure it's an array
1

Updated 2nd:

You could do a ternary operator

let myObj = {
  first: 'first value',
  second: 'second value'
};

myObj.third = myObj.third ? [...myObj.third, 123] : [123];

console.log(myObj);

2 Comments

Yes i can. But when i would fetch data from API, i won't be able to tell if property is there or not until if condition. I was expecting it to do in single line of code.
Updated answer above

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.