H
H
hobu4ok912016-04-23 10:25:07
JavaScript
hobu4ok91, 2016-04-23 10:25:07

How to add an object to an array?

I created a factory in angularJs, and it has the following functions:

var cart = {
itemsCookie: '',

 init: function(itemsCookie) {
            this.itemsCookie = itemsCookie;

            if (!($cookies.get(this.itemsCookie) instanceof Array)) {
                $cookies.put(this.itemsCookie, []);
            }
        },

addItem: function(item, quantity) {

            // Устанавливаем значение по умолчанию для кол-ва
            if (quantity === undefined) quantity = 1;

            var items = $cookies.get(this.itemsCookie);

            items.push({
                id: item.id,
                quantity: quantity,
                price: item.price
            });

            $cookies.put(this.itemsCookie, items);

        }
}

in the console gives the following error:
Error: items.push is not a function

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question