Filter JSON per keyword

Hi,
I am trying to apply rule based on a keyword as per this process: , but in separate

Starting from:

[
{
"places": "ITALY, ROME",
"place1": "ITALY, VENICE",
"place2": "ITALY, TURIN",
"place3": "SPAIN, MADRID",
"place4": "SPAIN, VALENCIA",
},
{
"places": "ROME, 10",
"place1": "MADRID, 20",
"place2": "TURIN, 20",
"place3": "LONDON, 10",
"place4": "PARIS, 15",
},
{
"places": "ROME, EUROPE",
"place1": "LONDON, EUROPE",
"place2": "NEW YORK, NORTH-AMERICA",
"place3": "MELBOURNE, OCEANIA",
"place4": "VALENCIA, EUROPE",
}
]

Searching for Keyword: “Rome” and only leaving those lines

Keyword: ROME

[
{
"places": "ITALY, ROME",
},
{
"places": "ROME, 10",
},
{
"places": "ROME, EUROPE",
}
]

Using

const results = {}
const data = items[0].json

for (const key of Object.keys(data)) {
  if (data[key].includes('Rome')) {
    results[key] = data[key]
  }
}

return [ { json: results } ];

Only returns 1st part due to “Item[]”, how to apply to all, was trying with foreach, but this JS is not my strong :confused:. Thank you very much.

Building on top of your code it would look like this:

const results = [];

items.forEach(item => {
  const data = item.json
  for (const key of Object.keys(item.json)) {
    if (data[key].toUpperCase().includes('Rome'.toUpperCase())) {
      results.push({ json: { [key]: data[key] } });
    }
  }
});

return results;

Here the example workflow:

1 Like