0

how do join the array and create new array?

var page_list = [ 'dkey', 'dvalue' ];
var json = '{"key1":"value1","key2":"value2","key3":"value3"}';
var jp = JSON.parse(json);

for( var index in jp ) {
   var obj = jp[index]; 
   //page_list.concat( ['test1','value2'] );
}

//expecting output
[ [ 'dkey', 'dvalue' ], [ 'key1', 'value1'], ['key2','value2'], ['key3','value3'] ]

https://jsfiddle.net/zerolfc/zjgx2pcj/1/

Can someone give me a solution, on how do I achieve the expecting output?

2
  • jp is not an array. jp is an object with attributes key1, key2 and key3. So jp[index] has no meaning. Commented Jun 19, 2015 at 11:18
  • @Blip jp[index] works! Commented Jun 19, 2015 at 11:20

4 Answers 4

1

var page_list = [ 'dkey', 'dvalue' ];
var arr = []; //Create new array
arr.push(page_list); //Push page_list

var json = '{"key1":"value1","key2":"value2","key3":"value3"}';
var jp = JSON.parse(json);
for( var key in jp ) {
   arr.push([key, jp[key]]); //Push key, value 
}

document.write(JSON.stringify(arr))
//outputs
//[["dkey","dvalue"],["key1","value1"],["key2","value2"],["key3","value3"]]

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this:-

$(function() {

  var array = [];
  var page_list = [ 'dkey', 'dvalue' ];

  var new_arr = '{"key1":"value1","key2":"value2","key3":"value3"}';

  var jp = JSON.parse(new_arr);
  parentArr = [];

  for( var index in jp ) {
      var obj = jp[index];
      parentArr.push([index, obj]);
  }
  parentArr.push(page_list);
  console.log(parentArr);

 });

https://jsfiddle.net/zjgx2pcj/4/

3 Comments

Why did you add $(function(){ }); ?
@Cerbrus Just defining it all in a scope. Its not necessary you can remove it .
For answers on SO, I'd advice against adding extra's like that.
1

You can do this:

var page_list = [ 'dkey', 'dvalue' ];
var json = '{"key1":"value1","key2":"value2","key3":"value3"}';
var jp = JSON.parse(json);

// Map all values from `jp` into an array.
var arr = Object.keys(jp).map(function (key) {return [key, jp[key]]});
// Add `page_list` as first item of the array.
arr.unshift(page_list);

console.log(arr);
alert(JSON.stringify(arr));

Object.keys(jp) gets an array of keys from jp (So, ['key1', 'key2', 'key3']).

Then, .map iterates over that result, where [key, jp[key] is returned for each key, which results in ['key1', 'value1'] etc.
Finally, page_list is added at the front of arr.

Comments

1

Use .push() and remove the obj:

$(function() {
    var array = [];
    var page_list = [ 'dkey', 'dvalue' ];
    var new_arr = '{"key1":"value1","key2":"value2","key3":"value3"}';

    var jp = JSON.parse(new_arr);
    array.push( page_list );
    for( var index in jp ) {
        array.push( [index, jp[index]] );
    }
    console.log ( array );
});

Fiddle: https://jsfiddle.net/praveenscience/zjgx2pcj/6/

3 Comments

Doesn't outputs the expected result.
var arr = []; arr.push(page_list); Repeating the comment Doesn't outputs the expected result
Yes it looks good now you accept my original comment Doesn't outputs the expected result

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.