将数组推入一个JSON数组

时间:2022-03-06 12:16:45

I'm trying to push two whole arrays into one array.

我要把两个数组放到一个数组中。

The two arrays are named "quiz" and "t", how do I push them into "canvas"? Am I totally out of logic here or what am I missing? :)

这两个数组被命名为“quiz”和“t”,如何将它们放入“canvas”中?我在这里完全不符合逻辑,还是我遗漏了什么?:)

var canvas = {};

canvas.push({
    QuizModule: quiz,
    Elements: t
    });

json_elements = JSON.stringify({Elements: canvas }, null, "\t");

2 个解决方案

#1


5  

It should be [] not {}. Push is applied on the array [] not on object {}.

它应该是[]而不是{}。Push应用于数组[]而不是对象{}。

var canvas = [];

canvas.push({
    QuizModule: quiz,
    Elements: t
    });

#2


0  

push() is a function for Arrays, not Objects.

push()是数组的函数,而不是对象。

If you want to keep canvas as an Object, you could use extend() from Underscore.js (http://underscorejs.org/#extend):

如果希望将canvas作为对象,可以使用从下划线开始的extend()。js(http://underscorejs.org/扩展):

_.extend(canvas, { QuizModule: quiz, Elements: t });

jQuery extend() would to the same.

jQuery扩展()也是一样的。

#1


5  

It should be [] not {}. Push is applied on the array [] not on object {}.

它应该是[]而不是{}。Push应用于数组[]而不是对象{}。

var canvas = [];

canvas.push({
    QuizModule: quiz,
    Elements: t
    });

#2


0  

push() is a function for Arrays, not Objects.

push()是数组的函数,而不是对象。

If you want to keep canvas as an Object, you could use extend() from Underscore.js (http://underscorejs.org/#extend):

如果希望将canvas作为对象,可以使用从下划线开始的extend()。js(http://underscorejs.org/扩展):

_.extend(canvas, { QuizModule: quiz, Elements: t });

jQuery extend() would to the same.

jQuery扩展()也是一样的。