Access last JSON element with javascript [duplicate] - javascript

This question already has answers here:
Get the last item in an array
(59 answers)
Closed 8 years ago.
Good evening,
I'm facing a simple problem in JSON. I'm developping a smartwatch (Pebble) app which gets data from an API, which returns the following :
{
"name": "toto",
"count": 55,
"results": {
"collection1": [
{
"article": "Content number 1"
},
{
"article": "Content number 2"
},
{
"article": "Content number 3"
}
]
}
}
The problem is the following : is it possible to access only the latest "article" content from this JSON ? So in this case, it would be "Content number 3".
Here is what I've got so far, but it returns only the 1st element :
ajax({ url: 'MY_API_URL', type:'json' }, function(data){
simply.vibe(); // Vibrate the watch
var message = data.results.collection1[0].article;
simply.body(message); // display the message on the watch
Thanks a lot !

That would be:
data.results.collection1[data.results.collection1.length - 1].article

data.results.collection1[data.results.collection1.length-1].article;

Related

Pure JavaScript loop through JSON [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 5 months ago.
I have the following JSON structure
{
"posts":{
"title 1":{
"content":"The content",
"url":"https://www.site.nl"
},
"title 2":{
"content":"The content",
"url":"https://www.site.nl"
}
}
}
I want loop through this JSON and get all data but I don't know how to get further
What I have so far is
Object.entries(JSON.parse(this.response))
//output: posts,[object Object]
Who can help me with this JavaScript loop to get the title, content and url?
not so hard to do, especially if you read the doc
const data =
{ "posts":
{ "title 1": { "content": "The content", "url": "https://www.site.nl" }
, "title 2": { "content": "The content", "url": "https://www.site.nl" }
}
}
for (let title in data.posts )
{
console.log( title, data.posts[title].content, data.posts[title].url )
}

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

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

process json array object [duplicate]

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

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