Store array in jQuery cookie [duplicate] - javascript

This question already has answers here:
Create array in cookie with javascript
(10 answers)
Closed 9 years ago.
I am storing array in cookie through jQuery cookie plugin but each time I get it from cookie, it returns empty
I define array as:
var myArray = [];
myArray[ 0 ] = "hello";
myArray[ 1 ] = "world";
Set it in cookie
$.cookie('cookie-array', myArray);
But getting this cookie and printing, prints empty string
console.log($.cookie('cookie-array'));
EDIT:
Arrays define in other questions are object arrays no like this array I mentioned here. Also I dont want to user JSON library.

Have a look at:
https://code.google.com/p/cookies/
to store an array
$.cookie('COOKIE_NAME', escape(myArray.join(',')), {expires:1234});
to get it back
cookie=unescape($.cookie('COOKIE_NAME'))
myArray=cookie.split(',')

Related

How to make all objects into one array and remove duplicates? [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 1 year ago.
I have this object, and I want Remove duplicates and make it into one array
var sports = [
['basketball','fotball','racing'],
['fotball','basketball','swimming'],
];
What is the best way to get it like this:
['basketball','fotball','racing','swimming'],
The flat() will make the sports into one array, and is probably the best option here?
Just have to remove the duplicates any tips?
use below:
[...new Set(sports.flat())]
Here sports.flat() flatten out the array. See the doc here
and new Set() will make them unique. See the doc here

Creating dictionary using python from javascript var data [duplicate]

This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Make dictionary from list with python [duplicate]
(5 answers)
Convert list into a dictionary [duplicate]
(4 answers)
Closed 4 years ago.
trying to figure out how to do this and have yet to find a good solution. I pulled this data out of an XML response. It was in a var tag. Now what I would like to do is create a dictionary out of it. The domain.com should be paired with the number right listed behind it.
This is the data:
[
'cb131.domain1.com', '147827',
'cb143.domain2.com', '147825',
'cb175.domain1.com', '147454',
'cb190.domain.com', '146210',
'cb201.domain.com', '146208',
'cb219.domain.com', '146042',
'cb225.domain.com', '146282',
'cb900.domain.com', '148461',
'cb901.domain.com', '148493',
'cb902.domain.com', '148495',
'cb903.domain.com', '148497',
'cb904.domain.com','148499',
'cb905.domain.com', '148501',
'cb906.domain.com', '148503',
'cb907.domain.com', '148505',
'cb908.domain.com', '148507',
'cb909.domain.com', '148509'
]
So for example cb131.domain1.com should be paired with 147827, cb143.domain2.com paired with 147825 and so on.
Drawing a blank on a good quick solution on how to do this. Hopefully someone can help.
Thanks!
Edited with answer I choose below:
I choose this answer and also to help anyone else I add a nice way to print out the results (data is the string I obtained):
import ast
i = iter(ast.literal_eval(data))
dic = dict(zip(i, i))
for key , value in dic.items():
print(key, " :: ", value)
This should do it. Assuming the list is saved to a variable l:
keys = l[::2]
vals = l[1::2]
dic = dict(zip(keys, vals))
You can create an iterator from the list after using ast.literal_eval to parse it from the input text, zip the iterator with itself, and pass the generated sequence of tuples to the dict constructor:
import ast
i = iter(ast.literal_eval(data))
dict(zip(i, i))
Assuming you have the above in a python array called data, you can do:
new_data = []
for i in range(0, len(data), 2):
new_data.append((data[i], data[i+1]))
Now new_data would be a list of tuples. You could certainly create a better data structure to hold these pairs if you want.
I do not yet know Python that I can write a snippet, but:
initialize an empty dictionary in Python
create a for loop counting index from 0 to length of your array in steps of two.
inside add a dictionary entry with key of value at index and value at index + 1
perhaps check for duplicates
Does this answer help you?
This is Python - quickly google'd:
dictionary = { }
for idx in range(0, len(data), 2)
dictionary[data[idx]] = data[idx + 1]

Why am I getting undefined getting data from my json? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
In my angularjs file I stringified my json result
console.log("details... "+JSON.stringify(response));
it is somewhat of this nature
{"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE",
when I try to console for firstname I get a surprising outcome of undefined
console.log("firstname... "+ response.data.data.firstname) ;
please what could be wrong
The response.data.data is an array, that's what the square brackets are []. You must first access the item itself before accessing the firstname attribute on that item.
For example, to get the first item, you would do
console.log(response.data.data[0].firstname)
Instead of:
console.log("firstname... "+ response.data.data.firstname) ;
Do:
console.log("firstname... "+ response.data.data[0].firstname) ;
This happens because firstname is a key to an object which is inside an array named data.
Your final data is an array. You have to specify index on that:
var response = {"data":{"success":true,"errorCode":0,"data":[{"id":1098,"surname":"Tony","firstname":"Wilson","othername":"","dob":"Jun 9, 2000 12:00:00 AM","gender":"MALE"}]}};
console.log("firstname... "+ response.data.data[0].firstname)

Get data from JS array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Probably very easy question, but I couldn't find solution.
How to get data from this object?
That how it looks in consolo.log()
UPDATE:
Thank you for you answers.
That what I used before and it worked, but when I try on this array it returns error.
console.log(array[1].data);
Output picture
UPDATE2:
So I tried to make it a text, but I couldn't.
console.log(tempArray);
console.log("String: " + tempArray.toString());
console.log("Stringify: " + JSON.stringify(tempArray));
Here is output:
Stringify attempt result
Maybe there is something wrong with how I create this array.
let tempArray = [];
And in the loop
tempArray.push({"id": id, "data": data.routes[0].geometry});
Thank you,
Dmitry
That's an array of objects, so you would get elements from it like so:
console.log(obj[i].data)
Where i is the element (numbered 0 through 2) that you want to access.

how to sort json data by date [duplicate]

This question already has answers here:
How to sort an object array by date property?
(25 answers)
Closed 8 years ago.
i want to sort my json response by date ,here is the javascript function
function successCallback(responseObj){
$.each(responseObj.allRecom.recom,function(index){
var data=JSON.stringify(responseObj); //contains json response
alert(responseObj.allRecom.recom[index].recomDate);//it contains date value from response.
var date=new Date(responseObj.allRecom.recom[index].recomDate);
date=date.toLocaleString(); //converted to local date string
});
}
now what i want is whatever the response will be come ,it should be sorted by date.thanks
Put every object in an array and then call these function:
dates.sort(function(a,b){return a-b}); //asc
dates.sort(function(a,b){return b-a}); //desc
just change 'dates' to corresponding name of the array

Categories