php merge two JSON arrays into one object [duplicate] - javascript

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)))

Related

Get property from objects to convert to simple array - javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have this array:
[
{
"id": "5e235b42a21b160a40c4a82f",
"title": "category one"
},
{
"id": "5e235b3ea21b160a40c4a82e",
"title": "category two"
}
]
And I want to convert it to the following array:
[
"5e235b42a21b160a40c4a82f",
"5e235b3ea21b160a40c4a82e"
]
Anyone have a simple suggestion?
The best way to do this is by iterating over the Object values and taking each id value.
The simplest way of doing this is by using .map() like so:
const newArray = myArray.map(item => item.id);
This should take each item and form the id property into an array.

Converting a JSON object to array? Read description [duplicate]

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);

Store an Array in sessionstorageItem [duplicate]

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));"

What is the correct format of populating a JSON object with a nested array of objects?

I'm trying to create a JSON object with a nested array of JSON objects. What is the correct format of this?
Here is an example what I am trying to create:
{
"reviewCount": 96,
"reviews": [
{"name": "Sean Steinman", "date": "reviewed 2 weeks ago", "reviewContent": "Fantastic Service"},
{"name": "Ryan Lundell", "date": "reviewed in the last week", "reviewContent":"Ask for Scott!"}
]
}
Here is what I have so far:
var reviewObj = {
reviewCount: reviews.length,
reviews: [{name: , date: , reviewContent:}]
}
After I initialize it, I will fill it with a for loop that runs through an existing array of strings.
CLARIFICATION:
The array that I'm using to populate the JSON object is:
[
"\nSean Steinman\nreviewed 2 weeks ago\n Fantastic Service\n",
"\nRyan Lundell\nreviewed in the last week\n Ask for Scott!\n• • •\n"
]
So I'm creating a new array in my for with tmpArr = reviews[i].split('/n');, and then where I'm getting stuck is how to stick that into the JSON object as an object.
First, you're not building a "JSON" object. You're just building an object. It's not JSON until you JSON-encode it. {"name": "bob"} is not JSON, it's an object literal. '{"name": "bob"}', the string, is JSON.
Second, you cannot loop inside an object literal, which is what your second code example seems to indicate you're trying to do. Instead, you need to initialize you reviews property to an empty array, and then loop and append items to the array.
var reviews = [
"\nSean Steinman\nreviewed 2 weeks ago\n Fantastic Service\n",
"\nRyan Lundell\nreviewed in the last week\n Ask for Scott!\n• • •\n"
];
var reviewObj = {
reviewCount: reviews.length,
reviews: []
}
reviews.forEach(function(line) {
var review = line.split("\n");
reviewObj.reviews.push({name: review[0], date: review[1], reviewContent: review[2]});
});

iterate json array in javascript [duplicate]

This question already has answers here:
draw google graph from json [closed]
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Parse JSON in JavaScript? [duplicate]
(16 answers)
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
hi i have generated following json , can i iterate through this
i have validated this josn using some sites
following is just example there can be more section like MATHS, LOGICAL REASONING ,ENGLISH
they can also have their individual types
{ "MATHS": [
{
"Section": "MATHS",
"Type": "INCORRECT",
"Count": "5"
},
{
"Section": "MATHS",
"Type": "NOT SOLVED",
"Count": "20"
}
],
"LOGICAL REASONING": [
{
"Section": "LOGICAL REASONING",
"Type": "CORRECT",
"Count": "1"
},
{
"Section": "LOGICAL REASONING",
"Type": "INCORRECT",
"Count": "4"
},
{
"Section": "LOGICAL REASONING",
"Type": "NOT SOLVED",
"Count": "20"
}
]
}
i have searched on may question on stack overflow but none of them can help me
Your JSON has 2 top-level elements. So, you can't logically iterate across the entire document (without flattening it).
But you can iteration across the "MATHS" and "LOGICAL REASONING" elements.
For example, using underscore:
_(data.MATHS).each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
Note the different access method to the 'LOGICAL REASONING' element, because of the space.
_(data['LOGICAL REASONING').each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
If you wanted all the data together, one way of doing it would be:
var flattened = _.union(data.MATHS, data['LOGICAL REASONING']);
_(flattened).each(function(item) {
// Process...
});
Check out the underscore docs for some more information about the possibilities. Many ways to skin a cat.
http://underscorejs.org/
Cheers.

Categories