hi how to compare 2 array javascript for multiple select
first array is account : ['ID': 2,'nama': 'test']['ID': 3,'nama': 'test1']['ID': 4,'nama': 'test4']['ID': 5,'nama': 'test5']
second array is account_selected : ['ID': 2,'nama': 'test']['ID': 5,'nama': 'test']
how to know on first array have id to second array, i have to code like this but just array [0] can compare
$.each( account, function( i, value ) {
var data = value;
if (data.ID == account_selected[0].ID){
temp += '<option selected value="'+data.ID+'" id="account_id'+data.ID+'" value="'+data.ID+'" >'+data.nama+'</option>'
} else {
temp += '<option value="'+data.ID+'" id="account_id'+data.ID+'" value="'+data.ID+'" >'+data.nama+'</option>'
}
});
this code just first compare first array account_selected ,i want to option is selected if data same
account_selected[0]should beaccount_selected[index]index > -1. Soif (index > -1 && data.ID == account_selected[index].ID). Why do you use jQuery functions for this? Native JavaScript has functions for this... Use jQuery for DOM manipulation, but not for working with or looping over arrays.