I want to ask you something guys, I hope you can help. I am confused how to remove last value from localstorage? the localstorage value I have as below, I want to delete the 'Sprints' value. If i use localStorage.removeItem('tags'), all values will be deleted right? but I just want remove the last value. please help me, cause i'm confused
localStorage is only capable of storing string values so what you've got there is an array, serialised to JSON.
To remove elements from it, you'll need to
Parse the string as JSON to an array
Remove the item
Stringify the array and put it back in localStorage
const tags = JSON.parse(localStorage.tags ?? "[]")
localStorage.tags = JSON.stringify(tags.slice(0, -1))
You can of course single-line this
localStorage.tags = JSON.stringify(JSON.parse(localStorage.tags ?? "[]").slice(0, -1))
Related
I try to store data from user-input in an object in order to be able to store the data in localstorage. My problem is that I cant figure out how to make it work. When I write something as value inside the object (data) it works fine but the user-input cant be stored as value in my object.
Any suggestions?
Thank you for your answers.
You can only store strings in localStorage, so you need some way to convert the object to a string first. JSON.stringify() does this.
var obj = {};
obj.userInputValue = "Pretend a user entered this";
var objAsString = JSON.stringify(obj);
localStorage.setItem("myObject", objAsString);
var jsonString = '{"DeviceId":3,"results":{"1":"[{\"x\":513,\"y\":565,\"width\":175,\"hight\":208}]"}}';
var message = JSON.parse(jsonString);
I got an error saying Unexpected token u in JSON at position 0
at JSON.parse.
Could you please guide me what's wrong?
THanks in advance!
At the last few characters looks wrong. The :212 has no sense as the value (that long array) for key "1" was already set, so that later :212 looks weird
Also enclosing it in single quotes it makes that all be like a huge string, and not as an array structure.
See Results key as value contains a sub array which contain "1" key which as value contains a string enclosing another json array (but escaped as plain string, so no structurally accesible for the main object . But that string if post -processed the :212 is paired to what? , no key, no comma neighter , to the precedent whole array which already was the value, not the key?. Anyway weird.
In your JSON string, there is wrong something with ":212", as it's not valid JSON, because it doesn't have any property that it's mapping the value for. For example, you are mapping values for width and height with properties keys. But for "212", there is no property.
Here is the above JSON formatted:
var jsonString = '{"DeviceId":"3","results":{"1":"[{\\"x\\":513,\\"y\\":565,\\"width\\":175,\\"hight\\":208}]"}}'
var message = JSON.parse(jsonString);
If you want to format the results, you can do to it, there is no error on it:
JSON.parse(message.results['1'])
Here is the JS Bin link for above code: https://jsbin.com/fiyeyet/edit?js,console
Just an advice
Professional code is all about proper spacing, proper identation , proper commenting, don't try to write down all within one single line, structure it VISUALLY nice to see nice to read nice to comprehend, and you will be approved in most jobs.
Hint: declare a normal array/object , convert it to json string using the proper function, then use the string variable returned by the function to test your code or whatever doing. That way, you can write down in the source really nice the structure.
I have made a webpage on JSFiddle that can display a simple JSON string. I plan on implementing ways to add/edit/remove keys and values, which is what the buttons on the right are for. Before adding all that, how do I go from this webpage representation back to a JSON string with the same formating when I click 'Save'?
Do I need to tag each element specifically some how and do a forEach(element) and extract the text value and then..?
Code on JSFiddle
<script>
var new_json = {"RegEx":{}}
var keys = document.getElementsByClassName("json_header")
for (key in keys){
//Somehow extract key value that is displayed, add to new_json as a key.
// for each key, need to add each value.
for (value in values){?}
}
Somehow I need to know which values belong to which key, is this possible with parent/child associaton?
In the JSFiddle, I append all the values as children to the key value
New to HTML and Javascript, any other suggestions are welcome.
Thank you!
I'm trying to take the hashed value from an object. What I'm basically doing is this:
target = $('a[href^="#products"]');
targetHashed = target.hash;
$targetHashed = $(targetHashed);
console.log(targetHashed);
I'm putting the reference in "target", then getting the hashing and everything following it with ".hash" then converting the variable that contains the hashed value "targetHashed" to an object so I can do things like getting the offset, etc. Problem is that "targetHashed" is outputting undefined whenever I try to append .hash to it. Anyone know where I'm going wrong?
If you want to get the value of the href attribute of the selected elements you need to use attr so your second line would look something like.
targetHashed = target.attr('href');
But that only selects the first element, if you want to use all of them then you'll need to loop through that array.
When i add new category, i am storing child category in serialize format, Which works just perfect. Like :-
a:1:{i:0;s:2:"41";} But when i edit it back and try to change it to other category, which are loaded via Ajax-Json Javascript. But value is still 41 as above. It stores value in below format :-
s:2:"41";, Which is wrong and i get error while going back and editing it again.
Does any one know how this serialize failed to store in proper format ?,
Thanka
I got it Working, Oh, I had to dig and Understand what is serialize first and how php understands and converts it in serialize format.
Here you go from php.net
String s:size:value;
Integer i:value;
Boolean b:value; (does not store "true" or "false", does store '1'
or '0')
Null N;
Array a:size:{key definition;value definition;(repeated per
element)}
Object O:strlen(object name):object name:object
size:{s:strlen(property name):property name:property
definition;(repeated per property)}
That means, I was not sending value as array, and since value was not getting sent in array via HTML form, it didn't appended a for array tag. :)
And when i made my HTML``input field name as category[] from category, Everything worked as expected :)
Thanks
How are you editing it (code sample)?
You should unserialize it first, then modify, and serialize again.