Merging javascript objects

拟墨画扇 提交于 2019-12-13 04:35:52

问题


Wish to merge two objects. However, when a property "date" with same value exists in both the objects, the other properties (tag1 and tag2) are to be clubbed under the same date.

Example input

var myVar1 = [
{
"date": "2015-07-16",
  "tag1": 35.34},
{
"date": "2015-07-18",
  "tag1": 34.12}
  ];

var myVar2 = [
{
"date": "2015-07-16",
  "tag2": 45.34},
{
"date": "2015-07-17",
  "tag2": 44.12}
  ];

Desired output

mergedVar = [
{
"date": "2015-07-16",
  "tag1": 35.34,
  "tag2": 45.34},
{
"date": "2015-07-17",
  "tag2": 44.12},
{
"date": "2015-07-18",
  "tag1": 34.12}
  ];

回答1:


I posted my comment with the idea that someone else would post a more serious answer, but since nobody came along, I guess I’ll describe how you might go about doing it seriously.

First, you’ll have to know how to merge one object into another. It’s not too difficult, but you do have to know how to use for-in, which isn’t frequently taught, and it’s usually a good idea to include a hasOwnProperty check which is even less-frequently taught:

function mergeObjectInto(source, target) {
    for(var property in source) {
        if(Object.prototype.hasOwnProperty.call(source, property)) {
            target[property] = source[property];
        }
    }
}

Essentially, this enumerates over all properties of the source. If that property is an own property of the object—that is, it’s not something it inherited from its prototype—we copy that property over to the target.

Now, to model the kind of operation you want to do, we can decompose it down into a simpler operation: given an existing array, put another object in that array, merging if necessary. A naïve implementation might look like this:

function mergeObjectIntoArray(existingArray, newObject) {
    // Search for an already-existing object in the array that shares the same
    // value for the `date' property.
    var correspondingExistingObject = null;
    for(var index = 0, length = existingArray.length; index < length; index++) {
        if(existingArray[index].date === newObject.date) {
            correspondingExistingObject = existingArray[index];
            break;
        }
    }
    if(correspondingExistingObject !== null) {
        // If we found an existing object that corresponds to our new object, just
        // merge any new properties in rather than adding a new item to the array.
        mergeObjectInto(newObject, correspondingExistingObject);
    }else{
        // Otherwise, bite the bullet and add the new object since there's nothing
        // we can usefully merge into.
        existingArray.push(newObject);
    }
}

You could then implement the sort of merge operation you wanted by calling this function in a loop. But if you work it out, that’s going to have quadratic time, which will be an issue if this has to work on a large number of data items. As it happens, there is a way you can work around it. If we sort items into a hash-table-like structure keyed by date property as we go, we can check in constant time whether we have an object with that date or not, and if so, merge into it; otherwise, add it. That leads to a very reasonable linear time for the entire algorithm.

One complication is that until recently with ECMAScript 6, JavaScript hasn’t really had a real “hash table”. The closest thing we have are objects, which can only have string keys. That’ll work for us, but we do need to exercise some caution: for better or worse, browser vendors have created some property names with special significance, like __proto__, that we really don’t want to step on, so we’ll prefix our keys with some character that prevents any clashes with built-in names. I chose # for this purpose. (# is a hash, and it’s a hash-table.)

In code, here’s how that would work out, this time the function taking an entire array of items and merging all objects with duplicate date property values:

function mergeDuplicateObjectsInArray(array) {
    var result = [];
    var objectsByDate = {};
    for(var index = 0, length = array.length; index < length; index++) {
        var object = array[index];
        var hashKey = '#' + object.date;
        var mergeTarget;
        if(Object.prototype.hasOwnProperty.call(objectsByDate, hashKey)) {
            // Already have an object by that date; we ought to merge into that.
            mergeObjectInto(object, objectsByDate[object.date]);
        }else{
            // No object by that date yet; we'll be the first.
            result.push(object);
            objectsByDate[object.date] = object;
        }
    }
    return result;
}

This still misses out on one last thing: it’s only merging objects within a single array, not two different arrays. But it will do what you want if you created a single array with the objects of both arrays, e.g. myVar1.concat(myVar2). In any case, you could also modify the for loop in it to loop over an array of arrays, and then loop over the objects in those arrays, which is what my golfed version did. The other difference to the golfed version is the golfed version avoids modifying the original arrays or objects, which would be a relatively simple modification of the unobfuscated version: to make a copy, just merge into an empty object.

Hopefully that clears things up.



来源:https://stackoverflow.com/questions/32217485/merging-javascript-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!