[code node] Split out items

Describe the problem/error/question

I want to split out items depending on key names matching values from the array ‘DateProperties’.

So if a key with a matching name occurs in the item, a ‘copy’ (with modifications) will be pushed to results.

My issue is that every item of ‘results’ is a copy of the last item that is pushed.

What is the error message (if any)?

No error message, this is a pure JavaScript object referencing issue, that I can’t wrap my head around.

Please share your workflow

//
let result = [];

for (const item of $input.all()) {
  
  for (const date of item.json.DateProperties) {
    //check if null or empty
    if (item.json[date] != null && item.json[date] != '') {
      item.json.MasterCalendarDate = item.json[date]
      item.json.hasDate = true;
      item.json.tag = date;
      
      result.push(item);
      //console.log(result);
    }
  }
}

return result;

My current fix is below, but is there a way I could have made my previous attempt work?

let results = [];
let items = $input.all();

for(let i=0; i<items.length; i++){
  
  for (const date of dates = items[i].json.DateProperties) {
    
    if (items[i].json[date] != null && items[i].json[date] != '') {

        results.push(
          {
            "json":
              {
                "MasterCalendarDate": items[i].json[date],
                "hasDate": true,
                "tag": date
              },
            "pairedItem": i    
          }    
        )
    }
  }
  
}

return results;

Share the output returned by the last node

Information on your n8n setup

  • n8n version:current
  • Database (default: SQLite):default
  • n8n EXECUTIONS_PROCESS setting (default: own, main):default
  • Running n8n via (Docker, npm, n8n cloud, desktop app):docker
  • Operating system:Ubuntu Server

Can you please share some sample input code?
Also the desired output as well.

I’ll take a look once you send that

Sorry for ghosting this thread. I got completely sidetracked, and don’t know when I’ll be able to look into this exact issue.