Replace multiple variables values with Function Node

I’m getting success replacing one single variable value with this code.

let url1 = $node["WooCommerce Trigger"].json["meta_data"][1]["value"];
items[0].json.url1 = url1.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];
return items;

But how can I do that for url2 and url3?

and How to handle it in case ["meta_data"][2]["value"] or ["meta_data"][3]["value"] are not present?

This is actually less of an n8n related question, more a JavaScript-related one.

Anyway the solution is a loop:

let index = 0;
for (const data of $node["WooCommerce Trigger"].json["meta_data"]) {
  items[0].json[`url${index++}`] = data.value.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];
}
return items;

Not sure if you skip on purpose the first value in “meta_data”, in my example I do not and iterate over all of them and so also start with url0.

Here the nodes:

2 Likes