1

How can I count number of letter "a" into every element of the array?

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];

I try to use split() method, but could not finish ....

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];
let newA = a.toString();

for(i = 0; i < a.length; i++) {
  let newAr = a[i].split(',');

  for(j = 0; j < newAr.length; j++) {
    console.log(newAr[j].split(','));
  }
}

Thank you!

2
  • 1
    "but cannot to finish" What's unfinished? What result do you expect, and what are you getting instead? (And why are you turning your array into a string?) Commented Oct 14, 2018 at 12:04
  • Please don't tag-spam. This has nothing to do with function, or html for instance. Commented Oct 14, 2018 at 12:04

3 Answers 3

2

Array.prototype.map()

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

String.prototype.match()

The match() method retrieves the matches when matching a string against a regular expression.

You can use map() and match() to get the array of count in each string item:

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];

let aCountArr = a.map(i => {
  return i.match(/a/g).length;
});
console.log(aCountArr);

OR: You can use for...of

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];

let aCountArr = [];
for(let c of a){
  aCountArr.push(c.match(/a/g).length)
}
console.log(aCountArr);

Sign up to request clarification or add additional context in comments.

Comments

1

How can I count number of letter "a" into every element of the array?

If the goal is to end up with a new array with the counts of how many as there are in the word in the first array, then map and reduce should do it:

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];
let result = a.map(entry =>
    Array.from(entry).reduce((counter, ch) =>
        ch === "a" ? counter + 1 : counter,
        0
    )
);
console.log(result);

map loops through the original array letting us process each entry and produce a result for it. We turn the string into an array of characters, then use reduce to count how many of those are as.

There are about eighteen other ways. :-) For instance, we could use replace to remove all characters that aren't a and take the length:

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];
let result = a.map(entry =>
    entry.replace(/[^a]/g, "").length
);
console.log(result);

3 Comments

@ЕлисейГорьков - Yes, absolutely. Both of the loops above (map and reduce/replace) could be replaced with for loops.
can you give an exapmle?
@ЕлисейГорьков - I think with just a small amount of research you'll find lots of examples of for loops.
0

Get count for every element in array:

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];
let count = a.map(el => el.match(/a/g).length);
console.log(count);

Get count for entire array as whole:

let a = ['Guatemala', 'Canada', 'Costa Rica', 'Anguilla', 'North Korea'];
let count = a.join('').match(/a/g).length;
console.log(count);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.