Wednesday, 2 October 2013

Unable to add properties to a object using $.each() function

Unable to add properties to a object using $.each() function

I have a object habits contain some values
var habits={
"Drinking":"No",
"Smoking":"No"
}
I need to add the values in this variable to another variable in this format
var NewHabits=new Object();
NewHabits.Drinking="No";
NewHabits.Smoking="No";
My habits variable is big so i decided to add values using jquery $.each()
function
var NewHabits=new Object();
$.each(habits,function(index, value)
{
NewHabits.index=value;
}
But after executing this code i am getting value in NewHabits variable as
below

"index":"No","index":"No"
the problem is giving variable like this NewHabits.index
Please help me to solve this

No comments:

Post a Comment