Change value or remove object from array of objects - javascript

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)

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

Mapping/digging into an array that is nested in an object - 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.

unable to call onchange function on windows load

window.onload = function (){
$("#work_sanctioned_year").change();
};
$(document).ready(function(){
$("#work_sanctioned_year").change();
});
$("#work-sanctioned-year").change(function(){
var work_sanctioned_year = $(this).val();
alert(work_sanctioned_year);
$.ajax({
url: 'get_data.php',
type: 'post',
data: {work_sanctioned_year:work_sanctioned_year},
dataType: 'json',
success:function(response){
FusionCharts.ready();
}
});
FusionCharts.ready(function() {
var dataChart = new FusionCharts({
"type": "overlappedBar2d",
"renderAt": "chart-5",
"width": '100%',
"height": '400',
"dataFormat": "json",
"dataSource": {
"chart": {
"caption": "Major League Baseball - Season Rankings",
"subCaption": "Teams in the Lead",
"xAxisName": "Team",
"yAxisName": "Position",
"theme": "fusion",
"showValues": "0"
},
"categories": [{
"labelFontSize": "13",
"category": [{
"label": "Boston Red Sox"
}, {
"label": "New York Yankees"
}, {
"label": "Tampa Bay Rays"
}, {
"label": "Toronto Blue Jays"
}, {
"label": "Baltimore Orioles"
}, {
"label": "Cleveland Indians"
}, {
"label": "Detroit Tigers"
}, {
"label": "Minnesota Twins"
}, {
"label": "Chicago White Sox"
}, {
"label": "Kansas City Royals"
}, {
"label": "Cleveland Indians"
}, {
"label": "Detroit Tigers"
}, {
"label": "Minnesota Twins"
}, {
"label": "Chicago White Sox"
}]
}],
"dataset": [{
"seriesname": "Matches",
"data": [{
"value": parseInt(document.getElementById("work_sanctioned").value)-4650
}, {
"value": "84"
}, {
"value": "86"
}, {
"value": "86"
}, {
"value": "85"
}, {
"value": "85"
}, {
"value": "88"
}, {
"value": "83"
}, {
"value": "86"
}, {
"value": "86"
}, {
"value": "85"
}, {
"value": "88"
}, {
"value": "83"
}, {
"value": "86"
}]
}, {
"seriesname": "Wins",
"data": [{
"value": "57"
}, {
"value": "54"
}, {
"value": "42"
}, {
"value": "39"
}, {
"value": "24"
}, {
"value": "46"
}, {
"value": "38"
}, {
"value": "35"
}, {
"value": "29"
}, {
"value": "25"
}, {
"value": "46"
}, {
"value": "38"
}, {
"value": "35"
}, {
"value": "29"
}, {
"value": "25"
}]
}]
}
})
.render();
});
});
when m selecting something from the dropdown its working. but when what m trying to do is call the function with the first value by default.I have tried both window.onload method and .ready function individually but still function is not getting called.. and also m not getting error in console related to this so this is making it harder to spot what actually is wrong over here
<script>
function work_sanctioned(){
console.log('onchange');
var work_sanctioned_year = $("#work-sanctioned-year").val();
alert(work_sanctioned_year);
$.ajax({
url: 'get_data.php',
type: 'post',
data: {work_sanctioned_year:work_sanctioned_year},
dataType: 'json',
success:function(response){
FusionCharts.ready();
}
});
FusionCharts.ready(function() {
var dataChart = new FusionCharts({
"type": "overlappedBar2d",
"renderAt": "chart-5",
"width": '100%',
"height": '400',
"dataFormat": "json",
"dataSource": {
"chart": {
"caption": "Major League Baseball - Season Rankings",
"subCaption": "Teams in the Lead",
"xAxisName": "Team",
"yAxisName": "Position",
"theme": "fusion",
"showValues": "0"
},
"categories": [{
"labelFontSize": "13",
"category": [{
"label": "Boston Red Sox"
}, {
"label": "New York Yankees"
}, {
"label": "Tampa Bay Rays"
}, {
"label": "Toronto Blue Jays"
}, {
"label": "Baltimore Orioles"
}, {
"label": "Cleveland Indians"
}, {
"label": "Detroit Tigers"
}, {
"label": "Minnesota Twins"
}, {
"label": "Chicago White Sox"
}, {
"label": "Kansas City Royals"
}, {
"label": "Cleveland Indians"
}, {
"label": "Detroit Tigers"
}, {
"label": "Minnesota Twins"
}, {
"label": "Chicago White Sox"
}]
}],
"dataset": [{
"seriesname": "Matches",
"data": [{
"value": parseInt(document.getElementById("work_sanctioned").value)-4650
}, {
"value": "84"
}, {
"value": "86"
}, {
"value": "86"
}, {
"value": "85"
}, {
"value": "85"
}, {
"value": "88"
}, {
"value": "83"
}, {
"value": "86"
}, {
"value": "86"
}, {
"value": "85"
}, {
"value": "88"
}, {
"value": "83"
}, {
"value": "86"
}]
}, {
"seriesname": "Wins",
"data": [{
"value": "57"
}, {
"value": "54"
}, {
"value": "42"
}, {
"value": "39"
}, {
"value": "24"
}, {
"value": "46"
}, {
"value": "38"
}, {
"value": "35"
}, {
"value": "29"
}, {
"value": "25"
}, {
"value": "46"
}, {
"value": "38"
}, {
"value": "35"
}, {
"value": "29"
}, {
"value": "25"
}]
}]
}
})
.render();
});
}
</script>
and then called this on window.onload as well as on select property onchange
<select id="work-sanctioned-year" onChange="work_sanctioned()" style="max-width:40%;min-width:20%; margin:auto;">
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
</select>
finally on window.onload
<script>
window.onload = function (){
work_sanctioned();
};
</script>

Jquery Javascript object - compare and replace some of the values with another similar object

There is an Javascript object (formData) that I need to replace some of the values by comparing with another object (dataObject). The JSON for both objects is below. Now what I need is, snippet:
Main formData Object:
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}
I need to compare the "value" to the other dataObject by "name" property and replace if it's different.
dataObject to compare to:
{
"name": "abNotes",
"value": "Example Notes 123456"
}
So the replaced FormData object will be changed to:
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes 123456",
"active": true
}
Main object that needs to be replaced
JSON.stringify (formData);
form data::
{
"name": "Demo",
"fieldGroupList": [{
"name": "instructions",
"label": "Instructions",
"fieldList": [{
"name": "INSTRUCTION",
"instructionList": [{
"instructionText": "All enabled fields are required."
}],
"type": "INSTRUCTION"
}]
},
{
"name": "ab",
"label": "Ab",
"fieldList": [{
"name": "abDate",
"label": "Ab Date",
"type": "DATE",
"value": "1425186000000",
"active": true
},
{
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}]
},
{
"name": "Record",
"label": "Record",
"fieldList": [{
"name": "RecordNumber",
"label": "Record Number: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "1234567890",
"active": true
},
{
"name": "otherNumber",
"label": "Other: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "887766",
"active": true
},
{
"name": "eligibleAll",
"instructionList": [{
"instructionText": "Is eligible for ALL?"
}],
"type": "SINGLE_FROM_SET",
"value": "true",
"optionList": [{
"name": "Yes",
"value": "true"
},
{
"name": "No",
"value": "false"
}],
"active": true
},
{
"name": "exclusionReasonCode",
"instructionList": [{
"instructionText": "Select from the following list of sample:"
}],
"type": "SINGLE_FROM_SET",
"value": "DCS",
"optionList": [{
"name": "DCS",
"value": "Test"
}],
"active": true
}]
},
{
"name": "bDemo",
"label": "Demo",
"fieldList": [{
"name": "mId",
"label": "M ID:",
"type": "TEXT_BOX_SINGLE",
"active": false
},
{
"name": "firstName",
"label": "First Name:",
"type": "TEXT_BOX_SINGLE",
"value": "John",
"active": true
},
{
"name": "lastName",
"label": "Last Name",
"type": "TEXT_BOX_SINGLE",
"value": "Doe",
"active": true
},
{
"name": "genderCode",
"instructionList": [{
"instructionText": "Gender:"
}],
"type": "SINGLE_FROM_SET",
"optionList": [{
"name": "FEMALE",
"value": "FEMALE"
},
{
"name": "MALE",
"value": "MALE"
},
{
"name": "UNKNOWN",
"value": "UNKNOWN"
}],
"active": true
},
{
"name": "dateOfBirth",
"label": "Date of Birth:",
"type": "DATE",
"value": "-157748400000",
"active": true
}]
},
{
"name": "generalComments",
"label": "General Comments",
"fieldList": [{
"name": "comments",
"label": "Comments: (Optional)",
"type": "TEXT_BOX_MULTIPLE",
"value": "Comments Text Example",
"active": true
}]
}],
"Id": 1,
"periodId": 2015,
"orgId": 4,
"version": 1
}
The values that I need from this object:
var dataObject = $('#'+formName).serializeArray();
console.log("data:::"+JSON.stringify(dataObject));
dataObject:::
[{
"name": "abDate",
"value": "Sun Mar 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
},
{
"name": "abNotes",
"value": "Example Notes 123456"
},
{
"name": "Number",
"value": "1234567890"
},
{
"name": "otherNumber",
"value": "887766"
},
{
"name": "Yes",
"value": "true"
},
{
"name": "No",
"value": "false"
},
{
"name": "DCS",
"value": "Test"
},
{
"name": "firstName",
"value": "John"
},
{
"name": "lastName",
"value": "Doe"
},
{
"name": "FEMALE",
"value": "FEMALE"
},
{
"name": "MALE",
"value": "MALE"
},
{
"name": "UNKNOWN",
"value": "UNKNOWN"
},
{
"name": "dateOfBirth",
"value": "Fri Jan 01 1965 00:00:00 GMT-0500 (Eastern Standard Time)"
},
{
"name": "comments",
"value": "Comments Text Example"
}]
EDIT:
What I have tried so far:
$.extend(formData ,dataObject);
deepCopy:
function deepCopy(src, dest) {
var name,
value,
isArray,
toString = Object.prototype.toString;
// If no `dest`, create one
if (!dest) {
isArray = toString.call(src) === "[object Array]";
if (isArray) {
dest = [];
dest.length = src.length;
}
else { // You could have lots of checks here for other types of objects
dest = {};
}
}
// Loop through the props
for (name in src) {
// If you don't want to copy inherited properties, add a `hasOwnProperty` check here
// In our case, we only do that for arrays, but it depends on your needs
if (!isArray || src.hasOwnProperty(name)) {
value = src[name];
if (typeof value === "object") {
// Recurse
value = deepCopy(value);
}
dest[name] = value;
}
}
return dest;
}
First, convert dataObject from an array of objects to an object that maps names to values. Then go through the main form data, replacing values from the corresponding elements of doHash.
var formData = {
"name": "Demo",
"fieldGroupList": [{
"name": "instructions",
"label": "Instructions",
"fieldList": [{
"name": "INSTRUCTION",
"instructionList": [{
"instructionText": "All enabled fields are required."
}],
"type": "INSTRUCTION"
}]
}, {
"name": "ab",
"label": "Ab",
"fieldList": [{
"name": "abDate",
"label": "Ab Date",
"type": "DATE",
"value": "1425186000000",
"active": true
}, {
"name": "abNotes",
"label": "Ab Notes: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "Example Notes",
"active": true
}]
}, {
"name": "Record",
"label": "Record",
"fieldList": [{
"name": "RecordNumber",
"label": "Record Number: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "1234567890",
"active": true
}, {
"name": "otherNumber",
"label": "Other: (Optional)",
"type": "TEXT_BOX_SINGLE",
"value": "887766",
"active": true
}, {
"name": "eligibleAll",
"instructionList": [{
"instructionText": "Is eligible for ALL?"
}],
"type": "SINGLE_FROM_SET",
"value": "true",
"optionList": [{
"name": "Yes",
"value": "true"
}, {
"name": "No",
"value": "false"
}],
"active": true
}, {
"name": "exclusionReasonCode",
"instructionList": [{
"instructionText": "Select from the following list of sample:"
}],
"type": "SINGLE_FROM_SET",
"value": "DCS",
"optionList": [{
"name": "DCS",
"value": "Test"
}],
"active": true
}]
}, {
"name": "bDemo",
"label": "Demo",
"fieldList": [{
"name": "mId",
"label": "M ID:",
"type": "TEXT_BOX_SINGLE",
"active": false
}, {
"name": "firstName",
"label": "First Name:",
"type": "TEXT_BOX_SINGLE",
"value": "John",
"active": true
}, {
"name": "lastName",
"label": "Last Name",
"type": "TEXT_BOX_SINGLE",
"value": "Doe",
"active": true
}, {
"name": "genderCode",
"instructionList": [{
"instructionText": "Gender:"
}],
"type": "SINGLE_FROM_SET",
"optionList": [{
"name": "FEMALE",
"value": "FEMALE"
}, {
"name": "MALE",
"value": "MALE"
}, {
"name": "UNKNOWN",
"value": "UNKNOWN"
}],
"active": true
}, {
"name": "dateOfBirth",
"label": "Date of Birth:",
"type": "DATE",
"value": "-157748400000",
"active": true
}]
}, {
"name": "generalComments",
"label": "General Comments",
"fieldList": [{
"name": "comments",
"label": "Comments: (Optional)",
"type": "TEXT_BOX_MULTIPLE",
"value": "Comments Text Example",
"active": true
}]
}],
"Id": 1,
"periodId": 2015,
"orgId": 4,
"version": 1
};
var dataObject = [{
"name": "abDate",
"value": "Sun Mar 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)"
}, {
"name": "abNotes",
"value": "Example Notes 123456"
}, {
"name": "Number",
"value": "1234567890"
}, {
"name": "otherNumber",
"value": "887766"
}, {
"name": "Yes",
"value": "true"
}, {
"name": "No",
"value": "false"
}, {
"name": "DCS",
"value": "Test"
}, {
"name": "firstName",
"value": "John"
}, {
"name": "lastName",
"value": "Doe"
}, {
"name": "FEMALE",
"value": "FEMALE"
}, {
"name": "MALE",
"value": "MALE"
}, {
"name": "UNKNOWN",
"value": "UNKNOWN"
}, {
"name": "dateOfBirth",
"value": "Fri Jan 01 1965 00:00:00 GMT-0500 (Eastern Standard Time)"
}, {
"name": "comments",
"value": "Comments Text Example"
}];
var doHash = {};
$.each(dataObject, function() {
doHash[this.name] = this.value;
});
$.each(formData.fieldGroupList, function() {
$.each(this.fieldList, function() {
if (this.name in doHash) {
this.value = doHash[this.name];
}
});
});
console.log(JSON.stringify(formData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Categories

Resources