How to remove JSON header? - javascript

i have following JSON:
{
"folder": [
{
"$": {
"id": "471841542",
"name": "Manajemen Pemasaran",
"description": "",
"user_id": "186868958",
"shared": "1",
"shared_link": "https://www.box.com/s/b0nupgg8boe25nyfz781",
"permissions": "kcfgtedopnhsuvy",
"size": "2258167",
"file_count": "5",
"created": "1352203782",
"updated": "1352260971"
},
"tags": [
{}
]
},
{
"$": {
"id": "469509910",
"name": "C++",
"description": "",
"user_id": "186868958",
"shared": "1",
"shared_link": "https://www.box.com/s/byyidt02k4sglbuvxlw3",
"permissions": "kcfgtedopnhsuvy",
"size": "1322331",
"file_count": "128",
"created": "1352101667",
"updated": "1352203670"
},
"tags": [
{}
]
},
{
"$": {
"id": "469476618",
"name": "Default Sync Folder",
"description": "",
"user_id": "186868958",
"shared": "0",
"shared_link": "",
"permissions": "kcfgtedopnhsuvy",
"size": "49509",
"file_count": "1",
"created": "1352098703",
"updated": "1352098713"
},
"tags": [
{}
]
},
{
"$": {
"id": "469469972",
"name": "My Box Files",
"description": "",
"user_id": "186868958",
"shared": "0",
"shared_link": "",
"permissions": "kcfgtedopnhsuvy",
"size": "32426",
"file_count": "1",
"created": "1352097877",
"updated": "1352097977"
},
"tags": [
{}
]
}
]
}
I want it changed become like this:
{
"folder": [{
"id": "471841542",
"name": "Manajemen Pemasaran",
"description": "",
"user_id": "186868958",
"shared": "1",
"shared_link": "https://www.box.com/s/b0nupgg8boe25nyfz781",
"permissions": "kcfgtedopnhsuvy",
"size": "2258167",
"file_count": "5",
"created": "1352203782",
"updated": "1352260971"
"tags": [
{}
]
},
{
"id": "469509910",
"name": "C++",
"description": "",
"user_id": "186868958",
"shared": "1",
"shared_link": "https://www.box.com/s/byyidt02k4sglbuvxlw3",
"permissions": "kcfgtedopnhsuvy",
"size": "1322331",
"file_count": "128",
"created": "1352101667",
"updated": "1352203670",
"tags": [
{}
]
},
{
"id": "469476618",
"name": "Default Sync Folder",
"description": "",
"user_id": "186868958",
"shared": "0",
"shared_link": "",
"permissions": "kcfgtedopnhsuvy",
"size": "49509",
"file_count": "1",
"created": "1352098703",
"updated": "1352098713",
"tags": [
{}
]
},
{
"$": {
"id": "469469972",
"name": "My Box Files",
"description": "",
"user_id": "186868958",
"shared": "0",
"shared_link": "",
"permissions": "kcfgtedopnhsuvy",
"size": "32426",
"file_count": "1",
"created": "1352097877",
"updated": "1352097977"
"tags": [
{}
]
}
]
}
Is there a way remove $ from JSON above?

This should create the result you want:
var json = { ... }; // your JSON data
var newJson = { folder: [] };
for (var i = 0, l = json.folder.length; i < l; ++i) {
var tmp = json.folder[i].$;
tmp.tags = json.folder[i].tags;
newJson.folder.push(tmp);
}
console.log(newJson);

Related

Add an object inside an array of nested object depending of an id existing on the array of the nested object and ignore duplicate

I have two Array of objects, the first one and the parent one is "factory".
The FactoryParent array contains objects and inside that list of object there's a list of products,
The second one is an array of products.
First of all I need to detect the factory with a the field name and parent, after that I need to check my array of products and depending of the field family.name and family.parent add that particular product to the array of products on the FactoryParent array, if the object exist I need to ignore and goes on until I go through all the array of products ArrayOfProducts
There's an unique UID, if it exists on Factory-->Object--->products[] ignore and check the other one,
FactoryParent = [
{
"name": "MT",
"parent": null,
"Normes": [
"NF EN 1111"
],
"version_schema": null
},
{
"name": "MS150",
"parent": "MR",
"Normes": [
"NF EN 7777"
],
"products": [
{
"Id": "78789789784dsz654z",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL X",
"serie": "something"
}
}
]
},
{
"name": "ME150",
"parent": "MM",
"products": [
{
"Id": "45d456d44567a8a798a79a87456",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "Lunatic S",
"serie": "something"
}
},
{
"Id": "60d4b24fea1adc05a6e53e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "MOMS S",
"serie": "somting"
}
},
{
"Id": "46f654df564df5645646z",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "RAL S",
"serie": "somting"
}
}
]
}
]
ArrayOfProducts = [
{
"Id": "60d4b24fea1adc05a6e4974e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
{
"Id": "sd798d7s8ds756sd4789",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
{
"Id": "46f654df564df5645646z",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "RAL S",
"serie": "somting"
}
},
{
"Id": "88ds456sd4897sd89d7s",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL S",
"serie": "somting"
}
}
]
My final array should look like this:
finalArray = [
{
"name": "MT",
"parent": null,
"Normes": [
"NF EN 1111"
],
"version_schema": null
},
{
"name": "MS150",
"parent": "MR",
"Normes": [
"NF EN 7777"
],
"products": [
{
"Id": "78789789784dsz654z",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL X",
"serie": "something"
}
},
{
"Id": "88ds456sd4897sd89d7s",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL S",
"serie": "somting"
}
}
]
},
{
"name": "ME150",
"parent": "MM",
"products": [
{
"Id": "45d456d44567a8a798a79a87456",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "Lunatic S",
"serie": "something"
}
},
{
"Id": "60d4b24fea1adc05a6e53e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "MOMS S",
"serie": "somting"
}
},
{
"Id": "46f654df564df5645646z",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "RAL S",
"serie": "somting"
}
},
{
"Id": "60d4b24fea1adc05a6e4974e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
{
"Id": "sd798d7s8ds756sd4789",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
]
}
]
Edit : What I have done :
origArr = will contain the list of all products.
updatingArr = will be the factory products.
removeDuplicate(origArr: any, updatingArr: any) {
let ids = new Set(origArr.map((d: { ID: any; }) => d.ID));
let merged = [...origArr, ...updatingArr.filter((d: { ID: unknown; }) => !ids.has(d.ID))];
}
ArrayOfProducts.forEach(function (arrayItem) {
let objectOfFactory = FactoryParent.find((appli: any) => appli.nom === arrayItem.famille.nom);
console.log(objectOfFactory); // will return the object of the factory array that have the same parent name/name on the product name.
At this point I need to add my object inside that array if it's not duplicated
});
Check if this works for you...!
var FactoryParent = [
{
"name": "MT",
"parent": null,
"Normes": [
"NF EN 1111"
],
"version_schema": null
},
{
"name": "MS150",
"parent": "MR",
"Normes": [
"NF EN 7777"
],
"products": [
{
"Id": "78789789784dsz654z",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL X",
"serie": "something"
}
}
]
},{
"name": "ME150",
"parent": "MM",
"products": [
{
"Id": "45d456d44567a8a798a79a87456",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "Lunatic S",
"serie": "something"
}
},
{
"Id": "60d4b24fea1adc05a6e53e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "MOMS S",
"serie": "somting"
}
},
{
"Id": "46f654df564df5645646z",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "RAL S",
"serie": "somting"
}
}
]
}
]
var ArrayOfProducts = [
{
"Id": "60d4b24fea1adc05a6e4974e",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
{
"Id": "sd798d7s8ds756sd4789",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "DYNAMIC S",
"serie": null
}
},
{
"Id": "46f654df564df5645646z",
"family": {
"name": "ME150",
"parent": "MM",
"gamme": "RAL S",
"serie": "somting"
}
},
{
"Id": "88ds456sd4897sd89d7s",
"family": {
"name": "MS150",
"parent": "MR",
"gamme": "RAL S",
"serie": "somting"
}
}
]
function formatData(){
return FactoryParent.map(factory => {
var products = ArrayOfProducts.filter(product => factory.name === product.family.name && factory.parent === product.family.parent)
if(factory?.products){
products.forEach(product => {
var isProductExits = factory.products.some(factoryProduct => factoryProduct.Id === product.Id);
if(!isProductExits){
factory.products.push(product);
}
})
}
return factory
})
}
console.log(formatData())

JavaScript time comparison with arrays

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

Access and change 2d Json Array, Javascript

I'm trying to access and change the data I receive from a JSON api call.
The format of the call is like this:
{
"count": 391,
"value": [
{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
},
{
"id": "id2",
"name": "name2",
"url": "url2",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
},...
and I want to add an extra entry to each "value", such that I have:
{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
"date": "date" <---------
}
I've tried:
$.each(data, function (idx) {
data.value[idx].currentDate = new Date().toJSON().slice(0, 16);
})
and:
$.each(data, function (idx) {
data[1][idx].currentDate = new Date().toJSON().slice(0, 16);
})
any Ideas on how I could remedy this problem?
Much thanks.
You can do with your existing code but just need slightly tweaking,
var data = {
"count": 391,
"value": [{
"id": "id1",
"name": "name1",
"url": "url1",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
}, {
"id": "id2",
"name": "name2",
"url": "url2",
"project": {
"id": "projId",
"name": "BT-GIT",
"url": "otherurl",
"state": "wellFormed"
},
"defaultBranch": "master",
"remoteUrl": "remote"
}]
};
$.each(data.value, function (index,data) {
data.currentDate = new Date().toJSON().slice(0, 16);
});
console.log(data);
See Demo : https://jsfiddle.net/mj3vu6s6/4/
data.value = data.value.map(function(v) { v.date = Date.now() })

How can I group data with an Angular filter by timestamp?

My MVC return this JSON
[
{
"$id":"1",
"Tags":
[
{"$id":"2","Values":
[
{"$id":"3","Tag":{"$ref":"2"},"ID":4,"TagID":1,"Value1":5.00,"TimeStamp":"2015-10-26T15:23:50","Quality":1,"Tags_ID":1},
{"$id":"4","Tag":{"$ref":"2"},"ID":7,"TagID":1,"Value1":4.00,"TimeStamp":"2015-10-26T15:25:50","Quality":2,"Tags_ID":1}
],"Reports":[{"$ref":"1"}],"ID":1,"Name":"0101WT370/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"5","Values":[],"Reports":[{"$ref":"1"}],"ID":2,"Name":"0101WT371/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"6","Values":[],"Reports":[{"$ref":"1"}],"ID":3,"Name":"0101WT395/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null},
{"$id":"7","Values":[],"Reports":[{"$ref":"1"}],"ID":4,"Name":"0101WT396/WT.VALUE","Type":null,"Archive":null,"Server":null,"Created":null,"Edited":null}
],
"ID":3,"Name":"A"
},
{
"$id":"8",
"Tags":[],"ID":4,"Name":"B"
}
]
And i want winth Angular to group values by Timestamp part - hourly, daily or monthly
I want this result example for daily:
Tag | Timestamp | Value |
Tag1| 26-10-2015 | 4.5 = (9/2) = value1+value2 / count
Can you give me any suggestion?
EDIT:
I found this where give me something like way to start from :)
Try the code below :
JAVASCRIPT
function sortTimeStamp(obj) {
for(i=0; i<obj.length;i++) {
for(z=0; z<obj[i].Tags.length;z++) {
for(y=0; y<obj[i].Tags[z].Values.length;y++) {
obj[i].Tags[z].Values.sort(function (a, b) {
return new Date(b.TimeStamp).getTime() - new Date(a.TimeStamp).getTime();
});
}
}
}
return obj;
}
var myObj = [
{
"$id": "1",
"Tags": [
{
"$id": "2",
"Values": [
{
"$id": "3",
"Tag": {
"$ref": "2"
},
"ID": 4,
"TagID": 1,
"Value1": 5,
"TimeStamp": "2015-10-26T15:23:50",
"Quality": 1,
"Tags_ID": 1
},
{
"$id": "4",
"Tag": {
"$ref": "2"
},
"ID": 7,
"TagID": 1,
"Value1": 4,
"TimeStamp": "2015-10-26T15:25:50",
"Quality": 2,
"Tags_ID": 1
}
],
"Reports": [
{
"$ref": "1"
}
],
"ID": 1,
"Name": "0101WT370/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "5",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 2,
"Name": "0101WT371/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "6",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 3,
"Name": "0101WT395/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
},
{
"$id": "7",
"Values": [],
"Reports": [
{
"$ref": "1"
}
],
"ID": 4,
"Name": "0101WT396/WT.VALUE",
"Type": null,
"Archive": null,
"Server": null,
"Created": null,
"Edited": null
}
],
"ID": 3,
"Name": "A"
},
{
"$id": "8",
"Tags": [],
"ID": 4,
"Name": "B"
}
];
myObj = sortTimeStamp(myObj);
console.log(myObj);
https://jsfiddle.net/3y7wfqwq/

How can i get the total count id of this collection using javascript?

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

Categories

Resources