6Ortiz
6Ortiz

Reputation: 39

forEach loop to pull element from array & push into new array

I am trying to have my forEach loop loop through var fruit and push any found strings into arr2.

Here is my code:

var fruit = [1,2,3, "apple", "banana", "grape"] 

function isString(fruit) {
var arr2 = []
fruit.forEach(function(element) {
if(element == "string"){
arr2.push(element)
}
return arr2
})

}

//desired output is arr2 = "apple", "banana", "grape"

I'm not sure what I'm doing wrong here. Thanks for any advice.

Upvotes: 0

Views: 24526

Answers (5)

P215W
P215W

Reputation: 11

Javascript code, that will work:

var fruit = [1,2,3, "apple", "banana", "grape", true, false]; 

function isString(fruit) {
    var arr2 = [];
    fruit.forEach(function(element) {
        if (isNaN(element)) {   // only pushes the strings, not numbers or booleans
            arr2.push(element);
        }
        console.log(arr2);  // output as wished: apple, banana, grape
    })
}
isString(fruit);

Upvotes: 1

Olivier Boissé
Olivier Boissé

Reputation: 18143

You should use typeof and returns arr2 at the end of the function isString

var fruit = [1,2,3, "apple", "banana", "grape"] 

function isString(fruit) {
  var arr2 = [];
  fruit.forEach(function(element) {
    if(typeof element === "string"){
      arr2.push(element);
    }
  })
  
  return arr2;
}

console.log(isString(fruit))

By the way you could use filter function to make it shorter

var fruit = [1,2,3, "apple", "banana", "grape"];

var fruitString = fruit.filter(function(element) {
   return typeof element === "string";
});

console.log(fruitString);

Upvotes: 1

Alin.B
Alin.B

Reputation: 57

I use this foreach sequence in one of my code...maybe will help you.

string[] Body;

string first = Body.First(); // get content of first index of "Body" array

    if (first != "")                // if first index of "Body" array contain data's
    {
      string  Content = "";

        foreach (string line in Content)        // do something
        {
            Content = Content + line + "\n";
        }
    }

Upvotes: 2

Eres
Eres

Reputation: 1729

Two issues:

  1. You are putting the return within the loop. So it return before even the loop completes. That means the first element that is not a string, and it returns and you get an empty array.

  2. element == "string" is not a correct way to check type. You can use typeof element === "string"

There is a third possible issue and that is you actually have to call the function. Find below working code:

var fruit = [1, 2, 3, "apple", "banana", "grape"]

function isString(fruit) {
    var arr2 = []
    fruit.forEach(function(element) {
        if (typeof element === "string") {
            arr2.push(element)
        }

    })
    return arr2;

}

isString(fruit);

Upvotes: 0

Ahmed Hammad
Ahmed Hammad

Reputation: 3095

Replace:

  element == "string"

With:

  typeof element == "string"

Because what you're basically doing is comparing the actual data not the type.

Upvotes: 0

Related Questions