0

I have an array of strings

var arr = ['welcome', 'test', 'the', 'nothing'];

Now I want to find all strings that have 4 or more letters. I use the following code but it only returns one string.

  for ( var i = 0; i < arr.length; i++) {
  if (arr[i].length >= 4) {
     return arr[i];
  }
}

How can I return all strings with 4 or more letters? Thanks in advance.

lejhbah
  • 1,061
  • 4
  • 16
  • 30
  • 3
    This is precisely what [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) is for. The first example is nearly exactly what you want (7 letters instead of 4). – vox Nov 27 '17 at 22:01
  • Please try searching before asking – charlietfl Nov 27 '17 at 22:06

4 Answers4

4

Accumulate all matched objects into a array and return the array :

  let filteredArray =[];
  for ( var i = 0; i < arr.length; i++) {
  if (arr[i].length > 3) {
     filteredArray.push(arr[i]);
  }

Or use a function : let filteredArray = arr.filter(v => v.length > 3)

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

You can just use Array.filter()

const filterByLength = (arr, requiredLength) => {
  return arr.filter(v => v.length > requiredLength)
}

console.log(filterByLength(arr, 3)) // ['welcome', 'test', 'nothing']
peteb
  • 18,552
  • 9
  • 50
  • 62
  • 2
    `arr.filter(v => v.length > 3)` doesn't change `arr`. You don't assign the function result to a variable. So it is useless. – davidxxx Nov 27 '17 at 22:06
  • 1
    This code is still not right. You still don't assign`filterByLength(arr, 3)` to a variable. This would be better like that for example : `const newArr = filterByLength(arr, 3)` – davidxxx Nov 27 '17 at 22:18
  • @davidxxx I'm aware that `filter()` returns a new Array instance and doesn't perform an inline mutation of the array `filter()` was called on. Since the question asked doesn't demonstrate the usage of the result, I opted to not assign it to some arbitrary variable and instead left it up to the asker to utilize the result however they saw fit. I updated my answer with a `console.log()` around the filtered array to better illustrate their is a new Array returned, per you commentary. – peteb Nov 27 '17 at 22:35
0

This is because when you return you are exiting the loop after it finds only one match.

Try this:

let arr = ["this", "is", "longer", "than", "4"];
for ( var i = 0; i < arr.length; i++) {
  if (arr[i].length >= 4) {
     console.log(arr[i]);
  }
}

Alternatively you could add it to a different array and use that

    let arr = ["this", "is", "longer", "than", "4"],
    otherarr = [];
    for ( var i = 0; i < arr.length; i++) {
      if (arr[i].length >= 4) {
         otherarr.push(arr[i]);
      }
    }
    console.log(otherarr);

Or you could simplify by using the Array.filter method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

let arr = ["this", "is", "longer", "than", "4"];
console.log(
  arr.filter(function(array_element) {
    return array_element.length >= 4;
  })
);
zfrisch
  • 8,474
  • 1
  • 22
  • 34
-1

You should use Javascript's Array.filter() method.

arr.filter(string => string.length > 4)

Malisbad
  • 189
  • 1
  • 7