]
I want to count how many times does 3 words “sell”, “buy”, “neutral” but I dont have any JS knowledge and the “Summarize” node doesnt support my request.
Thanks a lot! It worked flawlessly. By the way can you explain how the code works? I’m learning Javascript now so I kinda want to know how the code works.
First imported the data from the previous steps and assigned it to data : const data = $input.all();
Create Object
He created an object named wordCounts that holds the values buy, neutral, and sell : const wordCounts = { buy: 0, neutral: 0, sell: 0, };
Loop over data
Then he looped over the data and incremented each variable in wordCounts whenever the word appeared.
Object.values(data).forEach((item) => is running the function in the following curly braces for each item in the json. So this code block runs for each piece of data.
const text = item?.json?.test.toLowerCase(); Saves the spot in the json we expect to find buy, sell, or neutral to the variable text. This is just so it’s easier to work with.
If statements
Then we just run if statements to increment the count in the variables if the data matches. For example:
if (text.includes("buy")) { wordCounts.buy++; }
if (text.includes("buy")) runs the code in the following {} if it’s true.
Syntax Explanation
wordCounts.buy++; adds one to the variable.
wordCounts.buy is just a way to access the variable that’s inside of the wordCounts object.
++ is just shorthand for adding one. wordCounts.buy++; is the same thing as wordCounts.buy = wordCounts.buy + 1;
Get the data back to the node
Then return { wordCounts }; simply returns to object wordCounts back to the result of the node
Feel free to ask any follow up questions. I am also a self taught coder, keep at it