0

I need to find the index of an array of array object where it is true. My array:

[
 [
  {id: 1, status: false}, 
  {id: 2, status: true}
 ],
 [
  {id: 13, status: false}, 
  {id: 14, status: false}
 ],
 [
  {id: 18, status: true}, 
  {id: 19, status: false}
 ]
]

In the case above I should expect index 0 and 2 to be returned.

1
  • There's some real ambiguity in your question/example. The answer to this question is [0, 2]: "What are the indices of the arrays in the outer array that have some element in them with status of true?" But it is also the answer to a very different question: " For what indices are there inner arrays where the value at that index has status of true?" Which do you mean? Commented Oct 26, 2020 at 19:56

3 Answers 3

2

Use reduce to get indices of the elements where some items fulfill the condition:

input.reduce((acc, arr, i) => (
    arr.some(obj => obj.status) && acc.push(i)
, acc), [])

const input = [[{"id":1,"status":false},{"id":2,"status":true}],[{"id":13,"status":false},{"id":14,"status":false}],[{"id":18,"status":true},{"id":19,"status":false}]];

const result = input.reduce((acc, arr, i) => (
  arr.some(obj => obj.status) && acc.push(i)
  , acc), []);

console.log(result);

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

Comments

0

There are two steps in the solution of the problem.

  • First traverse the array
  • Second check the condition. You can check the condition using two array methods.
    • some: Checks if any of the elements in an array pass a test and returns true or false based on the test.
    • filter: Creates an array based on condition and that's the reason I checked array length in the following code.

const data = [
  [
    { id: 1, status: false },
    { id: 2, status: true },
  ],
  [
    { id: 13, status: false },
    { id: 14, status: false },
  ],
  [
    { id: 18, status: true },
    { id: 19, status: false },
  ],
];

let ret = [];
data.forEach((x, i) => {
  if (x.some((y) => y.status)) ret.push(i);
});
console.log(ret);

ret = [];
data.forEach((x, i) => {
  if (x.filter((y) => y.status).length > 0) ret.push(i);
});
console.log(ret);

I have used both some and filter methods to check the condition and for array traversese I have used array forEach method.

1 Comment

that's a misuse of filter. some was created to take care of such cases - it clearly states the purpose of the code, using filter here just adds an unnecessary cognitive and performance burden.
0

Basically same solution as from marzelin, but more readable in my opinion. The comma operator in the callback return value can be quite confusing.

const data = [
    [
     {id: 1, status: false}, 
     {id: 2, status: true}
    ],
    [
     {id: 13, status: false}, 
     {id: 14, status: false}
    ],
    [
     {id: 18, status: true}, 
     {id: 19, status: false}
    ]
   ]

//pV=previous value, cV=current value, cI=current index
const result = data.reduce(function (pV, cV, cI) {
    if (cV.some(function (obj) {
            return obj.status
        })) {
        pV.push(cI);
    }
    return pV;
},[]);



console.log(result);

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.