# Why function not push others elements from the sub array

**URL:** <https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731>\
**Category:** General\
**Created:** [August 2, 2019, 6:51am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731 "2019-08-02T06:51:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quintis](https://avatars.discourse-cdn.com/v4/letter/q/57b2e6/32.png) [@Quintis](https://forum.freecodecamp.org/u/Quintis)\
**Post date:** [August 2, 2019, 6:51am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/1 "2019-08-02T06:51:19Z")

</div>

Hello ) Why function do not push other elements which equal elem from sub array ?

```auto
  let newArr = [];
  // change code below this line

  for (let i = 0; i < arr.length; i++) {
   if (arr[i].indexOf(elem) == -1){
newArr.push(arr[i]);
  }}

  // change code above this line
  return newArr;
}

// change code here to test different cases:
console.log(filteredArray([[["amy", "beth", "sam"], ["dave", "sean", "peter"] ]], "peter"));

```

Why it is not like this [[“amy”, “beth”, “sam”], [“dave”, “sean”] ] in case above?

---

<div class="post-metadata">

**Author:** ![Hassanraja447](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hassanraja447/32/98591_2.png) [@Hassanraja447](https://forum.freecodecamp.org/u/Hassanraja447)\
**Post date:** [August 2, 2019, 7:16am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/2 "2019-08-02T07:16:04Z")

</div>

Would you clearly mention what do you want to achieve in this challenge.  
Firstly, the code you copied here is missing

```auto
funtion filteredArray(arr, elem) {

```

Secondly, filtered tray has an extra pair of brackets

```auto
/// incorrect
console.log(filteredArray([[["amy", "beth", "sam"], ["dave", "sean", "peter"] ]], "peter"));
/// correct
console.log(filteredArray([["amy", "beth", "sam"], ["dave", "sean", "peter"] ], "peter"));

```

Here is a link to working example of the challenge.  
[pen](https://codepen.io/HassanRaja/pen/EqvLPV?editors=1011)  
Feel free to play with it.

---

<div class="post-metadata">

**Author:** ![Quintis](https://avatars.discourse-cdn.com/v4/letter/q/57b2e6/32.png) [@Quintis](https://forum.freecodecamp.org/u/Quintis)\
**Post date:** [August 2, 2019, 7:20am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/3 "2019-08-02T07:20:27Z")

</div>

Hello ) I want to know why function above do not push all sub array which have the element ? Whyconsole.log is not like this [[“amy”, “beth”, “sam”], [“dave”, “sean”] ] in case above?

---

<div class="post-metadata">

**Author:** ![Hassanraja447](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hassanraja447/32/98591_2.png) [@Hassanraja447](https://forum.freecodecamp.org/u/Hassanraja447)\
**Post date:** [August 2, 2019, 7:29am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/4 "2019-08-02T07:29:49Z")

</div>

The straightforward answer is, it is because of the logic put in there.

```auto
if (arr[i].indexOf(elem) === -1){
newArr.push(arr[i]);
  }}

```

You are pushing array items which don’t have elem in them, if you want to have array of those item which does have elem in the you have to switch the condition like this.

```auto
if (arr[i].indexOf(elem) !== -1){
newArr.push(arr[i]);
  }}

```

That’s it

---

<div class="post-metadata">

**Author:** ![Hassanraja447](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hassanraja447/32/98591_2.png) [@Hassanraja447](https://forum.freecodecamp.org/u/Hassanraja447)\
**Post date:** [August 2, 2019, 7:31am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/5 "2019-08-02T07:31:27Z")

</div>

You can read more about indexOf method [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [August 2, 2019, 7:55am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/6 "2019-08-02T07:55:37Z")

</div>

Remember, you are _not_ pushing the elements from the sub arrays into the `newArr`, you are pushing the actual sub arrays. Which means you are excluding any sub array (not sub array elements) that contains the string passed to the `elem` parameter.

It is [indexOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) that is looping the sub arrays for you (like if you were using a nested loop). If it finds the element it will return the element index, if it does not find the element it will return `-1`.

Here is [the challenge](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops/) in question, just to be clear for anyone reading this.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 9:33am UTC](https://forum.freecodecamp.org/t/why-function-not-push-others-elements-from-the-sub-array/300731/7 "2021-01-17T09:33:17Z")

</div>

Closed on January 17, 2021, 9:33am UTC


