How to query json data nodes using JavaScript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Closed 9 years ago.
I wish to query json result data to be used in Razor view and would like to know how to search by a specific id and capture data for a specific node:
Here is an example(just a part) of my json:
{
"maxPages":1,
"data":[
{
"id":"123",
"perfomanceData":[
{
"platform":{
"name":"Test0",
"id":0
},
],
{
"id":"124",
"perfomanceData":[
{
"platform":{
"name":"Test1",
"id":0
},
],
{
"id":"125",
"perfomanceData":[
{
"platform":{
"name":"Test2",
"id":0
},
],
I wish to search by an ID example 125 and retrieve just this section
perfomanceData":[
{
"platform":{
"name":"Test2",
"id":0
},
]
How can I use JavaScript to do the above?

Related

Getting the object from an array on the basis of id [duplicate]

This question already has answers here:
How to find an appropriate object in array by one of its properties
(3 answers)
Closed 29 days ago.
I have a array as follows :
data = [
{
"data": {
"id":1,
"vol":"0.0"
"requiredId":100
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0",
"requiredId":2
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I just want to get the object from this array where id is equal to requiredId. How can I do that?
data.find(item => item.data.id === item.data.requiredId)

Sort an array by names with sort in TypeScript [duplicate]

This question already has answers here:
Sorting an array in Javascript based on predefined order
(3 answers)
Sort an array of object by a property (with custom order, not alphabetically)
(7 answers)
Sorting on a custom order
(4 answers)
Closed 5 months ago.
var arr = [{
name: 'low'
},
{
name: 'middle'
},
{
name: 'urgent'
},
{
name: 'high'
},
{
name: 'none'
}
]
How do i sort this array based on the name and the order should be 1.urgent, 2.high, 3.medium, 4.low, 5.none

How do I loop through and read values from a nested array? [duplicate]

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?

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

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

Categories