Matching up data in arrays and iterations

I have another question, sorry to be such a pain, I tried a few hours and now I’m frustrated.

I have a node that outputs the following


There are many arrays that come back each with a number of keys and results for each.

This feeds into the next node that iterates over the “objects” array to spit out a list.

const results = []
for (const item of items) {
  results.push.apply(results, item.json.objects)
}

return results.map(element => {
  const json = {};
  for (const key of Object.keys(element)) {
    json[key] = typeof element[key] === 'object' ? JSON.stringify(element[key]) : element[key];
  }
  return { json };
})

and this all all fine,

Except I am missing the Name and Website that links to each result from the previous node. how do I get them in a column along side each of these results in the array.

Some arrays as you can see have no results, but still have a name, I can deal with that later with an IF rule.

If your input is:

[
  {
    "objects": [
      { a: 1 },
      { b: 2 },
      { c: 3 }
    ],
    "name": "john",
    "website": "john.com"
  },
  {
    "objects": [
      { a: 1 },
      { b: 2 },
      { c: 3 }
    ],
    "name": "mary",
    "website": "mary.com"
  }
]

And if your target output is:

[
  { a: 1, name: "john", website: "john.com" },
  { b: 2, name: "john", website: "john.com" },
  { c: 3, name: "john", website: "john.com" }
  { a: 1, name: "mary", website: "mary.com" },
  { b: 2, name: "mary", website: "mary.com" },
  { c: 3, name: "mary", website: "mary.com" }
]

Then:

return [].concat(
	...items.map(i => i.json.objects.map(o => {
		return { json: { ...o, name: i.json.name, website: i.json.website } };
	}))
);
3 Likes