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.
Related
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Iterate through object properties
(31 answers)
How to iterate over a JavaScript object?
(19 answers)
Closed 1 year ago.
I have an array of data like:
var messageArr = [
messageA: [
weight: 1,
type: "pop up",
content: "lorem ipsum",
],
messageB: [
weight: 3,
type: "tile",
content: "Hello world",
],
]
I am looping through it like:
for (var key of Object.keys(messageArr )) {
console.log(messageArr.content);
}
But I cannot output the nested array items - nothing appears in console.
Would anyone know how I can get this to work?
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);
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I have multiple JSON objects which are have multiple JSON objects.
Now how do i iterate the Objects in deep.
My JSON structure is like below.
{
"phanim": {
"msg1": {
"data": "Hii ra..",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
},
"msg2": {
"data": "How r u?",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
}
}
}
Probably a recursive function - check if the typeof the current iterated property is an object, if so, iterate that!
var iterateObj = function(obj) {
for (var key in obj) {
if (typeof obj[key] === "object") {
iterateObj(obj[key]);
} else {
console.log(obj[key]);
}
}
}
Note: this won't work if one of your properties is an array - you'll need an additional check and logic for that.
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.