If key values match, merge and reduce. Restructuring JSON data response - javascript

I am trying to restructure my fetched JSON data. A sample of my JSON response is here:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
I want to take the fields key and properties from the includes > Asset and put them into the correct item > fields > picture that has the matching ASSETID. Then the includes would just be removed from the whole thing because it is no longer needed.
This is what the final result that I want:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
]
}
What I currently have is a function that iterates over each item in items and tries to see if the Assets array includes the ASSETID from the item.
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
const data = {
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
I'm not sure how to spread the properties in the way that I want to. Am I going in the right direction or is my thinking off?
The solution ended up being:
data.items.forEach(item => {
if (item.fields.picture !== undefined) {
const { id } = item.fields.picture.sys;
const foundAssets = data.includes.Asset.find(asset => asset.sys.id === id);
item.fields.picture = { ...item.fields.picture, fields: {...foundAssets.fields} };
}
});
delete data.includes;

I guess could simply use forEach, do the matching, spread what is required and delete what is not required. Something like this
const input = {
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
input.items.forEach(i => {
const foundAsset = input.includes.Asset.find(j => j.id === i.id);
i.fields.picture = { ...i.fields.picture,
...foundAsset.fields
};
});
delete input.includes;
console.log(input);

Related

Change value or remove object from array of objects

I've an array of objects and I need to change the values or "decorate" array:
If value in some object is empty, remove whole object from the array.
If value is an object, take only label or name
My array:
[
{ "name": "certificate", "value": "", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Certificate", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": [ "2017" ], "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "key": "g", "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "packaging", "value": " ", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Packaging", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "key": "gold", "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]
expected output:
[
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": "2017", "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]
any idea how to handle it?
this way...
const data =
[
{ "name": "certificate", "value": "", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Certificate", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": [ "2017" ], "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "key": "g", "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "packaging", "value": " ", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Packaging", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "key": "gold", "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]
data.reduceRight( (z,el,indx,arr) =>
{
if (Object.prototype.toString.call(el.value) === '[object Object]')
{
Object.keys(el.value).forEach( k=> { if (k!=='label' && k!=='name') delete el.value[k] } )
if (Object.keys(el.value).length === 0)
{
arr.splice(indx, 1)
}
}
else if ( el.value.length === 0 || ( typeof(el.value) === 'string' && /^\s*$/.test(el.value) ) )
{
arr.splice(indx, 1)
}
}
, null )
console.log( data )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
One of the approaches could be
let array = [
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": "2017", "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]
let newArr = array.map((item) => {
if (!item.value) return
if (typeof item === "object" && Object.keys(item).length > 0) {
if (item["key"]) {
delete item["key"]
}
return item
} else return item
});
console.log(newArr)
My approach to handle this issue will be:
const arr = [{
"name": "certificate",
"value": "",
"attributeDefinition": {
"type": {
"name": "text",
"__typename": "TextAttributeDefinitionType"
},
"label": "Certificate",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "manufacturer",
"value": "China Mint",
"attributeDefinition": {
"type": {
"name": "text",
"__typename": "TextAttributeDefinitionType"
},
"label": "Manufacturer",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "countryOfOrigin",
"value": "China",
"attributeDefinition": {
"type": {
"name": "text",
"__typename": "TextAttributeDefinitionType"
},
"label": "Country of origin",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "grossWeight",
"value": 30,
"attributeDefinition": {
"type": {
"name": "number",
"__typename": "NumberAttributeDefinitionType"
},
"label": "Gross weight",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "yearsOfIssue",
"value": ["2017"],
"attributeDefinition": {
"type": {
"name": "set",
"__typename": "SetAttributeDefinitionType"
},
"label": "Year(s) of issue",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "fineWeightUnit",
"value": {
"key": "g",
"label": "g"
},
"attributeDefinition": {
"type": {
"name": "enum",
"__typename": "EnumAttributeDefinitionType"
},
"label": "Fine weight unit",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "packaging",
"value": " ",
"attributeDefinition": {
"type": {
"name": "text",
"__typename": "TextAttributeDefinitionType"
},
"label": "Packaging",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
{
"name": "preciousMetal",
"value": {
"key": "gold",
"label": "Gold"
},
"attributeDefinition": {
"type": {
"name": "enum",
"__typename": "EnumAttributeDefinitionType"
},
"label": "Precious metal",
"__typename": "AttributeDefinition"
},
"__typename": "RawProductAttribute"
},
];
arr.forEach((objects, index) => {
for (let object in objects) {
if (typeof objects[object] !== 'object') {
let value = objects[object].toString().trim();
if (!value.length) {
arr.splice(index, 1);
}
} else {
if (!objects[object].toString().trim().length) {
arr.splice(index, 1);
}
}
}
})
Here is a proposal, you can filter to remove empty values and make a map with a condition on type of the value property.
const data = [
{ "name": "certificate", "value": "", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Certificate", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "manufacturer", "value": "China Mint", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Manufacturer", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "countryOfOrigin", "value": "China", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Country of origin", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "grossWeight", "value": 30, "attributeDefinition": { "type": { "name": "number", "__typename": "NumberAttributeDefinitionType" }, "label": "Gross weight", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "yearsOfIssue", "value": [ "2017" ], "attributeDefinition": { "type": { "name": "set", "__typename": "SetAttributeDefinitionType" }, "label": "Year(s) of issue", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "fineWeightUnit", "value": { "key": "g", "label": "g" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Fine weight unit", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "packaging", "value": " ", "attributeDefinition": { "type": { "name": "text", "__typename": "TextAttributeDefinitionType" }, "label": "Packaging", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
{ "name": "preciousMetal", "value": { "key": "gold", "label": "Gold" }, "attributeDefinition": { "type": { "name": "enum", "__typename": "EnumAttributeDefinitionType" }, "label": "Precious metal", "__typename": "AttributeDefinition" }, "__typename": "RawProductAttribute" },
]
const arrNoEmptyValues = data.filter( element => element.value && element.value != " ")
const result = arrNoEmptyValues.map( element => {
if(typeof element.value === "object") {
return {
...element,
value:{ label: element.value.label || "", name: element.value.name || "" }
}
}
else {
return element
}
})
//there is a case you didn't forecast it's when there is no name nor label in value, i propose to return empty "" label and name in this case, so your object structure when value is an object is more consistent
console.log(result)

loopback referencesMany relations with array not working

when i use relation referencesMAny with array in loopback. i have got error when i remove referencesMAny relation it's working. i am using loopback 3
{ "error": {
"statusCode": 500,
"name": "Error",
"message": "5d53eaa49b826748f0b72ca5is not an ObjectID string",
}}
it's my json schema :
{
"name": "Products",
"plural": "Products",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mixins": {
"TimeStamp": true
},
"properties": {
"name": {
"type": "string",
"default": ""
},
"image": {
"type": [],
"required": true,
"default": []
},
"about": {
"type": "string",
"required": true,
"default": ""
},
"categoryId": {
"type": [],
"required": false,
"default": []
},
"userId": {
"type": "string",
"required": true,
"default": ""
},
"storeId": {
"type": "string",
"required": true,
"default": ""
},
"location": {
"type": "geopoint",
"required": false
},
"price": {
"type": "object",
"required": true,
"default": {
"discount": "",
"actualRate": "",
"finalRate": ""
}
},
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
}
},
"validations": [],
"relations": {
"userDeatils": {
"type": "belongsTo",
"model": "Users",
"foreignKey": "userId"
},
"categories": {
"type": "referencesMany",
"model": "Categories",
"foreignKey": "categoryId"
},
"storeDeatils": {
"type": "belongsTo",
"model": "Users",
"foreignKey": "userId"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
],
"methods": {}
}
my json data object like this :
{
"name": "deara",
"image": ["dsadsa"],
"about": "dear ser",
"categoryId": [
"5d53eaa49b826748f0b72ca5"
],
"userId": "dsdsadsa",
"storeId": "dsdsadsadsad",
"location": {
"lat": 0,
"lng": 0
},
"price": {
"discount": "121",
"actualRate": "321",
"finalRate": "231"
},
"createdAt": "2019-08-09T12:02:54.514Z",
"updatedAt": "2019-08-09T12:02:54.514Z"
}
i don't understand where am i wrong.
thanks in advance
Loopback expects that dsadsad232323 will be an identifier of an object of another model, so categoryId must be array of id.

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 to select specific object in JSON

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

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