skip to Main Content

I have an array of strings. I want to loop through this array and create a new array that houses each string and the frequency it appears. So, if the string appears more than once the frequency value in the new array gets updated.

From an array that looks like:

let strArr = ["Apple", "Orange", "Banana", "Apple", "Pear", "Banana"];

The desired result would be:

termsArr = [
  {term: "Apple", frequency: 2},
  {term: "Orange", frequency: 1},
  {term: "Banana", frequency: 2},
  {term: "Pear", frequency: 1}
]

I am trying to use findIndex() to check if the termsArr has a matching term in it already (and if yes update the frequency value), but it is failing saying term is not defined. I don’t know why this is.

My full code:

let strArr = ["Apple", "Orange", "Banana", "Apple", "Pear", "Banana"];
let termsArr = [];

for (let i = 0; i < strArr.length; i++) {
  let arrItem = strArr[i];
  let objIndex = termsArr.findIndex((obj => obj.term == arrItem));

  if (objIndex > 0) {
    termsArr[objIndex].frequency = termsArr[objIndex].frequency + 1;
  }
  else {
    termsArr[i] = {
      term: arrItem,
      frequency: 1
    };
  }
}

Would anyone know what I’m doing wrong here?

I thought it might be failing initially because the termsArr is empty, so modified it as such:

  let objIndex = 0;
  if (i < 1) {
    objIndex = termsArr.findIndex((obj => obj.term == arrItem));
  }

But that did not make any difference.

3

Answers


  1. You should use strArr instead of textArr to assign the value for arrItem.

    Also change objIndex > 0 to objIndex !== -1 to correctly check if the term is not found in the termsArr.

    I will also suggest you to use === for the comparison in obj => obj.term === arrItem for strict equality.

    let strArr = ["Apple", "Orange", "Banana", "Apple", "Pear", "Banana"];
    let termsArr = [];
    
    for (let i = 0; i < strArr.length; i++) {
      //change here
      let arrItem = strArr[i];
      let objIndex = termsArr.findIndex(obj => obj.term === arrItem);
    
      if (objIndex !== -1) {
        //if the term is already in the termsArr, update the frequency
        termsArr[objIndex].frequency = termsArr[objIndex].frequency + 1;
      } else {
        //if the term is not in the termsArr, add a new object
        termsArr.push({
          term: arrItem,
          frequency: 1
        });
      }
    }
    
    console.log(termsArr);
    Login or Signup to reply.
  2. You should check for objIndex >= 0 instead of objIndex > 0 to include the case when the element is found at the first index.

    let strArr = ["Apple", "Orange", "Banana", "Apple", "Pear", "Banana"];
    let termsArr = [];
    
    for (let i = 0; i < strArr.length; i++) {
        let arrItem = strArr[i];
        let objIndex = termsArr.findIndex((obj => obj.term === arrItem));
    
        if (objIndex >= 0) {
            termsArr[objIndex].frequency++;
        } 
        else {
            termsArr.push({
                term: arrItem,
                frequency: 1
            });
        }
    }
    
    console.log(termsArr);
    Login or Signup to reply.
  3. Your let strArr = should be textArr = and that will solve it. You are referencing an undefined textArr variable

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search