javascript - Adding new element to the object based on the comparison -
i have following objects:
var empary= [{"empid":"101","name":"david"},{"empid":"102","name":"sam"}..];//2000 records var empary2= [{"empid":"101","name":"david"},{"empid":"105","name":"kevin"},{"empid":"109","name":"robert"},{"empid":"110","name":"rob"}..];//30000 records i need add new element empary object , populate new element value based on availability of particular record in empary2.
expected output:-
empary= [{"empid":"101","name":"david", **"founinempary2":"yes"**},{"empid":"102","name":"sam", **"founinempary2":"no"}**..];//2000 records if can jquery good. please me.
it's hard make sense of founinempary2 since object structures identical in both samples. assume other properties exist , use jquery $.extend() "merge" properties.
first efficient loop through big array once , create object using empid keys.
var tmp = {}; $.each( empary2, function(_, item){ tmp[ item.empid ] = item; }); this creates object like:
{ "101" : {"empid":"101","name":"david"}, "102" : {"empid":"102","name":"sam"} } now loop through first array , extend whatever in matching object in tmp object
$.each( empary, function(_, item){ $.extend( item, tmp[ item.empid ]); }); reference: $.extend() docs
Comments
Post a Comment