0

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?

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.