[SOLVED] Code Step Is Making All Array Values Match for each Item when Each Should be Unique to that item

Describe the issue/error/question

I am comparing data from a set of reference fields to those that actual users have. If the user has the data, the value is updated and I then ‘push’ the values into a new item in my json object. I have found that for some reason all users get the value of whatever the last user is that has been added.

I have done the following:

  • Made sure the temp object that I push to the ‘main’ object is cleared on each iteration of the loop
  • Checked the values of all objects at each step to make sure they are correct (via console)
  • Tried both ‘push’ and ‘concat’

It seems the problem lies in the push/concat step.

Here is an example of the output data I get. The “value” for the “customFields” on “billy bob” should be blank, but it assuming the value from “amy jones”.

{
“name”:“billy bob”,
“id”:“1”,
“customFields”:{
“name”:“SMART Friends Referred”,
“id”:“DyJJSgLHWY8F8AEgLk4M”,
“value”:“Dante Inferno, Tom Twoteeth”
}
},
{
“name”:“amy jones”,
“id”:“2”,
“customFields”:{
“name”:“SMART Friends Referred”,
“id”:“DyJJSgLHWY8F8AEgLk4M”,
“value”:“Dante Inferno, Tom Twoteeth”
}
}

Please share the workflow

Information on your n8n setup

  • n8n version: Version 0.209.4
  • Database you’re using (default: SQLite): Default
  • Running n8n with the execution process [own(default), main]: default
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Docker

Hey @mbowler,

It looks like something went wrong the json you pasted for your workflow, Can you edit your message and paste the workflow json between 2 sets of ``` this will then show the workflow preview and will allow us to see what is going on.

Just fixed it. After doing some research, I think the issue is with how I’m making my temp object copies. I think I need to be doing something like newArr = […origArr], but this is new to me.

1 Like

I meant to say, I fixed the code above, I didn’t fix the original problem.

Got it solved. This was a new one for me.

The issue was that I was doing a shallow copy of my objects and arrays for temp use in loops. This was in turn causing the values to pass through to the original each time I wrote to the temp objects, thus resulting in the value of the last contact being used for everyone.

The solution was liberal application of newObj = JSON.parse(JSON.stringify(origObj))

Glad you worked it out, I was looking at the code thinking there was a lot going on that might not need to be there :slight_smile:

lol. yeah, I have only been learning for like 5 months so it’s very utilitarian. Never efficient or clean.

1 Like

I am a big fan of just get it done and improve it later :slight_smile: here is a little tweet I posted a while back about a workflow I have that I improved that sort of shows how messy I can be with a first version: https://twitter.com/JoffCom/status/1601554022615642113

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.