Page 1 of 1

Local Storage Variable Array

Posted: Sat Jan 30, 2016 11:52 pm
by S Butler

Hi,
I have a request that returns a "results" array with applicable records from my database table. On success, I would like to store, in an array local storage variable "locations", only the _id values from each record in the response array. Unfortunately, when I use localStorage.setItem("locations", JSON.stringify(data)); it saves all values from all records from the response in the local storage variable array when I only want the _id values saved. Below is what is currently being saved in my local storage variable array:

{"results":[{"createdAt":{"$date":"2016-01-29T20:58:16.089Z"},"officeTel":"64562234","officeHours":"9AM-5PM","id":"56abd268e4b0e9b36b46c617","address":"2501 W. Kennedy Boulevard Tampa, FL 33609","updatedAt":{"$date":"2016-01-29T22:10:23.532Z"},"name":"Kennedy Family Care Center"},{"officeTel":"1513451346","id":"56abd293e4b0e9b36b46c618","address":"2106 South Lois Avenue Tampa, FL 33629","name":"Lois Family Care Center","officeHours":"9AM-5PM","createdAt":{"$date":"2016-01-29T20:58:59.660Z"},"updatedAt":{"$date":"2016-01-29T22:10:21.080Z"}},{"officeTel":"351341513","updatedAt":{"$date":"2016-01-30T14:44:58.570Z"},"servicesOffered":"Cardiology","name":"Manhattan Family Care Center","officeHours":"9AM-5PM","id":"56abd2bfe4b0e9b36b46c619","_createdAt":{"$date":"2016-01-29T20:59:43.213Z"},"address":"4212 S Manhattan Avenue Tampa, FL 33611"}]}

I would like the local storage variable "locations" to look like this instead (with only _id values from above)

{ "_id": [56abd268e4b0e9b36b46c617, 56abd293e4b0e9b36b46c618, 56abd2bfe4b0e9b36b46c619] }

Is there a snippet of javascript that could help me accomplish this? Thanks!


Local Storage Variable Array

Posted: Sun Jan 31, 2016 12:23 am
by leven.steve

I think you want to use the Underscore "pluck" function:
http://underscorejs.org/#pluck

I havent tried it but I think it'll be like this

var myIDsOnly=.pluck(myArray[0].results,'id');

Someone else may correct this syntax.


Local Storage Variable Array

Posted: Mon Feb 01, 2016 7:42 am
by Serhii Kulibaba

Hello,

Please use JS below for that:

prevar locations = [];
$.each( data.results, function( key, value ) {
locations.push(value._id);
});

Apperyio.storage.locations.set(locations);/pre


Local Storage Variable Array

Posted: Mon Feb 01, 2016 2:56 pm
by leven.steve

Noted! Thanks Sergiy
This is the sort of thing that would be very useful in a "hints and tips" section of your documentation.