1

JavaScript findIndex returns the very first finded index in case of duplicate values

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const index = arr.findIndex(m => m.a === 10)
console.log(index);

The above code will only return 0 index.

What should I do to get index 2 as well.

0

8 Answers 8

2

You could filter the keys of the array like this:

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const indices = [...arr.keys()].filter(i => arr[i].a === 10)
console.log(indices)

Or, just use a for loop

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const output = [];

for (let i = 0; i < arr.length; i++) {
  if (arr[i].a === 10)
    output.push(i)
}

console.log(output)

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

Comments

0
arr.reduce((result, current, index) => {
   return result.concat(current.a == 10 ? [index]: [])
})

1 Comment

Welcome to Stack Overflow! Please read what this site is about and "How to answer" before answering a question. Please add an explanation to your answer.
0

You can use of Array.reduce to loop on your arr and returns every indexes

Single line :

const arr = [{
  a: 10,
  b: 20,
  c: 30,
}, {
  a: 15,
  b: 25,
  c: 32,
}, {
  a: 10,
  b: 23,
  c: 350,
}]

// Value to check for
const v = 10;

// we loop on every entry of the arr
// we start with an empty array and look if the entries
// contains the value 10, if they do, we push the index of the
// entry in the array that was empty
//
// we do that for every entry of arr, when we treat all, we return
// the list of matching entries
const idx = arr.reduce((tmp, x, xi) => (x.a === v ? [...tmp, xi] : tmp), []);

console.log(idx);


Explained :

const arr = [{
  a: 10,
  b: 20,
  c: 30,
}, {
  a: 15,
  b: 25,
  c: 32,
}, {
  a: 10,
  b: 23,
  c: 350,
}]

const valToCheck = 10;

const indexes = arr.reduce((tmp, x, xi) => {
  if (x.a === valToCheck) {
    tmp.push(xi);
  }

  return tmp;
}, []);

console.log(indexes);

Comments

0

Use map and filter:

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}];
const res = arr.map(({ a }, i) => a == 10 ? i : "").filter(String);
console.log(res);

Comments

0

You could map the indices of the found items and filter only valid indices.

const
    array = [{ a: 10, b: 20, c: 30 }, { a: 15, b: 25, c: 32 }, { a: 10, b: 23, c: 350 }],
    indices = array
        .map((m, i) => m.a === 10 ? i : -1)
        .filter(i => i != -1);

console.log(indices);

Comments

0

You can just iterate through array and push matching index in new array.

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
let p = []
arr.forEach((ele,index)=> ele.a===10 ? p.push(index) : '')
console.log(p)

Comments

0

You can try like this:

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}];

const indices = arr.map((x, index) => {
	if (x.a === 10) {
		return index;
	}
})
.filter(x => x !== undefined);

console.log(indices);

Comments

0

If you want a function which works directly with Array, add this in Array.prototype

Array.prototype.findAllIndexes = function(iteratee){
    var resultArray = [];
    this.forEach(function(element) {
        var validated = iteratee.call(this, element);
        if(validated) {
            resultArray.push(element);
        }
    });
    return resultArray;
}

Then you can use it anywher you want with array object

(please handle your corner cases based od data type you use)

const arr = [{a: 10, b: 20, c: 30},{a: 15, b: 25, c: 32},{a: 10, b: 23, c: 350}]
const index = arr.findAllIndexes(m => m.a === 10)
console.log(index);

You can use some helper libraries to this kind of stuff like lodash.js, you can include them in your project.

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.