Select items from input via their indeces

Hi. Suppose I want to use data from first three elements for an email text. When I wirte an expression, it looks like this and contains no index:
{{$json["University"]}}

If I take Nodes instead of Current Nodes, it looks like this:
{{$node["Function3"].json["University"]}}

If I try to add an inted like $node["Function3"][0], it gives an error.

Can I operate items data via just addressing them without multiple Function nodes?

Hey @artildo!

Can you share the output of the previous node (the data you’re referencing to)? Please replace any sensitive information with dummy data

So I have such data:

[{
"Number": 508,
"Name": " New York University"
},
{
"Number": 460,
"Name": " Emerson College"
},
{
"Number": 272,
"Name": " Columbia College Chicago"
},
{
"Number": 259,
"Name": " University of Southern California"
},
{
"Number": 242,
"Name": " Chapman University"
}]

Just wanted to know, what’s the better way to send a message (Discord or email), saying: "Top 3 univrecities are " and take top 3 from the array. I found out, that Discord node sends a message for every item. Maybe I have to put a Function node and return the message using pure JS

If their position in the array remains constant then you can use the $items() variable. So to refer the top 3 universities (assuming that are also in the top 3 in the array), I would do something as follows

First: {{$items()[0].json.Name}}
Second: {{$items()[1].json.Name}}
Third: {{$items()[2].json.Name}}

You can read more about the $items() variable here: https://docs.n8n.io/nodes/expressions.html#method-items-nodename-string-outputindex-number-runindex-number

Thank you. In the Expression window this works

However, I have to put the Discord node as executing ONCE. And maybe because of that on execution, only the First value is shown, while second and third are blanc:
image

When the Once setting is off, I get all the data, but executed many times. Here’s definetely something wrong:

Yes, that is expected as it will send one message for each item. You can change that behavior by clicking on the “Settings” tab in the node (right of the “Parameter” tab) and then select “Execute Once”.

@jan Yes, this is the first what I did. But in this case the second, third and so on items via Expressions are not available in the Execution. Only the first item

try like this: $item(0).$node["name of the node that outputting the data"].json["Name"]

3 Likes

Thank you. This works greatly, albeit looks a bit counter-intuitive, with the element number before the array.

1 Like