arrays - How to loop through javascript object and copy values to new object -


this question has answer here:

i have javascript object on page contains info. need loop through object , copy object.

here starting code

var p_tag = {}; var new_tag = {}; p_tag.abc='abc'; p_tag.def='def'; p_tag.ghi='ghi';  (var key in p_tag) {    if (p_tag.hasownproperty(key)) {    console.log(key + "'s favorite fruit " + p_tag[key]);    } } 

i need take values p_tag , copy them new_tag object.

i may not know keys available on each page looking can go through available p_tags , copy them new_tag.

thanks,

just assign of old values new values.

for (var key in p_tag) {    if (p_tag.hasownproperty(key)) {        console.log(key + "'s favorite fruit " + p_tag[key]);         new_tag[key] = p_tag[key];    } } 

Comments