Reputation: 13854
I have this JSON
{
"doctors": [
{
"id": 8,
"schedules": [
{
"id": 8,
"totime": "11:17",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "10:17",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "d1",
"degree": "DA(Anaesthesia)",
"email": "[email protected]",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "d1",
"userid": 51,
"gender": "Male",
"mobile": "1234567900"
},
{
"id": 10,
"schedules": [
{
"id": 10,
"totime": "12:35",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "11:35",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "d3",
"degree": "BDS",
"email": "[email protected]",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "d3",
"userid": 56,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 1,
"schedules": [
{
"id": 1,
"totime": "12:55",
"dayId": 1,
"location": "Somajiguda",
"fromtime": "11:55",
"hospitalId": 5,
"day": "Monday",
"hospital": "Yashoda"
}
],
"username": "doctor",
"degree": "BDS",
"email": "",
"imagePath": null,
"department": "Critical Care",
"name": "doctor",
"userid": 4,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 7,
"schedules": [
{
"id": 7,
"totime": "11:17",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "11:17",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "donald",
"degree": "DA(Anaesthesia)",
"email": "[email protected]",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "donald",
"userid": 47,
"gender": "Male",
"mobile": "1234567989"
},
{
"id": 6,
"schedules": [
{
"id": 6,
"totime": "11:15",
"dayId": 1,
"location": "Somajiguda",
"fromtime": "11:15",
"hospitalId": 5,
"day": "Monday",
"hospital": "Yashoda"
}
],
"username": "john",
"degree": "BDS",
"email": "[email protected]",
"imagePath": null,
"department": "Anesthesiology",
"name": "john",
"userid": 46,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 5,
"schedules": [
{
"id": 5,
"totime": "13:11",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "12:11",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "sknayak",
"degree": "BDS",
"email": "[email protected]",
"imagePath": "",
"department": "Anesthesiology",
"name": "sknayak",
"userid": 38,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 2,
"schedules": [
{
"id": 2,
"totime": "16:26",
"dayId": 6,
"location": "Somajiguda",
"fromtime": "15:26",
"hospitalId": 5,
"day": "Saturday",
"hospital": "Yashoda"
}
],
"username": "drsukant",
"degree": "BDS",
"email": "",
"imagePath": null,
"department": "Anesthesiology",
"name": "sukant",
"userid": 9,
"gender": "Male",
"mobile": "1234567890"
}
]
}
In this JSON there is a field id which is unique.I am getting this json via an ajax like this
var test=$.ajax({
type: "GET",
url: projectUrl+"getDoctors",
dataType:"json",
jsonp: true,
async:false
}).responseText;
console.log(test);
As you can see in the JSON there is a field id.For example for id=8 username is d1,id=10,username is d3.I am storing the id in session.So for example if id is 8 then I want only those details(username d1,email [email protected].....) whose id is 8.
So how to filter the JSON to a specific value.
Upvotes: 1
Views: 302
Reputation: 15053
You can create a computed for a specific item:
self.doctors = ko.observableArray();
self.d3Doctor = ko.computed(function() {
return ko.utils.arrayFirst(self.doctors(), function(obj) {
return obj.id === 8;
});
});
Now you only have to worry about populating the doctors
observableArray:
$.getJSON(projectUrl+"getDoctors", function(response) {
ko.utils.arrayPushAll(yourViewModel.doctors, response.doctors);
});
This allows for the following:
<div data-bind="if: d3Doctor()">
<h3>Doctor with ID 8</h3>
<p>Name: <span data-bind="text: d3Doctor().name"></span></p>
<p>E-mail: <span data-bind="text: d3Doctor().email"></span></p>
<p>Address: <span data-bind="text: d3Doctor().address"></span></p>
...
</div>
Upvotes: 2
Reputation: 3837
Yes. I can answer this.
In my way, I personally prefer to turn all the data into array and itenerate it or manipulate it.
As mention in this question, we already push the data into this format:
doctors.push({id:currPat.id,name:currPat.username});
So, now I can use the array filter
function for doctors
array:
var result = doctors.filter(function(currentObject) {
return currentObject.id === 8;
});
console.log(result); // {id: 8, name:d1}
Or you can also use the .map()
in JQuery, and just make the function to check the data.id
and return the pair you want.
And if you would fight for a nano
seconds performance. I would guess my method is better than .map()
Upvotes: 1
Reputation: 18018
May be this?:
function findById(data, id){
for(var i=0;i<data.length;i++){
if(data[i].id === id) return { username:data[i].username, email:data[i].email};
}
// Or, you can use $.grep like this:
// var foundDoctors = $.grep(data,function(e){return e.id === id;})
// return foundDoctors.length > 0 && foundDoctors[0] || null;
// Or, you can use filter() method from Array (IE 9+)
}
findById(jsondata["doctors"], 8);
Upvotes: 1
Reputation: 6612
You can use each
to find it, try this:
$(document).ready(function() {
$.ajax({
type: "GET",
url: projectUrl+"getDoctors",
dataType: "json",
jsonp: true,
async: false
}).done(function(data) {
$.each(data.doctors, function(i, v) {
if (v.id == '8') {
console.log('found', v.username, v.email);
return false;
}
});
});
});
Upvotes: 1