indexOf always returning -1 in JavaScript

Posted about 10 months ago

I have the below setup

const myArr = [
   {name: 'Chris'},   
   {name: 'Archie'}
   {name: 'John'},   
   {name: 'Paul'},   
   {name: 'Daniel'},   
   {name: 'Kyle'}
let items = Object.keys(myArr.slice(0, 4));

let newItem = Math.floor(Math.random() * myArr.length);
let item = items.indexOf(newItem); // returns -1

This is due to Object.keys returning string values and newItem is returned as an int. You need to convert the int “toString()”

let newItem = Math.floor(
                Math.random() * myArr.length