Unable to return the index of an array

Hi Guys,

Please I need your help concerning an issue I am facing.

I have this array

var array = [{"id":150,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_dzac1n75xphq_1469965658.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-07-31 11:47:38","updated_at":"2016-07-31 11:47:38"},{"id":164,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_rgxyqs4jck6v_1480995716.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-12-06 03:41:57","updated_at":"2016-12-06 03:41:57"},{"id":165,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_m3na1v4bcrgp_1480995742.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-12-06 03:42:22","updated_at":"2016-12-06 03:42:22"}]

And I want to return the index of an object in it

var arrObject = {"id":165,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_m3na1v4bcrgp_1480995742.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-12-06 03:42:22","updated_at":"2016-12-06 03:42:22"}

I have tried

var index = array.indexOf(arrObject);

But it returns -1. Your help will be so appreciated.

Thanks

Hi there Megafu_Anthony,

please note that I am not by any means a javascript
guru, but I am rather good at searching. :sunglasses:

This is the solution that I found…

<script>
(function() {
   'use strict';
   var array = 
      [
       {"id":150,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_dzac1n75xphq_1469965658.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-07-31 11:47:38","updated_at":"2016-07-31 11:47:38"},
       {"id":164,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_rgxyqs4jck6v_1480995716.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-12-06 03:41:57","updated_at":"2016-12-06 03:41:57"},
       {"id":165,"post_id":247,"user_id":8,"file_path":"uploads/posts/8/post_fle_m3na1v4bcrgp_1480995742.jpg","mime":"","type":"image","link":"","public_id":"","created_at":"2016-12-06 03:42:22","updated_at":"2016-12-06 03:42:22"}
      ];
 
   var index = array.map(function(x) {return x.id; }).indexOf(165);
   console.log(index);
}());
</script>

coothead

1 Like

With ES6 you can use the .findIndex() method, like

const index = array.findIndex(el => el.id === arrObject.id)

BTW the reason why your code doesn’t work is that you can’t compare object literals for (even non-strict) equality as you can with primitive types – only object references. Compare

const fooObj = {foo: 1}
const barObj = {bar: 2}
const arr = [fooObj, barObj]
console.log(arr.indexOf(barObj))   // 1
console.log(arr.indexOf({bar: 2})) // -1

So if you really need to deep-check 2 objects against each other, you’ll either have to do so manually by iterating over their properties, or by “normalising” them using JSON.stringify(), like

const index = array.findIndex(el => JSON.stringify(el) === JSON.stringify(arrObject))

PS: This would also work particularly well with @coothead’s mapping idea, like

var index = array
  .map(JSON.stringify)
  .indexOf(JSON.stringify(arrObject));
2 Likes

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