1

My JSON array looks like this:

    {
  "messages": [
    {
      "msg": "?",
      "name": "johndoe"
    },
    {
      "msg": "hello",
      "name": "johndoe",
    },
    {
      "msg": "twitt",
      "name": "johndoe"
    },
    {
      "msg": "hello everyone!",
      "name": "johndoe"
    },
    {
      "msg": "hello! how are you",
      "name": "johndoe"
    },
    {
      .........

I would like to know how I can check how many times the word "hello" shows up in this whole array, and then just return the number. Thank you for the help.

RTVBasic
  • 35
  • 7
  • Possible duplicate of [How to count string occurrence in string?](https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string) – Harun Yilmaz Sep 19 '19 at 12:28

4 Answers4

2
let helloCount = data.messages.filter(message=>{return message.msg.includes('hello')}).length;

Try above one

Sudhakar
  • 533
  • 1
  • 3
  • 17
1

You can use reduce

const arr = {
  "messages": [{
      "msg": "?",
      "name": "johndoe"
    },
    {
      "msg": "hello",
      "name": "johndoe",
    },
    {
      "msg": "twitt",
      "name": "johndoe"
    },
    {
      "msg": "hello everyone!",
      "name": "johndoe!!"
    },
    {
      "msg": "hello! how are you",
      "name": "johndoe!"
    }
  ]
};

const count = arr.messages.reduce((acc, m) => {
  if (m.name === "johndoe") {
    acc += 1;
  }
  return acc;
}, 0);
console.log(count)

//shorter version
const count2 = arr.messages.reduce((acc, m) => m.name === "johndoe" ? acc += 1 : acc, 0);
console.log(count2)


// EDIT : if you wanna search for "johndoe" string and the name value is "johndoe!!"
const count3 = arr.messages.reduce((acc, m) => {
  if (m.name.includes("johndoe")) {
    acc += 1;
  }
  return acc;
}, 0);
console.log(count3)
Maxime Girou
  • 1,511
  • 2
  • 16
  • 32
  • is there a way to make this so that "m.name" will add 'acc' variable by 1 even if the name is "johndoe 123", "johndoe", and "johndoe!" ? So that it doesn't specifically check for the object that only has the name "johndoe" and there is more flexibility – RTVBasic Sep 19 '19 at 13:06
1

You need the following.

data.forEach(ele => {if(ele.msg.includes('hello')) counter++ });

Here is a demo https://onecompiler.com/javascript/3v2spaetr

karthikdivi
  • 3,466
  • 5
  • 27
  • 46
0

Since it it's a valid JSON format it may as well be treated as a string (before parsing) if that's the case a simple regex would be enough to count the occurrences of particular string inside of it

const input = `{
  "messages": [
    {
      "msg": "?",
      "name": "johndoe"
    },
    {
      "msg": "hello",
      "name": "johndoe",
    },
    {
      "msg": "twitt",
      "name": "johndoe"
    },
    {
      "msg": "hello everyone!",
      "name": "johndoe"
    },
    {
      "msg": "hello! how are you",
      "name": "johndoe"
    }
  ]
}`;

function count(input, match){
  return (input.match(new RegExp(match, "g")) || []).length;
}

console.log("hello: ", count(input, "hello"));
console.log("johndoe: ", count(input, "johndoe"));
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30