I have this JSON:
{
{
"status": {
"success": true,
"error": ""
},
"data": {
"hello_world": [
{
"uid": {
"email": "mydx#d.com",
"id": "d3c074d8b2b24e958"
},
"my_itm": {
"name": "BIg",
"id": "2380caa99cb426f"
},
"id": "6747e005f23f451"
},
{
"uid": {
"email": "mydx#d.com",
"id": "5859082d3c074d8b2b24e958"
},
"my_itm": {
"name": "Dog",
"id": "a99cb426f"
},
"id": "c6747e005f23f452"
}
]
}
}
I'm using handlebars and I want to access from my_itm the name. But I want to do that foreach my_itm. How can I do that?
Here is what I've tried:
{{#each hello_world.my_itm}}
<p>{{name}}</p>
{{/each}}
UPDATED:
Assuming you passed the data to the template under the 'params' name:
res.render('test', {params: <your data>})
You can do this:
{{#each params.data.hello_world}}
{{#if my_itm}}
{{my_itm.name}} <br>
{{/if}}
{{/each}}
Gives:
BIg
Dog
What you have didn't work because hello_world is an array and not an object with a my_itm property you can access directly, you need to loop through the array first.
PS: You also seem to have an extra { in the json.
Related
My data looks like this
data = [
{
"id": "27eb95f9",
"title": "hello",
"sort": 4
},
{
"id": "367da510",
"title": "bro",
"sup-title": "",
"sort": 2
},
{
"id": "27eb95f9",
"title": "how ",
"sort": 3
},
{
"id": "367da510",
"title": "you doing",
"sup-title": "",
"sort": 1
},
Now I want to use map funtion in React to show 'title' but I want the order to be according to "sort" in the object file. How can I do that ?
First use sort function, then map it! Please see below.
In the second exapmle you can use it to render your componenet as well.
Here can you read about sort method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
const data = [
{
"id": "27eb95f9",
"title": "hello",
"sort": 4
},
{
"id": "367da510",
"title": "bro",
"sup-title": "",
"sort": 2
},
{
"id": "27eb95f9",
"title": "how ",
"sort": 3
},
{
"id": "367da510",
"title": "you doing",
"sup-title": "",
"sort": 1
}];
const sortedTitles = data.sort((d1, d2)=>d1-d2).map((d)=>d.title);
console.table(sortedTitles);
render = data.sort((d1, d2)=>d1-d2).map((d)=>`<p>${d.title}</p>`).join("");
console.log(render);
document.getElementById("app").innerHTML = render
<div id="app"></div>
it may works, first JSON.parse(data) to have it as a object then
data.map(row => return {
...row, order : row.sort
})
I'm assigned a task to get data using underscore js.
This is my JSON :
$scope.myData= {
"buslist":
{
"code":"1",
"message":"Success",
"fromStationCode":"71",
"searchResult": [
{
"arrivalTime": "17:00:00",
"availableSeats": "42",
"boardingPointDetails": [
{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}
]
},
{
"arrivalTime": "18:00:00",
"availableSeats": "32",
"boardingPointDetails": [
{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}
]
}
]
}
}
From this I want only "name" field. By doing this :
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
I got all "boardingPointDetails". The result looks like:
[ [
{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}
],[
{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
},
{
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}
],[
{
...
}
]
...
]
Help me to retrieve only "name" from this.
If you just want array of names like ['Koyambedu', 'Madurai'] then below code would work.
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
// Flatten the data.
$scope.flatbdata = _.flatten($scope.bdata, true);
$scope.flatbdata = $scope.flatbdata.filter(function(d){
return d != undefined && d.hasOwnProperty('name')
})
// map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results
$scope.names = $scope.flatbdata.map(function(d){
return d.name;
});
Refer below links :
http://underscorejs.org/#flatten
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
If you just want the array of names, here it is:
$scope.bdata = _.pluck($scope.myTest.buslist.searchResult, 'boardingPointDetails');
var res= _.flatten($scope.bdata);
res=_.pluck(res,'name');
console.log(res);
var temp = _.pluck(_.flatten($scope.bdata),'name')
temp will have ["Koyambedu", "Nerkundram", "Adyar", "Madurai"]
Try this.
$scope = {}; // in real project you don't need this. it's just for snippet.
$scope.myData = {
"buslist": {
"code": "1",
"message": "Success",
"fromStationCode": "71",
"searchResult": [{
"arrivalTime": "17:00:00",
"availableSeats": "42",
"boardingPointDetails": [{
"code": "1631",
"name": "Koyambedu",
"time": "09:30:00"
}, {
"code": "961296",
"name": "Nerkundram",
"time": "09:45:00"
}]
}, {
"arrivalTime": "18:00:00",
"availableSeats": "32",
"boardingPointDetails": [{
"code": "2084",
"name": "Adyar",
"time": "09:30:00"
}, {
"code": "961296",
"name": "Madurai",
"time": "09:45:00"
}]
}]
}
};
$scope.names = _.chain($scope.myData.buslist.searchResult).pluck("boardingPointDetails").flatten(true).map(function(item) {
return item.name;
}).value();
console.log($scope.names);
<script src="http://underscorejs.ru/underscore-min.js"></script>
I have a mongo collection called settings
{
"_id": "123",
"type": "subject",
"name": "Main",
"list": [{
"_id": "123",
"name": "Maths"
}, {
"_id": "123",
"name": " Physics"
}]
}
{
"_id": "123",
"type": "exam",
"name": "Activities",
"list": [{
"_id": "123",
"name": "Reading"
}, {
"_id": "123",
"name": "Fluency"
}]
}
Note: the values provided in my mongo is only for reference purpose.
Now I have generated an array with the records in settings collection
self.sample = function() {
self.settingsObj = {}
// console.log(self.settings)
_.each(self.settings, function(settings) {
//_.each(self.settings, function(settings) {
if (!self.settingsObj[settings.type]) {
self.settingsObj[settings.type] = []
}
self.settingsObj[settings.type] = settings.list
})
console.log(self.settingsObj)
}
When I console the settingsObj I get result like this in my console
Object {subject: Array[2], exam: Array[2]}
Now I want to loop this inside a scope variable
$scope.search = [{
"name": "",
"data": ""
}]
inside this name i want to get the object name in this situation subject,exam and inside loop i want to loop the arrays for subject and exam.
I tried another ._each, and I got the key and value but how can I loop them correctly.
Otherwise please help me in generating subject and exams as different array
I'm not very sure if I understand your question.
But if I see well you're using a Lodash.js library so I provide below script using a lodash:
I assume that you have settings variable like this:
var settings = [ {
"_id": "123",
"type": "subject",
"name": "Main",
"list": [{
"_id": "123",
"name": "Maths"
}, {
"_id": "123",
"name": " Physics"
}]
}
,
{
"_id": "123",
"type": "exam",
"name": "Activities",
"list": [{
"_id": "123",
"name": "Reading"
}, {
"_id": "123",
"name": "Fluency"
}]
}];
And I create functions for process those settings:
function createSearch(settings){
return _(settings).flatMap(toSearchArray).value();
}
function toSearchArray(setting){
return _(setting.list).map("name").map(toSearchObject).value()
function toSearchObject(data){
return {
name: setting.type,
data: data
}
}
}
And when you call createSearch(settings)
you will receive this result:
[
{
"name": "subject",
"data": "Maths"
},
{
"name": "subject",
"data": " Physics"
},
{
"name": "exam",
"data": "Reading"
},
{
"name": "exam",
"data": "Fluency"
}
]
Hopefully this is what you need.
I am building an app using ionic/AngularJS. To test the web service calls I am using the best buy service. The response is as follows:
{
"from": 1,
"to": 10,
"total": 4665,
"currentPage": 1,
"totalPages": 467,
"queryTime": "0.009",
"totalTime": "0.045",
"partial": false,
"canonicalUrl": "/v1/categories?format=json&apiKey=****",
"categories": [
{
"id": "abcat0010000",
"name": "Gift Ideas",
"active": true,
"path": [
{
"id": "cat00000",
"name": "Best Buy"
},
{
"id": "abcat0010000",
"name": "Gift Ideas"
}
],
"subCategories": [
{
"id": "pcmcat140000050035",
"name": "Capturing Photos & Videos"
},
{
"id": "pcmcat140000050036",
"name": "Listening to Digital Music"
},
{
"id": "pcmcat140000050037",
"name": "Computing Made Easy"
},
{
"id": "pcmcat140000050039",
"name": "Simple GPS Navigation"
},
{
"id": "pcmcat140000050040",
"name": "Playing Video Games"
},
{
"id": "pcmcat140000050041",
"name": "Watching HDTV"
},
.........
How can I retrieve and display this information as a list on my device? I am aware of the $http service request, but how do I incorporate this into an ionic list?
Assuming that you already have written the logic to fetch data I have written this JSBin for implementing ion-list.
Let's Assume you are going to display the name field in your list.
app.js
var baseURL="*here your json request*";
var categoriesName=[];
$http.get('baseURL')
.success(function(response){
$scope.objectResponse = response.categories;
console.log($scope.objectResponse);
$scope.objectResponse.forEach(function(item) {
categoriesName.push(item.name);
console.log(categoriesName);
})
.error(function(response){
console.log(response);
});
html
<ul class="list">
<li class="item" ng-repeat="listname in categoriesName">
{{listname}}
</li>
</ul>
This is How your can retrieve and display this information as a list on device.
Im trying to fetch this Model in a nice way but dont really get it to work as i want.
The model gets fetched every second by a setInterval();. I do that because there is this backend system that are able to change the json that the model holds. The problem is that i dont know how to get a hold of ALL the json.
Right now i can only access "0"(question and id) and set that on the Model. I want to be able to access all the JSON and set on the Model. How would i parse that?
My json looks like this:
{
"0": [
{
"question": "lorem ispum",
"id": "1"
}
],
"1": [
{
"id": "1",
"alternative": "lorem ispum",
"percent": "14.0000",
"total": "7"
},
{
"id": "2",
"alternative": "lorem ispum",
"percent": "14.0000",
"total": "7"
},
{
"id": "3",
"alternative": "lorem ispum",
"percent": "60.0000",
"total": "30"
},
{
"id": "4",
"alternative": "lorem ispum",
"percent": "12.0000",
"total": "6"
}
]
}
And this is my Model
QuestionModel = Backbone.Model.extend({
id: "",
url: function() {
return "api/?action=getQuestionData&question_id="+this.id;
},
parse: function (response) {
return response[0][0];
}
});
Backbone does not support nested data out of the box. I would do the following:
QuestionModel = Backbone.Model.extend({
id: "",
url: function() {
return "api/?action=getQuestionData&question_id="+this.id;
},
parse: function (response) {
return { question: response["0"][0], answers: response["1"] };
}
});
Then you can access your properties like questionModel.get('question').id and your answers like questionModel.get('answers')[0].percent