I want to check if the an id already exist in my array. if It exists the older value should be removed, and the new value should be added to the array, if not it should be added to the array.
Here's the code that I tried but didn't work.
sample_array[0] = {'id' : 1, 'letter': 'a'};
sample_array[1] = {'id' : 2, 'letter': 'b'};
sample_array[2] = {'id' : 3, 'letter': 'c'};
sample_array[3] = {'id' : 4, 'letter': 'd'};
sample_array[4] = {'id' : 5, 'letter': 'e'};
input_id = 3;
count_length = sample_array.length;
input_letter = 'L';
idx = $.inArray(input_id, sample_array.id); // <- i think this is where it goes wrong.
if(idx == -1)
{
//add to the array
sample_array[count_length] = {'id' : input_id, 'letter': input_letter};
}
else
{
//remove then add to the array
sample_array.splice(idx, 1);
sample_array[count_length] = {'id' : input_id, 'letter': input_letter};
}