Trouble processing nested arrays in n8n JavaScript function

I’m working with n8n and having issues with a JavaScript function node. The problem is about handling nested arrays in the output from an API query to a SaaS service.

Here’s what I’m dealing with:

  • Got a big JSON input with lots of results
  • Each result has some nested arrays
  • My current code works fine for single-level nested arrays
  • But it’s not handling deeper nested arrays correctly

My function looks something like this:

function processResults(items) {
  const processed = [];
  
  for (const item of items) {
    for (const article of item.json.articles) {
      const newItem = { ...article };
      
      if (article.tags) {
        newItem.tags = article.tags.join('\n');
      }
      
      if (article.indicators) {
        newItem.indicators = ''; // This part isn't working right
      }
      
      processed.push(newItem);
    }
  }
  
  return processed;
}

The output is okay for most fields, but the ‘indicators’ field is empty. How can I fix this to properly handle the nested ‘indicators’ array?

I’m not great with JavaScript, so any help would be awesome. Thanks!

I’ve encountered similar issues with nested arrays in n8n. For the ‘indicators’ field, you’re setting it to an empty string, which explains why it’s not showing up in the output. Instead, try processing it like you did with the ‘tags’ field. Here’s a modified version of your function that should handle both ‘tags’ and ‘indicators’:

function processResults(items) {
  const processed = [];
  
  for (const item of items) {
    for (const article of item.json.articles) {
      const newItem = { ...article };
      
      if (Array.isArray(article.tags)) {
        newItem.tags = article.tags.join('\n');
      }
      
      if (Array.isArray(article.indicators)) {
        newItem.indicators = article.indicators.map(i => i.name).join('\n');
      }
      
      processed.push(newItem);
    }
  }
  
  return processed;
}

This assumes ‘indicators’ is an array of objects with a ‘name’ property. Adjust as needed based on your actual data structure. Remember to test thoroughly with your specific dataset.

As someone who’s worked extensively with n8n and JavaScript, I can relate to your struggles with nested arrays. I’ve found that recursion is often the key to handling deeply nested structures effectively. Here’s an approach that’s worked well for me:

function processNestedArrays(obj) {
  if (Array.isArray(obj)) {
    return obj.map(item => processNestedArrays(item)).join('\n');
  } else if (typeof obj === 'object') {
    return Object.entries(obj)
      .map(([key, value]) => `${key}: ${processNestedArrays(value)}`)
      .join('\n');
  }
  return obj;
}

function processResults(items) {
  return items.flatMap(item =>
    item.json.articles.map(article => ({
      ...article,
      tags: processNestedArrays(article.tags),
      indicators: processNestedArrays(article.indicators)
    }))
  );
}

This approach should handle arbitrary levels of nesting in both ‘tags’ and ‘indicators’. It’s more flexible and won’t break if the structure changes. Just be mindful of potential performance impacts with very large datasets.

hey dancingfox, i’ve run into this before. the issue is you’re not actually processing the indicators array. try something like this:

if (article.indicators) {
newItem.indicators = article.indicators.map(i => i.name || i).join(', ');
}

this should handle both objects and simple values in the array. good luck with your n8n workflow!