1

I'm struggling to find the index of the array that has id === 3.

const id = 3;
const bigList = [
  [
    { id: "1", foo: "a" },
    { id: "2", foo: "b" }
  ],
  [
    { id: "3", foo: "c" },
    { id: "4", foo: "d" }
  ],
  [
    { id: "5", foo: "e" },
    { id: "6", foo: "f" }
  ]
];

I tried this to filter out that array.

const filteredList = bigList.filter(list => list.filter(item => item.id === id))

But it was wrong.

I really appreciate if you show me how it works. Thanks!

1
  • You want just one index or all the matched elements in the list? Commented Jan 25, 2021 at 5:05

6 Answers 6

2

You're really close.

One way to do it is this,

bigList.filter(list => list.filter(item => item.id === "3").length !== 0);

If you look at the documentation of filter, you'll see that it returns a new array. So, the result of your inner filter would yield an array that needs to be evaluated to a true or false condition.

One way to do that is to check the length of the array.

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

Comments

1

You can use findIndex with a some(). Also notice that id variable is number but property of the object is a string

const id = 3;
const bigList = [
  [
    { id: "1", foo: "a" },
    { id: "2", foo: "b" }
  ],
  [
    { id: "3", foo: "c" },
    { id: "4", foo: "d" }
  ],
  [
    { id: "5", foo: "e" },
    { id: "6", foo: "f" }
  ]
];

const res = bigList.findIndex(x => x.some(y => y.id === id.toString()));
console.log(res)

Comments

1

You could use some to check if the list has the element with id of 3, and remember to cast the id to string before compare

const id = 3;
const bigList = [
  [
    { id: "1", foo: "a" },
    { id: "2", foo: "b" },
  ],
  [
    { id: "3", foo: "c" },
    { id: "4", foo: "d" },
  ],
  [
    { id: "5", foo: "e" },
    { id: "6", foo: "f" },
  ],
];

const filteredList = bigList.filter((list) =>
  list.some((item) => Number(item.id) === id)
);

console.log(filteredList);

or you could use Abstract Equality Comparison (==) to compare, then item.id will be coerced to number

const id = 3;
const bigList = [
  [
    { id: "1", foo: "a" },
    { id: "2", foo: "b" },
  ],
  [
    { id: "3", foo: "c" },
    { id: "4", foo: "d" },
  ],
  [
    { id: "5", foo: "e" },
    { id: "6", foo: "f" },
  ],
];

const filteredList = bigList.filter((list) =>
  list.some((item) => item.id == id)
);

console.log(filteredList);

Comments

1

Use the filter but only once so that inside it you can control the return value, like this:

console.log(bigList.filter((el) => {
    var returnVal = false;
    for (var j = 0; j < el.length; j++) {
        if (el[j].id == 3) {
            returnVal = true;
            break;
        }
        else { returnVal = false; }
    }
    return returnVal;
}));

Comments

1

You can flatten your array using array#flat and then filter it based on searchId.

const searchId = 3,
      bigList = [ [ { id: "1", foo: "a" }, { id: "2", foo: "b" } ], [ { id: "3", foo: "c" }, { id: "4", foo: "d" } ], [ { id: "5", foo: "e" }, { id: "6", foo: "f" } ] ],
      result = bigList
          .flat()
          .filter(({id }) => id === String(searchId));
console.log(result);

Comments

1

You can use findIndex method to find the index of the array containing id = 3.

bigList.findIndex((item) => (item.findIndex(el => el.id === id.toString())) > -1)

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.