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

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

Related

Getting only requested item from every object Javascript [duplicate]

This question already has answers here:
Remove property for all objects in array
(18 answers)
How to get a subset of a javascript object's properties
(36 answers)
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 2 years ago.
I am trying to figure out if I can send only relevant javascript info to the frontend from the backend because I want to hide things like ID, and others when my frontend makes a request.
To put things in perspective, here is a dummy code.
[
{
"_id": "215874514845452155",
"greeting":"Hello there"
},
{
"_id": "181474545841541515",
"greeting": "General Kenobi"
},
]
when requesting, I want to get result like:
[
{
"greeting": "Hello there"
},
{
"greeting": "General Kenobi"
},
]
I am still learning stuff and I know about loop function but I want to know if there is some neat trick to it. I am coming from R programming, we hate loops.
Use Array.prototype.map:
const input = [
{
"_id": "215874514845452155",
"greeting":"Hello there",
"other":"foo"
},
{
"_id": "181474545841541515",
"greeting": "General Kenobi",
"other":"bar"
},
];
const result = input.map(({greeting,other}) => ({greeting,other}));
console.log(result);
The above is a shorthand way of writing this:
const input = [
{
"_id": "215874514845452155",
"greeting":"Hello there",
"other":"foo"
},
{
"_id": "181474545841541515",
"greeting": "General Kenobi",
"other":"bar"
},
];
const result = input.map(x => {
return {
greeting: x.greeting,
other: x.other
}
});
console.log(result);

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.

Mongo node js , how to not return the whole object document? [duplicate]

This question already has answers here:
How to select a single field for all documents in a MongoDB collection?
(24 answers)
Closed 3 years ago.
For example on my object below. What if i dont want to return the whole object I just want to return DateInStock . How do we query that in mongo using node js ?
Object
{
"message": "success",
"data": [
{
"_id": "5ddc97ebeefab43ae69c09a3",
"VIN": "1D3HB18T29S817612",
"Body": "Quad Cab Pickup",
"BookValue": "6686",
"DateInStock": "08/01/2019",
"Description": "",
"Doors": 4,
"DriveType": "RWD",
"EngineCylinders": "8",
"EngineDisplacement": "5.7L",
"ExteriorColor": "Deep Water Blue Pearl",...
You can pass an object with the fields you want to include or exclude as a second parameter.
value of 1 will include the field and 0 will exclude it.
note that _id returns by default so you have to pass _id: 0 if you want to exclude it
db.yourColletion.find({ _id: "5ddc97ebeefab43ae69c09a3" }, { "DateInStock": 1 })

php merge two JSON arrays into one object [duplicate]

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

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