This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 7 years ago.
I have the following javascript objects. I would like to store the following array on sessionstorageItem, but it was giving me error. How can I able to store an array in sessionstorageItem?
data=[];
data[0] = [{
"num": 29,
"ser": 1,
}, {
"num": 44,
"ser": 2,
}]
data[1]=[{
"num": 10,
"ser": 3,
}]
allData = data.reduce(function (a, b) { return a.concat(b) });
// the following line gives me an error
var MDData=JSON.parse(sessionStorage.MDData);
if (MDData!==null) {console.log("Hello")}
sessionStorage.setItem('MyData', JSON.stringify(allData));
webStorage can only store strings. So you need to stringify your data.
sessionStorage.setItem('Myata', JSON.stringify(allData));
To retrieve it back you can do:
JSON.parse(sessionStorage.Myata);
you have and extra ')' in "sessionStorage.setItem('Myata', sell.allData));"
Related
This question already has answers here:
javascript filter array of objects
(8 answers)
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Closed 1 year ago.
Suppose there is an Array that has some objects like below
[{name:"Bruce wayne" age:42 DOA:true},{name: "Dick grayson" age:28 DOA:true},{name:"Jason todd" age:24 DOA:false}]
and I want to get a new Array with Objects which DOA is true (bruce and dick).
Is there any good API or something to do this?
your help is going to be appreciated Thx!
You can simply use Array.prototype.filter:
const data = [{
name: "Bruce wayne",
age: 42,
DOA: true
}, {
name: "Dick grayson",
age: 28,
DOA: true
}, {
name: "Jason todd",
age: 24,
DOA: false
}];
console.log(data.filter(obj => obj.DOA));
Mind that your JSON was also invalid.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
How will you access symbol property here in JS
console.log(hi[0]) outputs { symbol: "asdf" , name:"adad"}
but
console.log(hi[0].symbol) throws the error that symbol is undefined
hi is an array. First you need to access the object so hi[0] will provide the first object. Then access the object using the key which is 0 like below
const hi = [{
0: {
symbol: "asdf",
name: "adad"
}
}];
console.log(hi[0][0].symbol)
hi variable is an Array so you have to access the first element of the array first, then the Object property:
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
console.log(hi[0][0].symbol)
Like this?
console.log(hi[0][0].symbol)
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
hi[0][0].symbol is right.
console.log(hi[0]) => 0: {symbol: "asdf", name: "adad"}
console.log(hi[0][0]) => {symbol: "asdf", name: "adad"}
console.log(hi[0][0].symbol) => "asdf"
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I am new to javascript(json) I want to process each json object,the example prototype is given below please help me to solve the problem,I need to console it each room number given in json sample below
how can I loop through the each item in json array so that I can get individual json element and console it on browser window
let data = [
{
"king" :[
{
"id" : 1,
"room_no" : 101,
"price" : 2000
},
{
"id" : 2,
"room_no" : 102,
"price" : 3000
},
]
}
]
sorry for my bad english
Is this what you are looking for?
let data = [{
"king": [{
"id": 1,
"room_no": 101,
"price": 2000
},
{
"id": 2,
"room_no": 102,
"price": 3000
},
]
}]
data[0].king.forEach(item => console.log(item.room_no));
This question already has answers here:
Merging two json in PHP
(4 answers)
Closed 5 years ago.
I'm trying to merge two different json arrays into one object. The json arrays have different data (in terms of the data itself and the structure):
datafortable = [{"name": 3,"amount": "1190042293","category": "cars"}]
dataforchart = [{"name": 3,"amount": "5801"}]
What I would like to get is something like this:
datafortableandchart = {
"datafortable": [
{
"name": 3,
"amount": "1190042293",
"category": "cars"
}
],
"dataforchart": [
{
"name": 3,
"amount": "5801"
}
]
}
Then, in javascript I would like te be able to refer to the different json arrays like this:
dataprovider: datafortableandchart.datafortable
Is this possible?
First convert them to array then use array_merge to merge to arrays and again json_encode them
json_encode(array_merge(json_decode($datafortable , true),json_decode($dataforchart , true)))
This question already has answers here:
Converting a JS object to an array using jQuery
(18 answers)
Closed 5 years ago.
my server upon request, serves JSON from a node mysql query, but only the names row. Example:
{
"Name": "Charles"
}, etc.
How can I, get the value of Name and put it into an array? say I have this,
[
{
"Name": "Charles"
},
{
"Name": "Alex"
}
]
how can I get, Charles and Alex, into an array?
Like:
Names = ["Charles", "Alex]?
You can make use of the map function. The map method creates a new array with the results of calling a provided function on every element in this array. In your case, you need to select only the Name.
var arr = [
{
"Name": "Charles"
},
{
"Name": "Alex"
}];
var names = arr.map(x=>x.Name)
console.log(names);