0

I have a set of JQuery arrays that I need to join into a single array, jsonify and pass to the python script in the back end.

my_jq.js:

var a = ['one','two'];
var b = ['three','four'];
var c = ['five'];
var d = ['six','seven','eight']; 

I tried this, but it does not generate an array:

var e = $.merge(a,b,c,d)
var json_array = JSON.stringify(e)

What am I doing wrong?

2 Answers 2

1

var a = ['one','two'];
var b = ['three','four'];
var c = ['five'];
var d = ['six','seven','eight']; 

var myJsonString = JSON.stringify(a.concat(b,c,d));
console.log( myJsonString );
.as-console-wrapper{top:0}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

0

The $.merge jQuery function only takes two parameters. What you have is working but the values in c and d will be omitted.

You can use concat: [].concat(a,b,c,d)

or in ES6 with the spread syntax = var e = [...a, ...b, ...c, ...d]

1 Comment

It works - I get an Array object in the Inspect > Console. However, when I am calling array e, it comes back empty

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.