Reputation: 847
I have two javascript objects
var category = new Object();
I add some attributes to the object as
category.Hot = "Red";
category.Cold = "Blue";
category.Warm = "Yellow";
I have another object which holds the id, category value and some other attributes
var categorization = [];
categorization = {"id1 : Hot","id2 : Cold","id3 : Hot","id4 : Warm"},
Now I need to map all the items in categorization with their appropriate color in category. I need something like this.
var combinedResult = null;
combinedResult.id1 = {'Hot : Red'}
combinedResult.id2 = {'Cold : Blue'}
combinedResult.id3 = {'Hot : Red'}
combinedResult.id4 = {'Warm : Yellow'}
I'm new to javascript so there may be syntactical errors. I have achieved the same in C# any go ahead tips would be appreciated.
Upvotes: 0
Views: 414
Reputation: 3302
You may do it this way:
$.each(categorization,function(id, val){
var tempObj = {};
tempObj[val] = category[val];
combinedResult[id] = tempObj;
});
Here's sample Fiddle.
Upvotes: 4
Reputation: 518
Wouldn't something along the lines of the below be easier for you?
var categorization = [];
categorization.push({"Id" : "Hot","colour":"Red"});
categorization.push({"Id" : "Cold","colour":"Blue"});
categorization.push({"Id" : "Warm","colour":"Yellow"});
Then
categorization[0].Id, categorization[0].colour etc...
Upvotes: 0