Reputation: 7256
My goal is to have a object like below:
var attrs = {
price : [0, 500000],
housesize : [0, 40],
lotwidth : [0, 30],
storeys : 'both',
bedroom : 0,
bathroom : 0
};
But I dont want to hard code this and would like to generate automatically from the elements of another object. The object looks like this:
var Defaults = {
steps : [10,3,3], // steps for the slider to jump
filters : {
rangeFilter : ['price', 'housesize', 'lotwidth'], // list of slider inputs
radioFilter : ['storeys'], // list of radio inputs
increamentFilter: ['bedroom', 'bathroom'],
}
};
So I am trying to build an object whose keys will be all the values of the filters
object. The reason I want to do this is because if I add some value to the filters
object, I would not need to create a key in attrs
object. The numbers of keys in the attrs
object is exactly same as the elements in filters
object.
Any help would be appreciated. Cheers.
==========================================
This is what I came up with at the moment
var attrs = {};
for (var key in Defaults.filters) {
var obj = Defaults.filters[key];
for (var prop in obj) {
attrs[obj[prop]] = 0;
}
}
But at the moment I am adding 0 to all the keys of the new element. I need to come up with a way to add specific values to it dynamically. May be another object inside range filter ??
Upvotes: 0
Views: 1534
Reputation: 449
I always use this snipped for such tasks:
function deepCopy(obj) {
if (Object.prototype.toString.call(obj) === '[object Array]') {
var out = [], i = 0, len = obj.length;
for ( ; i < len; i++ ) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
if (typeof obj === 'object') {
var out = {}, i;
for ( i in obj ) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
return obj;
}
from: http://snipplr.com/view/15407/
Upvotes: 1