Combine JavaScript objects

I read another similar question on SO , but there was no clear answer to this question.

I have 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
}

I need to combine them into one object, which will be sent to PHP through AJAX. But first: what is the best way to combine them into one object (array)?

+3
source share
4 answers

That should work.

var array = []

array.push(yourObject);

this will put your object in an array, which should be used if you put a collection of the same object in one.

to combine 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;

}

, array JavaScript, .

http://www.cherny.com/webdev/60/javascript-function-arguments-default-values-passing-objects-and-overloading

+3

"" , . , . , . , , .

Dojo, :

var mergedObject = dojo.mixin(object1, object2);

, :

var merge = function() {
    var result = {},
        length = arguments.length,
        object = null,
        key    = null;

    if ( length < 2 ) {
        throw "Must merge two or more objects";
    }

    for ( var i=0; i<length; ++i ) {
        object = arguments[i];
        for ( var key in object ) {
            if ( !object.hasOwnProperty(key) ) { continue; }
            result[key] = object[key];
        }
    }
    return result;
};

var mergedObject = merge({a:1}, {b:2, c:3, d: {a: 1}}, {a: 2, c:[1,2,3]});
// mergedObject looks like {a:4, b:2, c:[1,2,3], d:{a:1}}

, :

var aggregate = function() {
    if ( length < 2 ) {
        throw "Must aggregate two or more objects";
    }

    // The following can be simplified to 
    //   return Array.prototype.slice.call(arguments);
    // but is left in a more explicit manner to illustrate the difference

    var result = [],
        length = arguments.length;

    for ( var i=0; i<length; ++i ) {
        if ( arguments.hasOwnProperty(i) ) {
            result.push(arguments[i]);
        }
    }

    return result;
};

var aggregation = aggregate({a:1}, {b:2, c:3, d: {a: 1}}, {a: 4, c:[1,2,3]});
// aggregation looks like [{a:1}, {b:2, c:3, d: {a: 1}}, {a: 4, c:[1,2,3]}];

, , mergedObject {a:4, b:2, c:[1,2,3], d:{a:1}}, d mergedObject.d, aggregation, [{a:1}, {b:2, c:3, d: {a: 1}}, {a: 4, c:[1,2,3]}] d aggregation[1].d.

, - , JavaScript

var aggregation = aggregate({a:1}, {b:2, c:3, d: {a: 1}}, {a: 4, c:[1,2,3]});

var aggregation = [{a:1}, {b:2, c:3, d: {a: 1}}, {a: 4, c:[1,2,3]}];
+5

JSON, JSON:

var containerJSON = {};

:

containerJSON[0] = moveJSON_1;
containerJSON[1] = moveJSON_2;

PHP json_decode(), .

0

mixin,

var mixin = function(addToJSON, someOtherJSON) {
  for(var key in someOtherJSON) {
     // don't overwrite 
     if(!moveJSON[key]) {
       moveJSON[key] = someOtherJSON[key]; 
     }
  }
};

var otherJSON = {'someOtherStuff': someOtherMoves[0].innerHTML};

mixin(moveJSON, otherJSON);

This should do what you need.

-1
source

Source: https://habr.com/ru/post/1723038/


All Articles