0

I have multiple JSON data like

var resultJSON = 
   '{"l":"1","p":"1","name":"john"},
    {"l":"1","p":"2","name":"john1"},
    {"l":"1","p":"2","name":"john2"}';

So Now,

I want to generate multidimensional array using jquery

resultJSON.each(function(){
var result = $.parseJSON(resultJSON);
    obj[1][1] = array("john");

}

How can I generate multidimensional array and if array has common key append the name?

OUTPUT will be:

 obj[1][1] = array("john");
 obj[1][2] = array("john1","john2");
7
  • can add your expected result? Commented Jan 13, 2016 at 8:27
  • @vsogrimen : yes updated. Commented Jan 13, 2016 at 8:27
  • You can use push(yourVal) to add elements to Array Commented Jan 13, 2016 at 8:32
  • @erikscandola : i tried push but its not generating key value pair i need Commented Jan 13, 2016 at 8:33
  • In your case, what's the key? Commented Jan 13, 2016 at 8:34

2 Answers 2

2

Firstly, your JSON is not an array, so you shold add square brackets.

Moreover you should check for each level of your final array, if the value exists and eventually initialize an array and push the name inside it.

var results = $.parseJSON('['+resultJSON+']');
var obj = [];
$.each(results, function(i, res){
    var l = res.l, p = res.p;
    if(!obj[l]) obj[l] = [];
    if(!obj[l][p]) obj[l][p] = [];
    obj[l][p].push(res.name)
});

You can find a snippet here:

var resultJSON = '{"l":"1","p":"1","name":"john"}, {"l":"1","p":"2","name":"john1"},{"l":"1","p":"2","name":"john2"}';

var results = $.parseJSON('['+resultJSON+']');
var obj = [];
$.each(results, function(i, res){
    var l = res.l, p = res.p;
    if(!obj[l]) obj[l] = [];
    if(!obj[l][p]) obj[l][p] = [];
    obj[l][p].push(res.name)
});

$('#output1').text(JSON.stringify(obj));
$('#output2').text(JSON.stringify(obj[1][2]));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<output id="output1"></output>
<br>
<output id="output2"></output>


EDIT

Alternative way:

var obj = {};
$.each(results, function(i, res){
  var l = res.l, p = res.p;
  if(!obj[l]) obj[l] = {};
  if(!obj[l][p]) obj[l][p] = [];
  obj[l][p].push(res.name)
});
//output {"1":{"1":["john"],"2":["john1","john2"]}} 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, just one more thing, after creating multidim obj how can i stringify it? JSON.stringify not working.
It is not working? What do you expect to have in output?
It is giving result with multiple null values whereas i need proper string
Yes, sure! It is because obj[0][0] and obj[0][1] are not explicited. You should start from 0 in arrays :)
any solution of this issue?
|
2
var src=[{"l":"1","p":"1","name":"john"},
         {"l":"1","p":"2","name":"john1"},
         {"l":"1","p":"2","name":"john2"}];

var obj={};
$.each(src,function(i,v){
  if (!obj[v.l]) {obj[v.l]={};obj[v.l][v.p]=[v.name]}
  else if (!obj[v.l][v.p]) obj[v.l][v.p]=[v.name]
  else obj[v.l][v.p].push(v.name);
})

The only jquery element in this is the $.each() method which you can even replace by the Array method .forEach() (the order of the anonymous function arguments must be changed then).

Comments

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.