Searching for Instance of Item in Nested Array

Hello, I am attempting to check every level of my nested array for the instance of a specific item (in this case; 11).

So firstly I would like to ask why my code is being thrown out of whack at the ‘if’ conditional.

Secondly, is there a better way to do this without nesting loops?

Regards, :slight_smile:

const arr2 = [[11, 12, 13], [14, 11, 15, 16], [17, 11], [18, 11], [19, 11], [[110, 11], [111, [112, 113, 11], 11], 11], 11];

function isItemOmnipresent(arrayOfArrays, item) {
  for (let i = 0; i < arrayOfArrays.length; i++) {
    for (let j = 0; j < arrayOfArrays[i].length; j++) {
      for (let k = 0; k < arrayOfArrays[i][j].length; i++) {
        if (arrayOfArrays[i][j][k].indexOf(item) == -1) {
          return false;

  return true;

console.log( isItemOmnipresent(arr2, 11) );

k++? :wink: :thinking:

Haha, thank you, but it’s still getting thrown at my ‘if’ conditional with…

TypeError: arrayOfArrays[i][j][k].indexOf is not a function

I’m struggling to think how to go about this. Could you point me in the right direction incase it’s something I’ve never seen before. I have tried google first, but I don’t think the questions raised online match exactly.

Am I supposed to be using recursion here? I’m really faltering on this one.

By the way, thanks for your speedy help today. I start a programming course on Monday and you’ve been a great aid :+1:

I want to check if an item (in my case the number 11) is present within both the outer array and each nested array within it. It should only return false if at least one array does NOT have the specified item.

Okay, so here’s where I’m at. I am trying to use recursion, but failing somewhat.

I am getting a return of ‘true’ even when I remove ‘11’ from the first nested array.

Please help :slight_smile:

const arr2 = [[12, 12, 13], [14, 11, 15, 16], [17, 11], [18, 11], [19, 11], [[110, 11], [111, [112, 113, 11], 11], 11], 11];

function isItemOmnipresent(arrayOfArrays, item) {

  for (let i = 0; i < arrayOfArrays.length; i++) {
    if (!arrayOfArrays.includes(11)) {
      return false;
    } else if (Array.isArray(arrayOfArrays[i])) {

  return true;

console.log( isItemOmnipresent(arr2, 11) );

Recursion needs a base case.
What is yours?

Also, to recurse you must progressively make the problem set smaller. As this is an array of arrays problem set, I think you have to be careful with how the problem gets smaller.

Isn’t my base case that it will exit the loop if ‘11’ is not included in the targeted array?

What if I give you an empty array?

Then it wouldn’t include ‘11’ and therefor the base call would take effect, no?

in the current version of the code, no?
(try it? maybe i’m wrong)

1 Like

I nested an empty array in there and I’m still getting “true” as a return.

I think I’ve stared at this too long now. I just need a solution at this point, if anyone would be so kind.

all I can do from here is suggest ideas for you (that may move you forward, ideally).

So you know that calling the function with [] should give false.
Why don’t you start there.
Then on a piece of paper, try to figure out some psuedocode that you believe will work. You can share the pseudocode for feedback first before coding…

Pppffft. Feeling completely downbeaten on this one. Can’t bare to look at it anymore.

Will just have to give it a miss if I can’t get an answer from anywhere. Hopefully someone can help me out on here with a solution, because it’s not for a lack of trying.

Cheers, though :+1:

We do not share code solutions in the forum, but if you insist you can always search online for similar problems and their solutions.

Mod edit: solution removed

Hi there, please be aware, we don’t share solutions of fCC challenges on here.

oh, i did not know it was a fCC challenge code,
i thought it was just a problem in hi regular code life,

Yes, I see that it may not be an fCC challenge, still I refer you to

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.