Mapping/digging into an array that is nested in an object - JavaScript - javascript

This is my first question on stack overflow. I am attempting to map an array that is housed within an object. My question is more regarding the targeting than the actual mapping process itself (I think). The goal of my code is to map an array to this target:
var target = {
"id": 1, //as an int
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": "all of the batter types as a string",
"ingredients": [],//a copy of all the toppings
"countOfFillings": 0
}
the object in question is:
var bakery = {
"items":
{
"item":
[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0003",
"type": "donut",
"name": "Old Fashioned",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
},
{
"id": "0004",
"type": "bar",
"name": "Bar",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7001", "name": "None", "addcost": 0 },
{ "id": "7002", "name": "Custard", "addcost": 0.25 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0.25 }
]
}
},
{
"id": "0005",
"type": "twist",
"name": "Twist",
"ppu": 0.65,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
]
},
{
"id": "0006",
"type": "filled",
"name": "Filled",
"ppu": 0.75,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
]
},
"topping":
[
{ "id": "5002", "type": "Glazed" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
],
"fillings":
{
"filling":
[
{ "id": "7002", "name": "Custard", "addcost": 0 },
{ "id": "7003", "name": "Whipped Cream", "addcost": 0 },
{ "id": "7004", "name": "Strawberry Jelly", "addcost": 0 },
{ "id": "7005", "name": "Rasberry Jelly", "addcost": 0 }
]
}
}
]
}
}
And this is my attempt at mapping. I'm able to get the array generated but only from the first item in the array as indicated with the [0]. Any feedback or advice would be greatly appreciated!
var exampleTest = bakery.items.item.map(mapCake);
function mapCake (aCake)
{
let newType = `${bakery.items.item[1].type}`
let result = {id: `${bakery.items.item[0].id}`,
type: newType,
name: `${bakery.items.item[0].name}`,
ppu: `${bakery.items.item[0].ppu}`,
batters: [`${bakery.items.item[0].batters.batter}`],
ingredients: `${bakery.items.item[0].topping}`,
countOfFillings: `${bakery.items.item[0].fillings}`.length};
return result
}

Try this:
function mapCake(aCake) {
let newType = `${aCake.type}`;
let result = {
id: `${aCake.id}`,
type: newType,
name: `${aCake.name}`,
ppu: `${aCake.ppu}`,
batters: [`${aCake.batters.batter}`],
ingredients: `${aCake.topping}`,
countOfFillings: `${aCake.fillings}`.length
};
return result;
}
Looks like in your function you're directly indexing a specific bakery item each time.
Your aCake parameter wasn't yet being used, at each iteration, aCake refers to the current item that is being iterated over - so that's what you want to be populating your result values with.

Related

How to keep attributes for an object in javascript if they have a specific key/value

I have an api call response with this format.
They are token and I know how to filter an array.
But here instead of an array I have an object.
{
"AFjQbjBwEF4HfBgKYTeK3SZg4cGRuB9o3rNGmQvHwtFX": {
"name": "000# Heartbreak",
"symbol": "NNPOSTER",
"description": "The crystal broken heart is a mirror through which residents of the NEONEXUS can take a peek into the real world as we know it.",
"seller_fee_basis_points": 500,
"image": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg",
"animation_url": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4",
"external_url": "https://neonexus.io",
"attributes": [
{
"trait_type": "City",
"value": "Miami"
},
{
"trait_type": "Theme",
"value": "Heartbreak in Miami"
},
{
"trait_type": "Origin",
"value": "Airdrop"
},
{
"trait_type": "Scene",
"value": "Moody"
},
{
"trait_type": "Period",
"value": "Lockdown"
},
{
"trait_type": "Artist",
"value": "Daramola"
}
],
"collection": {
"name": "NEONEXUS Posters",
"family": "Raynesfere"
},
"properties": {
"files": [
{
"uri": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg",
"type": "image/gif"
},
{
"uri": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4",
"type": "video/mp4"
}
],
"category": "video",
"creators": [
{
"address": "Ef896VQS1tfMACgL5Ce1TR7JZhehkrtowCyZZyYv7SWn",
"share": 100
}
]
}
},
"F8EeyxkR5EBs7ALaapV2jNos8jdbdgpqjtEaJ6fVfB3K": {
"name": "Skellies #4107",
"symbol": "SKULL",
"description": "Skellies is a collection of 8888 spooky skeletons presented by the SolSocks team",
"seller_fee_basis_points": 500,
"image": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png",
"attributes": [
{
"trait_type": "Background",
"value": "Pepto"
},
{
"trait_type": "Skin",
"value": "Purple Haze"
},
{
"trait_type": "Eyes",
"value": "Angry"
},
{
"trait_type": "Nose",
"value": "Double Double"
},
{
"trait_type": "Chest",
"value": "None"
},
{
"trait_type": "Ears",
"value": "none"
},
{
"trait_type": "Mouth",
"value": "WGMI #2"
},
{
"trait_type": "Glasses",
"value": "none"
},
{
"trait_type": "Headwear",
"value": "Droid"
},
{
"trait_type": "Limited Edition",
"value": "no"
},
{
"trait_type": "Dna",
"value": "82310019050"
}
],
"collection": {
"name": "Skellies X SolSocks",
"family": "Skellies"
},
"properties": {
"files": [
{
"uri": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png",
"type": "image/png"
}
],
"category": "image",
"creators": [
{
"address": "CpvHujvbSJ9btvPmP5FUKC8QpGM8AQjFk7uLJSsomoH2",
"share": 85
},
{
"address": "BQ6FxfRVDWKUVeqD9uHFqBjebJ9Um8RVzPTPZ1bBoByU",
"share": 15
}
]
}
},
}
I need to keep only the tokens with a specific collection.name: "Skellies X SolSocks".
I cannot use map because is an object.
Is there a map/filter/reduce function I can use?
You could use Object.entries(), to get an array of the objects key/value pairs.
We can then use Array.filter() to filter by any property, in this case name, then we can use Object.fromEntries() to create the desired result.
let obj = { "AFjQbjBwEF4HfBgKYTeK3SZg4cGRuB9o3rNGmQvHwtFX": { "name": "000# Heartbreak", "symbol": "NNPOSTER", "description": "The crystal broken heart is a mirror through which residents of the NEONEXUS can take a peek into the real world as we know it.", "seller_fee_basis_points": 500, "image": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg", "animation_url": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4", "external_url": "https://neonexus.io", "attributes": [ { "trait_type": "City", "value": "Miami" }, { "trait_type": "Theme", "value": "Heartbreak in Miami" }, { "trait_type": "Origin", "value": "Airdrop" }, { "trait_type": "Scene", "value": "Moody" }, { "trait_type": "Period", "value": "Lockdown" }, { "trait_type": "Artist", "value": "Daramola" } ], "collection": { "name": "NEONEXUS Posters", "family": "Raynesfere" }, "properties": { "files": [ { "uri": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg", "type": "image/gif" }, { "uri": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4", "type": "video/mp4" } ], "category": "video", "creators": [ { "address": "Ef896VQS1tfMACgL5Ce1TR7JZhehkrtowCyZZyYv7SWn", "share": 100 } ] } }, "F8EeyxkR5EBs7ALaapV2jNos8jdbdgpqjtEaJ6fVfB3K": { "name": "Skellies #4107", "symbol": "SKULL", "description": "Skellies is a collection of 8888 spooky skeletons presented by the SolSocks team", "seller_fee_basis_points": 500, "image": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png", "attributes": [ { "trait_type": "Background", "value": "Pepto" }, { "trait_type": "Skin", "value": "Purple Haze" }, { "trait_type": "Eyes", "value": "Angry" }, { "trait_type": "Nose", "value": "Double Double" }, { "trait_type": "Chest", "value": "None" }, { "trait_type": "Ears", "value": "none" }, { "trait_type": "Mouth", "value": "WGMI #2" }, { "trait_type": "Glasses", "value": "none" }, { "trait_type": "Headwear", "value": "Droid" }, { "trait_type": "Limited Edition", "value": "no" }, { "trait_type": "Dna", "value": "82310019050" } ], "collection": { "name": "Skellies X SolSocks", "family": "Skellies" }, "properties": { "files": [ { "uri": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png", "type": "image/png" } ], "category": "image", "creators": [ { "address": "CpvHujvbSJ9btvPmP5FUKC8QpGM8AQjFk7uLJSsomoH2", "share": 85 }, { "address": "BQ6FxfRVDWKUVeqD9uHFqBjebJ9Um8RVzPTPZ1bBoByU", "share": 15 } ] } }, }
const searchTerm = 'Skellies';
const result = Object.fromEntries(Object.entries(obj).filter(([key, val]) => {
return val.name.includes(searchTerm);
}));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could also try a for .. in loop to iterate over the object keys, adding any child objects with a matching name value.
let obj = { "AFjQbjBwEF4HfBgKYTeK3SZg4cGRuB9o3rNGmQvHwtFX": { "name": "000# Heartbreak", "symbol": "NNPOSTER", "description": "The crystal broken heart is a mirror through which residents of the NEONEXUS can take a peek into the real world as we know it.", "seller_fee_basis_points": 500, "image": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg", "animation_url": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4", "external_url": "https://neonexus.io", "attributes": [ { "trait_type": "City", "value": "Miami" }, { "trait_type": "Theme", "value": "Heartbreak in Miami" }, { "trait_type": "Origin", "value": "Airdrop" }, { "trait_type": "Scene", "value": "Moody" }, { "trait_type": "Period", "value": "Lockdown" }, { "trait_type": "Artist", "value": "Daramola" } ], "collection": { "name": "NEONEXUS Posters", "family": "Raynesfere" }, "properties": { "files": [ { "uri": "https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg", "type": "image/gif" }, { "uri": "https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4", "type": "video/mp4" } ], "category": "video", "creators": [ { "address": "Ef896VQS1tfMACgL5Ce1TR7JZhehkrtowCyZZyYv7SWn", "share": 100 } ] } }, "F8EeyxkR5EBs7ALaapV2jNos8jdbdgpqjtEaJ6fVfB3K": { "name": "Skellies #4107", "symbol": "SKULL", "description": "Skellies is a collection of 8888 spooky skeletons presented by the SolSocks team", "seller_fee_basis_points": 500, "image": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png", "attributes": [ { "trait_type": "Background", "value": "Pepto" }, { "trait_type": "Skin", "value": "Purple Haze" }, { "trait_type": "Eyes", "value": "Angry" }, { "trait_type": "Nose", "value": "Double Double" }, { "trait_type": "Chest", "value": "None" }, { "trait_type": "Ears", "value": "none" }, { "trait_type": "Mouth", "value": "WGMI #2" }, { "trait_type": "Glasses", "value": "none" }, { "trait_type": "Headwear", "value": "Droid" }, { "trait_type": "Limited Edition", "value": "no" }, { "trait_type": "Dna", "value": "82310019050" } ], "collection": { "name": "Skellies X SolSocks", "family": "Skellies" }, "properties": { "files": [ { "uri": "https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png", "type": "image/png" } ], "category": "image", "creators": [ { "address": "CpvHujvbSJ9btvPmP5FUKC8QpGM8AQjFk7uLJSsomoH2", "share": 85 }, { "address": "BQ6FxfRVDWKUVeqD9uHFqBjebJ9Um8RVzPTPZ1bBoByU", "share": 15 } ] } }, }
const result = {};
const searchTerm = 'Skellies';
for(let k in obj) {
if (obj[k] && ((obj[k].name || "").includes(searchTerm))) {
result[k] = obj[k];
}
}
console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can simply use a for loop to loop over the object's entries:
const yourObject = {"AFjQbjBwEF4HfBgKYTeK3SZg4cGRuB9o3rNGmQvHwtFX":{"name":"000# Heartbreak","symbol":"NNPOSTER","description":"The crystal broken heart is a mirror through which residents of the NEONEXUS can take a peek into the real world as we know it.","seller_fee_basis_points":500,"image":"https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg","animation_url":"https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4","external_url":"https://neonexus.io","attributes":[{"trait_type":"City","value":"Miami"},{"trait_type":"Theme","value":"Heartbreak in Miami"},{"trait_type":"Origin","value":"Airdrop"},{"trait_type":"Scene","value":"Moody"},{"trait_type":"Period","value":"Lockdown"},{"trait_type":"Artist","value":"Daramola"}],"collection":{"name":"NEONEXUS Posters","family":"Raynesfere"},"properties":{"files":[{"uri":"https://arweave.net/8TpUvw7hi7N_rVuG6avV_cOX2bXQ7OsY-ZNtILCM-zo?ext=jpg","type":"image/gif"},{"uri":"https://arweave.net/WdzVw-_XE7LQArXArYJNpeMSgFbYYwa1ATpCVrfrTK8?ext=mp4","type":"video/mp4"}],"category":"video","creators":[{"address":"Ef896VQS1tfMACgL5Ce1TR7JZhehkrtowCyZZyYv7SWn","share":100}]}},"F8EeyxkR5EBs7ALaapV2jNos8jdbdgpqjtEaJ6fVfB3K":{"name":"Skellies #4107","symbol":"SKULL","description":"Skellies is a collection of 8888 spooky skeletons presented by the SolSocks team","seller_fee_basis_points":500,"image":"https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png","attributes":[{"trait_type":"Background","value":"Pepto"},{"trait_type":"Skin","value":"Purple Haze"},{"trait_type":"Eyes","value":"Angry"},{"trait_type":"Nose","value":"Double Double"},{"trait_type":"Chest","value":"None"},{"trait_type":"Ears","value":"none"},{"trait_type":"Mouth","value":"WGMI #2"},{"trait_type":"Glasses","value":"none"},{"trait_type":"Headwear","value":"Droid"},{"trait_type":"Limited Edition","value":"no"},{"trait_type":"Dna","value":"82310019050"}],"collection":{"name":"Skellies X SolSocks","family":"Skellies"},"properties":{"files":[{"uri":"https://www.arweave.net/mG5dY4GC-3g9j661NRQWoGYWKUtbqDvfhW8whDVVEF0?ext=png","type":"image/png"}],"category":"image","creators":[{"address":"CpvHujvbSJ9btvPmP5FUKC8QpGM8AQjFk7uLJSsomoH2","share":85},{"address":"BQ6FxfRVDWKUVeqD9uHFqBjebJ9Um8RVzPTPZ1bBoByU","share":15}]}}};
const tokens = [];
const filteredObject = {};
for (let [token, entry] of Object.entries(yourObject)) {
if (entry.collection.name === 'Skellies X SolSocks') {
tokens.push(token);
filteredObject[token] = entry;
}
}
console.log(tokens);
console.log(filteredObject);

JS: merging 2 arrays within an object

Given a JSON object, how would you merge the batter array and topping array within the object. I know I can do let x = foo.batters.batter.concat(foo.topping), which will give me an array of the merged array but what if I wanted it within object?
foo = {
"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" }
]
}
Your answer is partially true. But you didn't add the value to the foo object.
You can do it like this:
foo.mergedArray = foo.batters.batter.concat(foo.topping);
After that, when you call foo.mergedArray you can access merged array of batter and topping.
If one of the batter and topping has changed foo.mergedArray will not be updated. Because foo.mergedArray has its own referance.
Try this:
foo.batters.batter = [...foo.batters.batter, ...foo.topping];

Kendo UI HierarchicalDataSource for Treeview with dynamic schema

I'm trying to display a Kendo treeview thanks to a HierarchicalDataSource.
It's a simple region, country, city hierarchy but the difficulty comes with the fact that we have 3 possible levels of regions and countries can appear at the second or at the third level.
Basically,
- First level of regions contains only other regions
- Second level of regions contains other regions or countries
- Third level of regions contains only countries
Here are my schemas:
var portsSchema = {
schema: {
data: "PortList",
model: {
id: "Code"
}
}
};
var countrySchema = {
schema: {
data: "CountryList",
model: {
id: "Code",
children: portsSchema
}
}
};
var regionCountrySchema = {
schema: {
data: "RegionList",
model: {
id: "id",
children: countrySchema
}
}
};
var regionSchema = {
schema: {
data: "RegionList",
model: {
id: "id",
children: regionCountrySchema
}
}
};
Depending on the fact that the region has countries or not, I would like to specify a specific type of children (regionSchema or regionCountrySchema).
var tvDataSource = new kendo.data.HierarchicalDataSource({
data: regions,
schema: {
model: function (data) {
**how to return the right children schema ?**
}
}
});
Returning { children: regionSchema } or {children: regionCountrySchema } triggers a js kendo error.
Any idea to achieve this ? Thank you.
JSON data sample below.
[
{
"Id": 1,
"Name": "MIDDLE EAST AND RED SEA",
"RegionList": [
{
"Id": 12,
"Name": "MIDDLE EAST",
"RegionList": [
{
"Id": 45,
"Name": "M. EAST",
"RegionList": [
],
"CountryList": [
{
"Id": 12007,
"Code": "AE",
"Name": "UAE",
"PortList": [
{
"Id": 6005,
"Code": "AEJEA",
"Name": "JEBEL ALI"
},
{
"Id": 16014,
"Code": "AEAJM",
"Name": "AJMAN"
},
{
"Id": 16015,
"Code": "AEAUH",
"Name": "ABU DHABI"
},
{
"Id": 15109,
"Code": "AEKLF",
"Name": "KHOR AL FAKKAN"
},
{
"Id": 15001,
"Code": "AERKT",
"Name": "RAS AL KHAIMAH"
},
{
"Id": 16018,
"Code": "AESHJ",
"Name": "SHARJAH"
},
{
"Id": 14863,
"Code": "AEQIW",
"Name": "UMM AL QAIWAIN"
},
{
"Id": 15647,
"Code": "AEFJR",
"Name": "AL - FUJAYRAH"
}
]
},
{
"Id": 12018,
"Code": "OM",
"Name": "OMAN",
"PortList": [
{
"Id": 6011,
"Code": "OMSLL",
"Name": "SALALAH"
},
{
"Id": 16218,
"Code": "OMSOH",
"Name": "SOHAR"
}
]
},
{
"Id": 10069,
"Code": "BH",
"Name": "BAHRAIN",
"PortList": [
{
"Id": 15345,
"Code": "BHKBS",
"Name": "BAHRAIN"
}
]
},
{
"Id": 62292,
"Code": "IQ",
"Name": "IRAQ",
"PortList": [
{
"Id": 15383,
"Code": "IQBSR",
"Name": "BASRA"
},
{
"Id": 14673,
"Code": "IQUQR",
"Name": "UMM QASR PT"
}
]
},
{
"Id": 62291,
"Code": "IR",
"Name": "IRAN, ISLAMIC REPUBLIC OF",
"PortList": [
{
"Id": 15250,
"Code": "IRBKM",
"Name": "BANDAR KHOMEINI"
},
{
"Id": 15249,
"Code": "IRBND",
"Name": "BANDAR ABBAS"
},
{
"Id": 14973,
"Code": "IRBUZ",
"Name": "BUSHEHR"
},
{
"Id": 14671,
"Code": "IRKHO",
"Name": "KHORRAMSHAHR"
}
]
},
{
"Id": 62306,
"Code": "KW",
"Name": "KUWAIT",
"PortList": [
{
"Id": 15810,
"Code": "KWSAA",
"Name": "SHUAIBA"
},
{
"Id": 15811,
"Code": "KWSWK",
"Name": "SHUWAIKH"
}
]
},
{
"Id": 12002,
"Code": "SA",
"Name": "SAUDI ARABIA",
"PortList": [
{
"Id": 15039,
"Code": "SAJUB",
"Name": "JUBAIL"
},
{
"Id": 16147,
"Code": "SADMM",
"Name": "AD DAMMAM"
}
]
},
{
"Id": 62364,
"Code": "QA",
"Name": "QATAR",
"PortList": [
{
"Id": 15739,
"Code": "QADOH",
"Name": "DOHA"
},
{
"Id": 14795,
"Code": "QAMES",
"Name": "MESAIEED"
}
]
}
]
}
],
"CountryList": [
]
},
{
"Id": 30,
"Name": "RED SEA",
"RegionList": [
{
"Id": 65,
"Name": "RED SEA",
"RegionList": [
],
"CountryList": [
]
}
],
"CountryList": [
{
"Id": 12002,
"Code": "SA",
"Name": "SAUDI ARABIA",
"PortList": [
{
"Id": 6003,
"Code": "SAKAC",
"Name": "KING ABDULLAH PORT"
},
{
"Id": 15731,
"Code": "SAJED",
"Name": "JEDDAH"
}
]
},
{
"Id": 10114,
"Code": "DJ",
"Name": "DJIBOUTI",
"PortList": [
{
"Id": 8059,
"Code": "DJJIB",
"Name": "DJIBOUTI"
}
]
},
{
"Id": 10122,
"Code": "ER",
"Name": "ERITREA",
"PortList": [
{
"Id": 15031,
"Code": "ERMSW",
"Name": "MASSAWA"
}
]
},
{
"Id": 62300,
"Code": "JO",
"Name": "JORDAN",
"PortList": [
{
"Id": 14801,
"Code": "JOAQJ",
"Name": "AL \u0027AQABAH"
}
]
},
{
"Id": 50001,
"Code": "SD",
"Name": "sd",
"PortList": [
{
"Id": 15734,
"Code": "SDPZU",
"Name": "PORT SUDAN"
}
]
},
{
"Id": 62425,
"Code": "YE",
"Name": "YEMEN",
"PortList": [
{
"Id": 15302,
"Code": "YEHOD",
"Name": "HODEIDAH"
},
{
"Id": 15304,
"Code": "YEMKX",
"Name": "MUKALLA"
},
{
"Id": 15300,
"Code": "YEADE",
"Name": "ADEN"
}
]
},
{
"Id": 10118,
"Code": "EG",
"Name": "EGYPT",
"PortList": [
{
"Id": 16030,
"Code": "EGSOK",
"Name": "SOKHNA PORT"
}
]
}
]
}
],
"CountryList": [
]
}
]

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