The below data is my json object
var column = {
"SNo": [{ "group": "true", "sort": "true", "filter": "true", "type": "number", "min": 1, "max": 1000, "format": "{0:c}"}],
"Name": [{ "group": "true", "sort": "true", "type": "string", "columnmenu": "true"}],
"City": [{ "group": "true", "type": "number", "filter": "true", "width": "100px", "columnmenu": "false"}]
};
I need the above data split into array list without looping
first array list : ["SNo", "Name", "City"]
Second array list : ["group", "sort", "filter", "type", "min", "max", "format", "group", "sort", "type", "columnmenu", "group", "type", "filter", "width", "columnmenu"]
Third array list: ["true", "true", "true", "number", 1, 1000, "{0:c}", "true", "true", "string", "true", "true", "number", "true", "100px", "false"]
Fourth array list : [7, 4, 5] // count of each attributes
Please help me. am new of this field. otherwise reduce the maximum no.of loops.
-
4What's the problem with looping ?Denys Séguret– Denys Séguret2014年06月26日 13:47:01 +00:00Commented Jun 26, 2014 at 13:47
-
I think from his last line, he has no problem with loops. He just want minimum no. of loops.Mithlesh Kumar– Mithlesh Kumar2014年06月26日 13:57:54 +00:00Commented Jun 26, 2014 at 13:57
-
1That's a JavaScript object, not JSON.Felix Kling– Felix Kling2014年06月26日 14:08:21 +00:00Commented Jun 26, 2014 at 14:08
3 Answers 3
Here I also got something working when you can have more keys in columns object.
var first_array = Object.keys(column);
var second_array = [];
var third_array = [];
var fourth_array = [];
first_array.forEach(function(inner_key){
var keysInInnerKey = Object.keys(column[inner_key][0]);
var valuesInInnerKey = keysInInnerKey.map(function(key){
return column[inner_key][0][key];
});
second_array = second_array.concat(keysInInnerKey);
third_array = third_array.concat(valuesInInnerKey);
fourth_array.push(keysInInnerKey.length);
});
Comments
Since you don't know the value of the object keys, perhaps a simple function that returns a set of keys or values can do the trick.
function getAll(obj, type) {
var arr = [];
for (var key in obj) {
if (type === 'keys') {
arr.push(key);
} else {
arr.push(obj[key]);
}
}
return arr;
}
var one = getAll(column, 'keys');
You can loop over the elements in the first array to get fill the rest of the arrays:
var two = [];
var three = [];
for (var i = 0, l = one.length; i < l; i++) {
two.push.apply(two, getAll(column[one[i]][0], 'keys'));
three.push.apply(three, getAll(column[one[i]][0], 'values'));
}
Comments
First array:
Object.keys(column)
Second array:
Object.keys(column['SNo'][0])
.concat(Object.keys(column['Name'][0]))
.concat(Object.keys(column['City'][0]))
Third array:
Object.keys(column['SNo'][0]).map(function(k){return column['SNo'][0][k]})
.concat(Object.keys(column['Name'][0]).map(function(k){return column['Name'][0][k]}))
.concat(Object.keys(column['City'][0]).map(function(k){return column['City'][0][k]}))
Forth array:
Object.keys(column).map(function(k){return Object.keys(column[k][0]).length})
No looping :) (not for at least....)