IndexOf () when array elements are objects (javascript)

For example, a variable named arrayElements

array types contain:
[{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}]

.

How do I get the position of an array element using id === 3

(3rd element) in a variable arrayElements

other than using a loop?

thank.

+3


source to share


6 answers


You need to loop at one point. But you can abstract it to look like you are not looping

function indexOfCallback(arr, callback, startIndex) {
    if (typeof startIndex == 'undefined') {
        startIndex = 0;
    }
    for(var i=startIndex; i < arr.length; i ++) {
        if (callback(arr[i])) {
            return i;
        }
    }
    return -1;
}

var array = [{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}];
// Search on id === 3
console.log(indexOfCallback(array, function(obj){
    return obj.id === 3;
}));
// Search on value === 6
console.log(indexOfCallback(array, function(obj){
    return obj.value === 6;
}));
      

Run codeHide result




As Anthony mentioned, this is proposed for ECMAScript 6. Here's a more complete polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

if (!Array.prototype.findIndex) {
  Array.prototype.findIndex = function(predicate) {
    if (this == null) {
      throw new TypeError('Array.prototype.find called on null or undefined');
    }
    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }
    var list = Object(this);
    var length = list.length >>> 0;
    var thisArg = arguments[1];
    var value;

    for (var i = 0; i < length; i++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        return i;
      }
    }
    return -1;
  };
}
console.log(array.findIndex(function(obj){
    return obj.id === 3;
}));

      

+4


source


arrayElements.map(o => o.id).indexOf(3);

      

Notes:



  • Possibly slower than loop because it transforms the entire array before searching. But with high level languages ​​like Javascript, you never know.
  • Infinitely more readable than a loop.
  • IE compatible (as opposed to findIndex from 2017).
+1


source


I wrote a function for you that you can use to get the job done, but uses a loop:

var yourObjArray = [{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}];


function objArrayIndex(objArray){
  for(var i = 0; i < objArray.length; i++){
    if(objArray[i]['id'] == 3){
      return i;
    }
  }
  return -1;
}

console.log(objArrayIndex(yourObjArray));

      

0


source


In such an array, you cannot get access items by id. Therefore, using a loop is the best solution you have. However, depending on your use case, you might also consider using an object instead of an array for direct access.

var container = { 1: {id:1, value:5}, 2: {id:2, value:6}, 3: {id:3, value:7} }

      

0


source


You can use Array.forEach I think

var fooArray = [{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}];
var foundIndex = -1;
fooArray.forEach(function(el, index){
    if(el.id==3) {
       foundIndex=index;
       return false;
     };
});

//foundIndex = 2

      

0


source


You can use an array filter, but I think you will get a better solution using a loop.

var array = [{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}];

var result = array.filter(condition);

function condition(value, index){
    if (value.id === 3) return index;
}

console.log(result);

      

0


source







All Articles