JS Merging object properties from an array of objects by matching ids - javascript

I have two arrays of objects. I want to look at the first one, find typeId then look in the second array for the match (states.typeId == stateTypes.id) then merge those properties into the first array's found match object. If the properties have same key append "stateType" to the property name if not just bring it over. I think an example would best explain it.
Array of objects
"states": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
],
"stateTypes": [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
],
Wanted array
"newArray": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 1,
"stageType-name": "PENDING",
"stageType-description": "Pending",
"stageType-label": "Pending"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 3,
"stageType-name": "COMPLETED",
"stageType-description": "Completed",
"stageType-label": "Completed"
},
{
"id": 3,
"typeId": 2,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 2,
"stageType-name": "IN_PROGRESS",
"stageType-description": "In Progress",
"stageType-label": "In Progress"
}
],

Something like this:
const state = [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
];
const stateTypes = [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
];
const temp = {};
for (const obj1 of state) {
temp[obj1.id] = { ...obj1 };
}
for (const obj2 of stateTypes) {
const destination = temp[obj2.id];
for (const [key, value] of Object.entries(obj2)) {
if (destination[key]) destination[`stateTypes-${key}`] = value;
else destination[key] = value;
}
}
const newArray = Object.values(temp);
console.log(newArray);

Related

Sorting Node's on the base of parent-Id [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 2 years ago.
Want to sort my data all node come first before calling its parent node
[
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
}
]
want to update data like this using javascript
[
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
}
]
try this one
const sortedData = [
{
"id": 7832454551,
"name": "usr",
"type": "DIRECTORY"
},
{
"id": 7832454554,
"name": "applications",
"type": "DIRECTORY",
"parentId": 7832454553
},
{
"id": 7832454555,
"name": "mimeinfo.cache",
"type": "FILE",
"parentId": 7832454554
},
{
"id": 7832454553,
"name": "share",
"type": "DIRECTORY",
"parentId": 7832454552
},
{
"id": 7832454552,
"name": "local",
"type": "DIRECTORY",
"parentId": 7832454551
}
].sort(function(a,b){
if(a.parentId!= undefined && b.parentId != undefined){
return a.parentId > b.parentId
}
return false;
});
console.log(sortedData);

Get the distinct object from the array of objects in Typescript Angular 8

I have an array of object as shown below:
[{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Okta Verify TOTP",
"provider": "OKTA",
"type": "token:software:totp",
"status": 0,
"id": "osthgek5jmWTckcka0h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
I want to get the distinct object as array based on the **provider**
I tried
[...new Set(item.filter(factor => factor.status == MultiFactorAuthenticationEnrolmentStatus.Enrolled).map(factor => factor.provider))];
This returns string of array such as ["GOOGLE", "OKTA","CUSTOM"]
My requirement is to get the Array of Object such as
[{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
Reference - How to get distinct values from an array of objects in JavaScript?
In the case that you have a preference for taking the first occurrence, you can first map the data into an object based on provider being the key and itself as the value. Once that is done, you can then extract the all of the values with Object#values.
const data = [{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Okta Verify TOTP",
"provider": "OKTA",
"type": "token:software:totp",
"status": 0,
"id": "osthgek5jmWTckcka0h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
const values = Object.values(
data.reduce((a, b) => {
if (!a[b.provider]) a[b.provider] = b
return a
}, {})
)
console.log(values)

Filter Array of user objects by array of roles in Javascript

I have an array of user objects.
I want to filter them based on the array of user roles.
filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const users = [{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 2,
"email": "user2#test.com",
"name": "User2",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 3,
"email": "user3#test.com",
"name": "User3",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 5,
"email": "user5#test.com",
"name": "User5",
"roles": [{
"id": 5,
"code": "ROLE_LAWYER",
"name": "Lawyer"
}]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))
console.log(filteredUsers)
Expected result
[{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
I am trying to image what result you want to receive....
You want to filter of users by array with possible roles, another worlds if user has one of roles of filter array you want to pass his to 'filteredUsers' array?
filter.includes(role.id) - I guess it is wrong, may be you want to
filter by role.code?
Array.find() doesn't support Explorer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.includes() - doesn't support Explorer too.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
I think the reason why that was happening was that you are trying to match the role id with the 'filter' array which contains the role code. From what i observed from the expected answer, you are looking for user who has any one of their role fits the filter. So do the following will filter by role code and with the expected result
const filteredUsers = users.filter((user) => {
return user.roles.map(role=>filter.includes(role.code)).includes(true)
})
this line of code basically map the filter to every role object to every user, if their role code is included in the filter it will add a true to the array(return array of map()) and for the filter function if the map() return array contains true then true(so basically a || for the whole array)

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

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