You are looking to see if the value of objects property is equal to the id. 31652237148248 is never going to equal 12
so you can just do a type of
var index = cartItems.findIndex(obj => typeof obj[id] !== undefined);
you can do a truthy check - will fail if it is a falsey value.
var index = cartItems.findIndex(obj => obj[id]);
you can use object keys and includes or if first is equal
var index = cartItems.findIndex(obj => Object.keys(obj).includes(id));
var index = cartItems.findIndex(obj => Object.keys(obj)[0] === id);
A bunch of ways to do it
Personally a better way is just to use an object and not an array for the data.
var items = {31652237148248: 12, 4365124714824: 4}
const addItem = (id, count) => {
cartItems[id] = (cartItems[id] || 0) + count
}
const getArray = () =>
Object.entries(items).map(([key, count]) => ({ [key]: count }))