I have stored an object with multiple properties in localStorage. It does not contain a single value like a string or a number. The name of the localStorage key is UserData and its value is the following object:
{
key: "1287C31D714BE16FBD44D093E4173CFF"
logTime: "20191013190439"
operatorDni: "46653980"
}
I need to retrieve the value of the object property operatorDni in order to perform some actions in my code. I have tried to retrieve it using this line of code:
operatorDni: string;
this.operatorDni= localStorage.getItem('UserData.operatorDni');
But I get null.
How can I get a property of an object in localStorage with the key? What am I doing wrong?
Thank you very much.
Use JSON.parse like
var userData= JSON.parse(localStorage.getItem('UserData'))
this.operatorDni=userData.operatorDni;
Also save it like
localStorage.setItem('UserData',JSON.stringify(yourObject))
Related
Is there any way to capture an attribute's alternate names in NodeJs?
e.g An API accepts an object as an input called requestBody, which could send a field called mobileNumber or mobile_number or mobNumber or contact_number or contactNumber etc,
And I wish to store them in a variable called contact
In Java there's annotation support via a library like fasterxml.jackson to do so
e.g
class Technology {
#JsonProperty("contact")
#JsonAlias({"mobileNumber", "mobile_number", "mobNumber", "contact_number"})
private String contact;
}
Can I do something similar in Node while parsing into a variable?
Tried the following -
If only one of those properties will be filled out I would use the property names as strings in an array and use Array.find() to get a value for the first match. If there is no value it will return undefined which is falsey and if there is a value it will be truthy and return the value from that property.
const props = ["mobileNumber", "mobile_number", "mobNumber", "contact_number"];
const mobile = props.find((prop) => requestBody[prop]);
The scenario is there will be three javascript files named js_file1.js , js_file2.js and js_file3.js.
I need to declare a JSON object in js_file1.js like the below one :
let configObject = {
key1:"Value1",
key2:"Value2"
};
now I am exporting it by:
module.exports={
configObject:configObject
};
now I am using it in js_file2.js by js_fileObject1.configObject and adding one more key value pair to it like:
js_fileObject1.configObject.key3 = "value3";
now I need to access this updated object into js_file3.js.
any suggestions to achieve this ? I am losing the last key value pair in js_file3.js and only the original one is coming.
I'm trying to pass a JSON object using the function session.Storage, in order to keep the object browsing between pages. The JSON object looks like this:
var shapeResult={"accuracy":
{"Syntactic":[], "Semantic":[], "Data_Assurance":[],"Risk":[]},
"completness":
{"Record":[], "Attribute":[],"Completness":[]},
"consistency":
{"Integrity":[]}
};
In my page there's a function that first assigns some values to the empty arrays (as strings) :
var shapeResult={"accuracy":
{"Syntactic":[ID,EMAIL] "Semantic":[ID]}
};
For each of these single value my Function will assign a 0, or a 1. In this way, accessing, for example the object, with this expression:
shapeResult.accuracy.Syntactic
I would obtain either a 0, or 1.
Then I try to save it in the session storage trough
session.Storage.setItem('session_data',JSON.stringify(session_data_temp));
session_data_temp=JSON.parse(session.Storage.getItem('session_data'))
What I obtain from the sessionStorage is the first JSON object, without the added values in the array and the 0's and 1's.
What's the problem?
Use sessionStorage instead of session.Storage
DEMO
var shapeResult={"accuracy":
{"Syntactic":1}
};
sessionStorage.setItem('session_data',JSON.stringify(shapeResult));
var session_data_temp=JSON.parse(sessionStorage.getItem('session_data'));
console.log(session_data_temp);
I wish to store some data dynamically in an array, using Javascript. My goal is to save a configuration, that the user has specified, and later store it in my database.
I am trying to create a array like this:
var config = [];
config.push({id:100,name:'Yashwant',age:30});
config.push({id:200,name:'Mahesh',age:35});
However, when I print out the config array, like this:
alert(config);
it prints:
[object Object],[object Object]
How can I store and push values to my array, dynamically?
EDIT:: Seems like I am storing the values correctly. How can I access it?
Alert casts the config parameter to string. If you want to see the value for debugging purposes, it is better to use console.log(config).
Or you could use JSON.stringify(config) to convert it to JSON string.
To access the values, you can do this: console.log(config[0].age, config[1].age);
If you want to print values of any javascript object you can use JSON class to either stringify object data or parse stringified data (JSON) back to object.
alert(JSON.stringify(config));
I have a variable :
var testData;
And I have a function that populates an array. Goes through an array and makes another array like so :
var person = {
"Name": obj.Name,
"Age": obj.Age,
}
partsObject.push(person);
I then want to make this array into JSON so I can use it with my D3 objects, so I do this :
testData = JSON.stringify(partsObject);
I can console log this variable, but when trying to go through it via D3's forEach method like so :
testData.forEach(function(d) // data is the JSON
{
I get the error Uncaught TypeError: testData.forEach is not a function
I don't understand how I can log the variable to the console yet it's as if I can't use it as JSON. Any ideas ?
As the name suggests stringify() converts a JavaScript object (the JSO in JSON) into a string of JSON. You can console.log() it because console.log expects to take a string, and anything that's not a string is converted to one to be displayed.
If you want to use it as an array again, you need to parse your string of JSON back to the JavaScript object: JSON.parse(testData).
You really dont need to stringify your Array to pass to d3. Do not to get confused with javascript objects, since forEach requires an array to loop through and you are passing a string to manipulate with forEach function
use:
partsObject.forEach(function(d)
{
...
JSON.stringify(partsObject); creates a string as"{'Name':'ABC','Age':23}"
Uncaught TypeError: testData.forEach is not a function caused because javascript was not able to find an Array
.stringify() turns a Javascript Object into a string. You would want to either run
partsObjects.forEach()
or alternativily you could turn the stringify'ed string back into an object with
(JSON.parse(testData)).forEach()
You are currently trying to loop through a String since you stringify your array.
Just do partsObject.forEach and don't stringify your Array.