0

I need a help in this question:

I have objects with arrays. I need get the values in the arrays to send to the server.

My question in Image:

screenshot


This is the code I print in the console:

var cart = sharedCartService.cart;
console.log(cart);

How I can get this datas?

2
  • 1
    From your image, you don't have two objects with arrays, you have one array with two objects. Also, it's not very clear what you are asking. You access array data with standard array notation: cart[0].cart_item_id etc. Commented Jan 26, 2017 at 17:12
  • what is that you are looking for ? Commented Jan 26, 2017 at 17:12

1 Answer 1

1

In javascript either you put values in an array:

var array = [1, 2, 3, 4];

that can be objects:

var array [
 {id: 1},
 {id: 2}
];

But you can't put key value pairs in an array

// not valid
var array = [id: 1, name: sdf];

Only objects can have key value pairs, arrays only have values.

In your json the keys add, decrement, find, etc are key value pairs and making your array not valid.

What you need to do is to put those in a third object or make the whole thing an object by replacing [] by {}

Once you have a valid object or array you can use for in to loop over the properties of your object or calling them directly with the dot notation or looping through the objects in your array.

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/for...in

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

Comments

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.