-1
var parsed = JSON.parse($.cookie('inavm_inventory'));

inventoryID = parsed['ids'];
inventoryName = parsed['names'];
inventoryCount = parsed['productcount'];

I have a cookie "inavm_inventory" which has 3 array's within it, "ids", "names" and "productcount".

I also have 3 local variables as you can see above.

Currently adding to the cookie all works fine - but when a user loads the page I'm trying to push the cookie arrays into the local variables.

It all works fine without the code above, but when using the code above - it breaks the "array". I've logged the values and they look like arrays, but I'm guessing they may be formatted as strings.

I'm just trying to format them back to arrays so that .push works on my arrays again.

Cookie example data:

{
    "ids": "[\"quant[2]\",\"quant[3]\"]",
    "names": "[\"2 3 Seater Sofa\",\"0 2 Seater Sofa\"]",
    "productcount": "[8,0]"
}
6
  • 1
    It's really hard to help with only the code you posted. (Also note that parsed['ids'] can be written as parsed.ids.) Commented Feb 13, 2017 at 15:27
  • The way you're using parsed, it doesn't look like it's an array. So it's not surprising if you call parsed.push(...) and it doesn't work (if that's what you're even doing). Commented Feb 13, 2017 at 15:30
  • Cookie data example: {"ids":"[\"quant[2]\",\"quant[3]\"]","names":"[\"2 3 Seater Sofa\",\"0 2 Seater Sofa\"]","productcount":"[8,0]"} Commented Feb 13, 2017 at 15:31
  • Great! Use the "edit" link to show that, not a comment. (Be sure to format it.) Also show us what code you're trying to run that's failing. Commented Feb 13, 2017 at 15:31
  • 1
    There are no arrays in the cookie. And again: We need to see what you're trying to do that's failing. Commented Feb 13, 2017 at 15:32

1 Answer 1

0

Your cookie contains an object with three properties. The values of those properties are all strings, not arrays.

Here's how it would look if they were arrays:

{
    "ids": ["quant[2]","quant[3]"],
    "names": ["2 3 Seater Sofa","0 2 Seater Sofa"],
    "productcount": [8,0]
}

So you want to look at what's producing the cookie value and correct it so it outputs those three arrays correctly.

If you're creating it with JavaScript, this would create the above correctly (building it up from pieces; the above would also work as a single block, but that's probably not how you're doing it):

var obj = {};
obj.ids = ["quant[2]","quant[3]"];
obj.names = ["2 3 Seater Sofa","0 2 Seater Sofa"];
obj.productcount = [8,0];

Or even

var obj = {
    ids: [],
    names: [],
    productcount: []
};
obj.ids.push("quant[2]");
obj.names.push("2 3 Seater Sofa");
obj.productcount.push(8);
obj.ids.push("quant[3]");
obj.names.push(["0 2 Seater Sofa"]);
obj.productcount.push(0);

Then creating the cookie:

$.cookie('inavm_inventory', JSON.stringify(obj)));

You might consider changing the structure entirely, though, and using a single array of objects:

[
    {
        id: "quant[2]",
        names: "2 3 Seater Sofa",
        count: 8
    },
    {
        id: "quant[3]",
        name: "0 2 Seater Sofa",
        count: 0
    }
]

Then once you'd parsed it, you'd use the objects:

console.log(parsed[0].id);    // "quant[2]"
console.log(parsed[0].name);  // "2 3 Seater Sofa"
console.log(parsed[0].count); // 8

...and so on (probably in a loop).

So here you'd start with:

var products = [];

And push full items onto that array:

products.push({id: "quant[2]", name: "2 3 Seater Sofa", count: 8});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your input. Cookies do not allow arrays, that's why they are strings. I should have mentioned that I use stringify to do this, so that I can put "arrays" into my cookie using: document.cookie = "inavm_inventory=" + JSON.stringify({ids: JSON.stringify(inventoryID), names: JSON.stringify(inventoryName), productcount: JSON.stringify(inventoryCount)}) + "; max-age=2592000;";
@user3676945: Of course cookies don't allow arrays; that's why I showed using JSON.stringify above. It'll work just as well with the alternate structure as your original structure. In both cases, what we're storing is JSON, which is a string.
As advised on this post, where I originally found the solution: stackoverflow.com/questions/2980143/… To which I am using JSON.parse to retrieve the arrays back from stringify... however, it leads to my current issue of them not being of type array. Somehow need to convert them back to arrays. I tried using "inventoryID = [inventoryID]" to convert them to arrays that way, but doesn't work too well
@user3676945: It's simple: Use an array of objects. When storing that information in the cookie, you do this: $.cookie("cookiename", JSON.stringify(theArray)). When retrieving the information from the cookie, you do this: theArray = JSON.parse($.cookie("cookiename")) I suggest doing some reading on JSON, which is a textual notation (thus, strings you can store in cookies). Mind you, cookies are usually not the right answer unless you're exchanging them with a server. If you're not, use localStorage instead (also just strings).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.