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));
Related
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))
I'm trying to query all data from an array in Firestore, but the certain array is made of objects. This is what my DB looks like:
https://imgur.com/a/4n1A2mb
You guys have any idea how I should do it?
This is the code I'm trying to achieve my goal with:
sawroomDB.collection("reservationsSecure").doc(todaysDate.getFullYear().toString()).get().then(doc => {
tableAllReservationsBody.innerHTML = doc.data().allReservations;
console.log(doc.data().allReservations)
})
The output I'm getting printed is just "[object Object],[object Object]" and in the console I can see and browse the arrays
What I need is to see the whole information from the objects, just by calling the array.
Thanks!
doc.data().allReservations is going to be an array of objects, as you can see from the console output. From what we can see in the screenshot, those objects contain at least one property called booking. If you want reach into the first object and get the value of the booking property:
const allReservations = doc.data().allReservations
const r1 = allReservations[0]
console.log(r1)
const booking = r1.booking
Of course, you might want to iterate the allReservations array instead of blindly indexing into it. But the point here is that each object just has that properties that you see in the console. You'll be able to see better when you log each object individually as above, rather than trying to log the entire array, which might compress down the contents of each object from your view.
I want to access a variable value of a Javascript object. The below image is a result of a console.log: console.log(variable).
I tried:
variable[0] -> returns undefined
Also tried converting using JSON.stringify -> shows '[]' on the console
Here's the screenshot of the value I want to get:
Assuming the array variable is the array of two objects, to get the string "tag1", you should try variable[0].name.
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 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.