合并JavaScript对象 [英] Merge JavaScript objects
问题描述
我已经阅读了另一个关于SO的类似问题但是没有这个问题的答案很明确。
I've read another similiar question on SO but there wasn't a clear answer at that question.
我有一些JavaScript对象如下所示:
I got some JavaScript objects that look like this:
var moveJSON = {
'name' : move[0].innerHTML,
'info' : move[1].innerHTML,
'power' : move[2].innerHTML,
'accuracy' : move[3].innerHTML,
'type' : move[4].innerHTML,
'category' : move[5].innerHTML,
'pp_min' : move[6].innerHTML,
'pp_max' : move[7].innerHTML
}
我需要将它们合并到一个对象中,然后通过AJAX发送给PHP。但首先:将它们合并为单个对象(数组)的最佳方法是什么?
I need to merge them into one object, that will be send to PHP via AJAX. But first: what's the best way to merge them into a single object (array)?
推荐答案
这应该可行。
var array = []
array.push(yourObject);
这会将你的对象放入一个数组中,如果你要放一个将同一个对象合二为一。
this will put your object into an array, which should be used if you were putting of a collection of the same object into one.
将不同的值合并到一个对象中:
to merge different values into one object:
function makeObject(){
var obj = {};
obj['info'] = somevalue; //arguments [0] maybe
obj['power'] = somevalue;
obj['accuracy'] = somevalue;
obj['type'] = somevalue;
obj['category'] = somevalue;
obj['pp_min'] = somevalue;
obj['pp_max'] = somevalue;
return obj;
}
这是一个链接描述将数组之类的参数传递给JavaScript中的函数,这可能对创建合并对象很有用。
Here is a link also describing passing arguments like array into a function in JavaScript which could be useful in creating the merged object.
这篇关于合并JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!