How to search for a specific ID in a nested object structure (API response handling)

I’m working with an API that returns data in a nested object format. Here’s what the structure looks like:

const apiResponse = {
  // other properties
  details: {
    item1: { uniqueCode: 'abc123', content: 'some data' },
    item2: { uniqueCode: 'def456', content: 'more data' },
    item3: { uniqueCode: 'ghi789', content: 'extra data' }
  }
};

I need to find an item based on its uniqueCode. If it exists, I want to get its content. If not, I should get null.

This is part of a bigger task where I’m processing API data to fill a spreadsheet. Some cells might be empty, so they’re not in the API response. I have a list of column codes, and for each one, I need to check if there’s a matching item in the response. If yes, I’ll use the content. If not, I’ll leave the cell blank.

Any ideas on how to search through this object efficiently? I’m not sure about the best approach here.

I’ve dealt with similar nested API responses before, and here’s an approach that worked well for me:

Use Object.values() to get an array of all the items in the details object, then use find() to search for the item with the matching uniqueCode. Here’s a function that does this:

function findItemByCode(apiResponse, code) {
  const item = Object.values(apiResponse.details).find(item => item.uniqueCode === code);
  return item ? item.content : null;
}

You can then use this function in a loop over your column codes:

columnCodes.forEach(code => {
  const content = findItemByCode(apiResponse, code);
  if (content) {
    // Fill the cell with content
  } else {
    // Leave the cell blank
  }
});

This approach is efficient and keeps your code clean. It’s worked well for me in similar situations, hope it helps!

hey, have u tried using a recursive function? it could work well for nested structures. something like:

function findCode(obj, code) {
  if (obj.uniqueCode === code) return obj.content;
  for (let key in obj) {
    if (typeof obj[key] === 'object') {
      let result = findCode(obj[key], code);
      if (result) return result;
    }
  }
  return null;
}

this should handle any level of nesting. just call it with ur api response and the code ur looking for.

Having worked extensively with nested API responses, I can suggest an alternative approach that might be more efficient, especially if you’re dealing with a large dataset. Instead of searching through the object each time, you could create a map of uniqueCodes to content once, then use it for lookups:

const codeMap = Object.values(apiResponse.details).reduce((map, item) => {
  map[item.uniqueCode] = item.content;
  return map;
}, {});

function getContentByCode(code) {
  return codeMap[code] || null;
}

This way, you’re only iterating through the details object once, making subsequent lookups O(1) operations. It’s especially useful when processing multiple columns in your spreadsheet. Integrating getContentByCode() into your workflow can improve efficiency and simplify your code.