How to transform an array list to a single one object using Function?

I have an array of elements but I need to get only the first and the second element, and merge them into a single object.

I try this :

var newItems = [];var nextTournament = {};

nextTournament.tournamentId1=items[0].id;
nextTournament.tournamentId2=items[1].id;

nextTournament.tournamentName1=items[0].name;
nextTournament.tournamentName2=items[1].name;

newItems.push(nextTournament);return newItems;

I have this error : ERROR: All returned items have to contain a property named “json”!

Any idea ?

Thx all !

Hey @MuMU,

All the items in the top-level array are wrapped within a JSON object. As the error message says, you need to create a JSON object within your object.

The following code does that for you:

var newItems = [];
var nextTournament = {};

nextTournament.tournamentId1=items[0].json.id;
nextTournament.tournamentId2=items[1].json.id;

nextTournament.tournamentName1=items[0].json.name;
nextTournament.tournamentName2=items[1].json.name;

newItems.push({json:nextTournament});
return newItems;

Note that to reference the value from the previous node I am again using the json keyword. So instead of using items[0].id, you have to use items[0].json.id.

I hope this helps :slight_smile:

1 Like