2

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.

3
  • 4
    What's the problem with looping ? Commented 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. Commented Jun 26, 2014 at 13:57
  • 1
    That's a JavaScript object, not JSON. Commented Jun 26, 2014 at 14:08

3 Answers 3

1

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);
});
Sign up to request clarification or add additional context in comments.

Comments

1

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'));
}

DEMO

Comments

0

DEMO

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....)

2 Comments

Thanks Kuf, But in second and third array you put the name that is SNo, Name, City like, but I don't know this column name because this data dynamically come from database. please give some other alternate solution. Thanks and advance
@Raviraj why can't you use any loops?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.