user2906074
user2906074

Reputation: 55

Using search method from string

I'm trying to count the number of times certain words appear in the strings. Every time I run it I get a

uncaught TypeErro: undefined is not a function

I just actually need to count the number of times each "major" appears.

Below is my code:

for(var i = 0; i < sortedarray.length; i++)
    {
        if(sortedarray.search("Multimedia") === true)
        {
            multimedia += 1;
        }
    }
    console.log(multimedia);

Here is my csv file which is stored in a 1d array.

"NAME","MAJOR","CLASS STANDING","ENROLLMENT STATUS"
"Smith, John A","Computer Science","Senior","E"
"Johnson, Brenda B","Computer Science","Senior","E"
"Green, Daisy L","Information Technology","Senior","E"
"Wilson, Don A","Information Technology","Junior","W"
"Brown, Jack J","Multimedia","Senior","E"
"Schultz, Doug A","Network Administration","Junior","E"
"Webber, Justin","Business Administration","Senior","E"
"Alexander, Debbie B","Multimedia","Senior","E"
"St. John, Susan G","Information Technology","Junior","D"
"Finklestein, Harold W","Multimedia","Freshman","E"

Upvotes: 0

Views: 68

Answers (5)

elclanrs
elclanrs

Reputation: 94131

Here's a more straightforward solution. First you count all the words using reduce, then you can access them with dot notation (or bracket notation if you have a string or dynamic value):

var words = ["NAME","MAJOR","CLASS STANDING","ENROLLMENT STATUS"...]

var count = function(xs) {
  return xs.reduce(function(acc, x) {
    // If a word already appeared, increment count by one
    // otherwise initialize count to one
    acc[x] = ++acc[x] || 1
    return acc
  },{}) // an object to accumulate the results
}

var counted = count(words)

// dot notation
counted.Multimedia //=> 3

// bracket notation
counted['Information Technology'] //=> 3

Upvotes: 1

Cerbrus
Cerbrus

Reputation: 72947

What you're probably trying to do is:

for(var i = 0; i < sortedarray.length; i++)
{
    if(sortedarray[i].indexOf("Multimedia") !== -1)
    {
        multimedia++;
    }
}
console.log(multimedia);

I use indexOf since search is a bit of overkill if you're not using regexes.

Also, I replaced the += 1 with ++. It's practically the same.

Upvotes: 2

yogesh
yogesh

Reputation: 574

I don't know exactly that you need this or not. But I think its better to count each word occurrences in single loop like this:

var occurencesOfWords = {};

for(var i = 0; i < sortedarray.length; i++)
{
    var noOfOccurences = (occurencesOfWords[sortedarray[i]]==undefined? 
                          1 : ++occurencesOfWords[sortedarray[i]]);

    occurencesOfWords[sortedarray[i]] = noOfOccurences; 
}
console.log(JSON.stringify(occurencesOfWords));

So you'll get something like this in the end:

{"Multimedia":3,"XYZ":2} 

Upvotes: 0

Rhumborl
Rhumborl

Reputation: 16609

You need to search inside each string not the array. To only search inside the "Major" column, you can start your loop at index 1 and increment by 4 :

var multimedia = 0;
for(var i = 1; i < sortedarray.length; i += 4)
{
    if(sortedarray[i].indexOf("Multimedia") > -1)
    {
        multimedia += 1;
    }
}
console.log(multimedia);

Upvotes: 2

Nicolas Albert
Nicolas Albert

Reputation: 2596

.search is undefined and isn't a function on the array.

But exists on the current string you want to check ! Just select the current string in the array with sortedarray[i].

Fix your code like that:

for(var i = 0; i < sortedarray.length; i++)
{
    if(sortedarray[i].search("Multimedia") === true)
    {
        multimedia += 1;
    }
}
console.log(multimedia);

Upvotes: -1

Related Questions