I'm new in Angularjs,i succeed to display every value from the key "type" from my json array ,but i want only to display "type" if the value is equal to "service" how can i do that.
I tried to do with custom filter but i don t understand how it works.
Thanks for advance for your help !
Here is my controller :
.controller('CategorieCtrl', function ($scope, $stateParams,$state,factocategories) {
var mytype = {};
$scope.displaysales = function(){
mytype = "sales";
console.log(mytype);
}
$scope.displaybuys = function(){
mytype = "buys";
console.log(mytype);
}
$scope.displaycharges = function(){
mytype = "charges";
console.log(mytype);
}
$scope.displayperso = function(){
mytype = "personals";
console.log(mytype);
}
console.log(mytype);
var mytoken = sessionStorage.getItem('token');
factocategories.send(mytoken).then(function(conf){
console.log(conf);
$scope.datab = conf.data;
})
})
here is my view :
<ion-view view-title="Catégories">
<ion-content>
<div class="item item-divider submenu">
<p class="submenu col-25" ng-click="displaysales()">Ventes</p><p class="submenu col-25" ng-click="displaybuys()">achats</p><p class="submenu col-25" ng-click="displaycharges()">Charges</p><p class="submenu col-25" ng-click="displayperso()">Perso</p>
</div>
<ul class="list" ng-repeat="item in datab track by $index">
<li class="item" ng-model="item.type">{{item.type}}</li>
</ul>
</ion-content>
</ion-view>
and here is my json :
{
"die": false,
"erreur": "",
"data": [
{
"id": 913,
"name": "Justine / Ma first facture",
"type": "services",
"invoice": true
},
{
"id": 914,
"name": "Justine / Facture 2",
"type": "services",
"invoice": true
},
{
"id": 917,
"name": "Abus / Fact",
"type": "services",
"invoice": true
},
{
"id": 930,
"name": "Abus / F - 00004",
"type": "",
"invoice": true
},
{
"id": 931,
"name": "Test client / F - 00005",
"type": "",
"invoice": true
},
{
"id": 8868,
"name": "Achat de marchandises",
"type": "buys",
"invoice": false
},
{
"id": 10223,
"name": "adidas",
"type": "sales",
"invoice": false
},
{
"id": 8870,
"name": "Apport personnel",
"type": "personals",
"invoice": false
},
{
"id": 8867,
"name": "Carburant, entretien du véhicule",
"type": "charges",
"invoice": false
},
{
"id": 8865,
"name": "Cotisation sociale",
"type": "charges",
"invoice": false
},
{
"id": 8863,
"name": "Déplacement",
"type": "charges",
"invoice": false
},
{
"id": 8872,
"name": "Emprunt",
"type": "charges",
"invoice": false
},
{
"id": 8861,
"name": "Energie",
"type": "charges",
"invoice": false
},
{
"id": 8871,
"name": "Fourniture de bureau",
"type": "charges",
"invoice": false
},
{
"id": 8864,
"name": "Frais banque, assurance, juridique",
"type": "charges",
"invoice": false
},
{
"id": 8877,
"name": "Logiciel, service web",
"type": "charges",
"invoice": false
},
{
"id": 8866,
"name": "Loyer",
"type": "charges",
"invoice": false
},
{
"id": 8874,
"name": "Mobilier",
"type": "charges",
"invoice": false
},
{
"id": 8869,
"name": "Mon salaire, prélèvement personnel",
"type": "personals",
"invoice": false
},
{
"id": 10779,
"name": "New law",
"type": "charges",
"invoice": false
},
{
"id": 10222,
"name": "nike",
"type": "services",
"invoice": false
},
{
"id": 10778,
"name": "Pakpak",
"type": "sales",
"invoice": false
},
{
"id": 8862,
"name": "Restauration",
"type": "charges",
"invoice": false
},
{
"id": 8875,
"name": "Site web",
"type": "charges",
"invoice": false
},
{
"id": 8873,
"name": "Sous-traitance",
"type": "charges",
"invoice": false
},
{
"id": 8860,
"name": "Télécom",
"type": "charges",
"invoice": false
},
{
"id": 8876,
"name": "Véhicule",
"type": "charges",
"invoice": false
}
]
}
<ul class="list" ng-repeat="item in datab track by $index">
<li class="item" ng-if="item.type === 'services'">{{item.type}}</li>
</ul>
ng-model is not required! is is meant for inputs and two data binding.
for the condinonallity use ng-if
Or you can use a filter like this :
<ul class="list" ng-repeat="item in datab track by $index | filter:{type:'services'}">
<li class="item">{{item.type}}</li>
</ul>
use ng-if="item.type=='service'"
<ul class="list" ng-repeat="item in datab track by $index" ng-if="item.type=='service'">
<li class="item" ng-model="item.type">{{item.type}}</li>
</ul>
Related
This is my first question on stack overflow. I am attempting to map an array that is housed within an object. My question is more regarding the targeting than the actual mapping process itself (I think). The goal of my code is to map an array to this target:
var target = {
"id": 1, //as an int
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": "all of the batter types as a string",
"ingredients": [],//a copy of all the toppings
"countOfFillings": 0
}
the object in question is:
var bakery = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0004",
"type": "bar",
"name": "Bar",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7001", "name": "None", "addcost": 0 },
{ "id": "7002", "name": "Custard", "addcost": 0.25 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0.25 }
]
}
},
{
"id": "0005",
"type": "twist",
"name": "Twist",
"ppu": 0.65,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
]
},
{
"id": "0006",
"type": "filled",
"name": "Filled",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7002", "name": "Custard", "addcost": 0 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0 },
{ "id": "7004", "name": "Strawberry Jelly", "addcost": 0 },
{ "id": "7005", "name": "Rasberry Jelly", "addcost": 0 }
]
}
}
]
}
}
And this is my attempt at mapping. I'm able to get the array generated but only from the first item in the array as indicated with the [0]. Any feedback or advice would be greatly appreciated!
var exampleTest = bakery.items.item.map(mapCake);
function mapCake (aCake)
{
let newType = `${bakery.items.item[1].type}`
let result = {id: `${bakery.items.item[0].id}`,
type: newType,
name: `${bakery.items.item[0].name}`,
ppu: `${bakery.items.item[0].ppu}`,
batters: [`${bakery.items.item[0].batters.batter}`],
ingredients: `${bakery.items.item[0].topping}`,
countOfFillings: `${bakery.items.item[0].fillings}`.length};
return result
}
Try this:
function mapCake(aCake) {
let newType = `${aCake.type}`;
let result = {
id: `${aCake.id}`,
type: newType,
name: `${aCake.name}`,
ppu: `${aCake.ppu}`,
batters: [`${aCake.batters.batter}`],
ingredients: `${aCake.topping}`,
countOfFillings: `${aCake.fillings}`.length
};
return result;
}
Looks like in your function you're directly indexing a specific bakery item each time.
Your aCake parameter wasn't yet being used, at each iteration, aCake refers to the current item that is being iterated over - so that's what you want to be populating your result values with.
how can I loop through this JSON in JavaScript and check whether a user's id exists? This data is from an image annotation collection and I am going to need to prevent a user from editing someone else's annotations.
In this example, lets just say that user id I am looking for is K2Lb1R7owqR9BYmpJAJzrg6w1s92 from the creator: key.
I tried using for example arr.includes() but didn't work for me.
What is my expected output? I think for now I need to just worry whether a user's id exists or not and if so, return false or true.
The creator object will always be present inside the body.
Thank you for your help.
[{
"body": [{
"created": "2020-07-03T11:49:32.058Z",
"purpose": "commenting",
"value": "don't move me",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
},
"type": "TextualBody"
}, {
"purpose": "tagging",
"value": "move",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"type": "TextualBody",
"created": "2020-07-03T11:49:30.850Z"
}],
"id": "IRCJPsz4NKkiuhVLE1hR",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"#context": "http://www.w3.org/ns/anno.jsonld",
"type": "Annotation",
"target": {
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0",
"selector": {
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector",
"value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"
}
}
}, {
"body": [{
"purpose": "commenting",
"type": "TextualBody",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"value": "anyone ever climb this mountain top?",
"created": "2020-07-03T10:57:40.590Z"
}, {
"creator": {
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92",
"name": "doss"
},
"purpose": "tagging",
"value": "ridge",
"type": "TextualBody",
"created": "2020-07-03T10:57:39.351Z"
}, {
"value": "i did in 2005",
"created": "2020-07-03T10:59:45.318Z",
"type": "TextualBody",
"creator": {
"name": "testme",
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
},
"purpose": "commenting"
}, {
"type": "TextualBody",
"created": "2020-07-03T10:59:43.966Z",
"purpose": "tagging",
"value": "testme",
"creator": {
"id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
"name": "testme"
}
}, {
"type": "TextualBody",
"purpose": "replying",
"value": "test",
"creator": {
"name": "doss",
"id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
},
"created": "2020-07-03T11:39:18.860Z"
}],
"type": "Annotation",
"#context": "http://www.w3.org/ns/anno.jsonld",
"photoDocId": "92wNwz2aaqy7CWf3mGo1",
"target": {
"selector": {
"value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156",
"conformsTo": "http://www.w3.org/TR/media-frags/",
"type": "FragmentSelector"
},
"source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
},
"id": "IlI7SRjFm8qohmHDLBw2"
}]
You can make use of flatMap to get flat data in array then using some to find out whether the value is present or not:
var array=[{ "body": [{ "created": "2020-07-03T11:49:32.058Z", "purpose": "commenting", "value": "don't move me", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" }, "type": "TextualBody" }, { "purpose": "tagging", "value": "move", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "type": "TextualBody", "created": "2020-07-03T11:49:30.850Z" }], "id": "IRCJPsz4NKkiuhVLE1hR", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "#context": "http://www.w3.org/ns/anno.jsonld", "type": "Annotation", "target": { "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0", "selector": { "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector", "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344" } }}, { "body": [{ "purpose": "commenting", "type": "TextualBody", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "value": "anyone ever climb this mountain top?", "created": "2020-07-03T10:57:40.590Z" }, { "creator": { "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92", "name": "doss" }, "purpose": "tagging", "value": "ridge", "type": "TextualBody", "created": "2020-07-03T10:57:39.351Z" }, { "value": "i did in 2005", "created": "2020-07-03T10:59:45.318Z", "type": "TextualBody", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "purpose": "commenting" }, { "type": "TextualBody", "created": "2020-07-03T10:59:43.966Z", "purpose": "tagging", "value": "testme", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" } }, { "type": "TextualBody", "purpose": "replying", "value": "test", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "created": "2020-07-03T11:39:18.860Z" }], "type": "Annotation", "#context": "http://www.w3.org/ns/anno.jsonld", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "target": { "selector": { "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156", "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector" }, "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0" }, "id": "IlI7SRjFm8qohmHDLBw2"}];
var result = array.flatMap(elem=>elem.body).some(user=>user.creator.id==='K2Lb1R7owqR9BYmpJAJzrg6w1s92');
console.log(result);
some will return either true or false, which is what you need here.
I hope this helps.
You can make use of the some function:
const data = [{"body":[{"created":"2020-07-03T11:49:32.058Z","purpose":"commenting","value":"don't move me","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"},"type":"TextualBody"},{"purpose":"tagging","value":"move","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"type":"TextualBody","created":"2020-07-03T11:49:30.850Z"}],"id":"IRCJPsz4NKkiuhVLE1hR","photoDocId":"92wNwz2aaqy7CWf3mGo1","#context":"http://www.w3.org/ns/anno.jsonld","type":"Annotation","target":{"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0","selector":{"conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector","value":"xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"}}},{"body":[{"purpose":"commenting","type":"TextualBody","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"value":"anyone ever climb this mountain top?","created":"2020-07-03T10:57:40.590Z"},{"creator":{"id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92","name":"doss"},"purpose":"tagging","value":"ridge","type":"TextualBody","created":"2020-07-03T10:57:39.351Z"},{"value":"i did in 2005","created":"2020-07-03T10:59:45.318Z","type":"TextualBody","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"purpose":"commenting"},{"type":"TextualBody","created":"2020-07-03T10:59:43.966Z","purpose":"tagging","value":"testme","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"}},{"type":"TextualBody","purpose":"replying","value":"test","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"created":"2020-07-03T11:39:18.860Z"}],"type":"Annotation","#context":"http://www.w3.org/ns/anno.jsonld","photoDocId":"92wNwz2aaqy7CWf3mGo1","target":{"selector":{"value":"xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156","conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector"},"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"},"id":"IlI7SRjFm8qohmHDLBw2"}]
console.log(data.some(({body}) => body.some(({creator}) => creator.id === 'K2Lb1R7owqR9BYmpJAJzrg6w1s92')));
I'm working on a project where I need to get all the vacant classrooms for the day and basically filter out the ones being in use.
I get all the data from the school's own API with JSON response body that looks like this:
{
"status": "success",
"reservations": [
{
"id": "18935",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:51",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "50",
"type": "room",
"code": "A440.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A440.5"
},
{
"id": "2995",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20362",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:00:00",
"endDate": "2017-04-27T14:00:00",
"resources": [
{
"id": "51",
"type": "room",
"code": "A450.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.1"
},
{
"id": "2402",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3064",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20237",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T11:15:00",
"endDate": "2017-04-27T13:00:00",
"resources": [
{
"id": "45",
"type": "room",
"code": "A420.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A420.4"
},
{
"id": "2433",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3058",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20888",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:57",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "62",
"type": "room",
"code": "A520.5",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.5"
},
{
"id": "3092",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2444",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "22586",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:48",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "52",
"type": "room",
"code": "A450.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A450.3"
},
{
"id": "3004",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2294",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "525",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18816",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:58",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "41",
"type": "room",
"code": "A340.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.1"
},
{
"id": "2989",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "795",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "599",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "20431",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:56",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "40",
"type": "room",
"code": "A320.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A320.7/8"
},
{
"id": "2416",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2386",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18588",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:49",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "25",
"type": "room",
"code": "A130.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.1"
},
{
"id": "26",
"type": "room",
"code": "A130.3",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A130.3"
},
{
"id": "2979",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "582",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "18940",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "2996",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "2267",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2268",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "31",
"type": "room",
"code": "A210.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A210.2"
}
],
"description": ""
},
{
"id": "12041",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:04:53",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "2510",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "23",
"type": "room",
"code": "A520.7",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A520.7"
}
],
"description": ""
},
{
"id": "24630",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:05",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "3277",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "42",
"type": "room",
"code": "A340.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A340.2"
}
],
"description": ""
},
{
"id": "27205",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:07",
"startDate": "2017-04-27T14:15:00",
"endDate": "2017-04-27T17:00:00",
"resources": [
{
"id": "35",
"type": "room",
"code": "A240.2",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.2"
},
{
"id": "775",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "3384",
"type": "realization",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "25917",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:00",
"startDate": "2017-04-27T15:15:00",
"endDate": "2017-04-27T16:00:00",
"resources": [
{
"id": "36",
"type": "room",
"code": "A240.4",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A240.4"
},
{
"id": "593",
"type": "realization",
"code": "",
"name": ""
},
{
"id": "595",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
},
{
"id": "21932",
"subject": "subjectName",
"modifiedDate": "2017-04-27T06:05:06",
"startDate": "2017-04-27T16:00:00",
"endDate": "2017-04-27T18:00:00",
"resources": [
{
"id": "43",
"type": "room",
"code": "A350.1",
"parent": {
"id": "2",
"type": "building",
"code": "A",
"name": "buildingA"
},
"name": "A350.1"
},
{
"id": "2464",
"type": "student_group",
"code": "",
"name": ""
},
{
"id": "2747",
"type": "student_group",
"code": "",
"name": ""
}
],
"description": ""
}
]
}
In order to get the JSON response I need to put startDate (the moment user searches the vacant rooms, in this situation it is: 2017-04-27T10:55) and endDate (set to end of the day, 2017-04-27T22:00). The result for this query is the JSON response above.
The problem is that the API I'm using doesn't contain any data for the vacant rooms but only for the ones booked for certain times so I made a list of all the rooms in the building and compared it to the booked ones to filter them out:
var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1',
'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];
var data = JSON.parse(responseText);
var booking = Object.create(null);
var free;
data.reservations.forEach(function (reservation) {
reservation.resources.some(function (resource) {
if (resource.type === 'room') {
booking[resource.code] = booking[resource.code] || [];
booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
return true;
}
});
});
free = rooms.filter(function (a) {
return !booking[a];
});
console.log(free);
Results:
A210.3
A220.5
A320.2
A520.6
A510.4
This only returns the ones that are not being used at all during the day but I need to get the ones that are vacant for hour or two.
For this booked class for example:
"startDate": "2017-04-27T13:15:00",
"endDate": "2017-04-27T16:00:00",
"type": "room",
"code": "A520.5"
for this I would need to print out:
A520.5 - 2 hours 20 minutes
So I need to get the room / time from the startDate of the search (2017-04-27T10:55) and compare it to the startDate of the booked room (2017-04-27T13:15:00) to get the time remaining for that room to be vacant.
TO CLARIFY:
I have all the data for booked rooms starting from 2017-04-27T10:55
until 2017-04-27T22:00 as you can see from the JSON response above.
I need to somehow compare the startDate of the JSON query when the user
searches for the rooms (2017-04-27T10:55) and compare it to the booked
rooms startDate to get the time how long the room stays vacant.
This is how I'm doing my startDate and endDate for the JSON query:
// Timestamp needs to be formed (YYYY-MM-DDTxx:xx) in order for JSON query to work
var todaysDate = new Date();
function convertDate(date) {
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth() + 1).toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
}
// Current time when user searches for vacant rooms
var currentTime = new Date();
var time = "T" + currentTime.getHours() + ":" + currentTime.getMinutes();
// variables for the JSON query
var startDate = convertDate(todaysDate) + time;
var endDate = convertDate(todaysDate) + ("T22:00");
// JSON-query
var getRooms = {
"startDate": startDate,
"endDate": endDate
};
I have JSON Data like below
{
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}]
}
}
I want to select item object having id 0001.So how can I select or filter only this single item object using javascript or Jquery??
please help me and thanks in advance
Like this:
var item = data.items.item.filter(function(x){
return x.id === '0001';
})[0];
I have this collection: https://graph.facebook.com/2playcz/albums
This collection contains 8 id. How can i get the total count id of this collection using javascript? (Total = 8)
Source:
{
"data": [
{
"id": "201936779932071",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tr\u00e9ninky",
"link": "https://www.facebook.com/album.php?fbid=201936779932071&id=190320081093741&aid=41883",
"cover_photo": "201937046598711",
"count": 8,
"type": "normal",
"created_time": "2012-07-02T09:33:43+0000",
"updated_time": "2012-09-15T12:05:44+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "1788805921",
"name": "Edita Nov\u00e1"
},
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/201936779932071/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "205206429605106",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Turnaje a akce",
"link": "https://www.facebook.com/album.php?fbid=205206429605106&id=190320081093741&aid=42900",
"cover_photo": "205208716271544",
"count": 14,
"type": "normal",
"created_time": "2012-07-10T19:36:53+0000",
"updated_time": "2012-09-15T12:04:05+0000",
"can_upload": false
},
{
"id": "221784994613916",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tenisova \u0161kolka 2play",
"description": "Tenisov\u00e1 \u0161kolka 2play",
"link": "https://www.facebook.com/album.php?fbid=221784994613916&id=190320081093741&aid=49379",
"cover_photo": "221785024613913",
"count": 9,
"type": "normal",
"created_time": "2012-08-31T11:19:59+0000",
"updated_time": "2012-09-14T15:17:53+0000",
"can_upload": false
},
{
"id": "203405996451816",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Webov\u00e9 fotografie - Logo",
"link": "https://www.facebook.com/album.php?fbid=203405996451816&id=190320081093741&aid=42285",
"cover_photo": "203406586451757",
"count": 11,
"type": "normal",
"created_time": "2012-07-05T10:12:40+0000",
"updated_time": "2012-09-14T15:16:40+0000",
"can_upload": false
},
{
"id": "190332361092513",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=190332361092513&id=190320081093741&aid=39232",
"cover_photo": "225939404198475",
"count": 2,
"type": "normal",
"created_time": "2012-06-09T13:52:38+0000",
"updated_time": "2012-09-12T18:15:51+0000",
"can_upload": false
},
{
"id": "190802884378794",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Wall Photos",
"link": "https://www.facebook.com/album.php?fbid=190802884378794&id=190320081093741&aid=39324",
"cover_photo": "190802891045460",
"count": 2,
"type": "wall",
"created_time": "2012-06-10T13:19:48+0000",
"updated_time": "2012-07-17T17:16:19+0000",
"can_upload": false
},
{
"id": "205207126271703",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Ostatn\u00ed",
"link": "https://www.facebook.com/album.php?fbid=205207126271703&id=190320081093741&aid=42902",
"cover_photo": "205209679604781",
"count": 4,
"type": "normal",
"created_time": "2012-07-10T19:40:05+0000",
"updated_time": "2012-07-16T14:47:16+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/205207126271703/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "190320914426991",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=190320914426991&id=190320081093741&aid=39224",
"cover_photo": "190327474426335",
"count": 1,
"type": "profile",
"created_time": "2012-06-09T13:29:16+0000",
"updated_time": "2012-06-09T13:43:08+0000",
"can_upload": false
},
{
"id": "190322704426812",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Logo",
"description": "Loga spole\u010dnosti",
"link": "https://www.facebook.com/album.php?fbid=190322704426812&id=190320081093741&aid=39225",
"type": "normal",
"created_time": "2012-06-09T13:34:09+0000",
"updated_time": "2012-07-05T10:16:58+0000",
"can_upload": false
}
]
}
If you mean the count of items in the data array, it would just be data.length.
First of all, there 9 ids. If you want to count the number of elements in array there's a built-in property length. So that if your object is called x you retrieve the length of data by accessing x.data.length.
On the other hand, if you wanted to count the number of unique ids (IDs should by unique anyway, but if you really really wanted to) you have to manually iterate through the array and count them:
var countIds = function (arr) {
var uniqueIds = {}, num = 0;
if (!arr.data) return false;
arr.data.forEach(function (val, i) {
if (typeof uniqueIds[val.id] === "undefined") {
++num;
uniqueIds[val.id] = true;
}
});
return num;
};