2

I got two Objects

object = {
  id: 1,
  Oid: 'ff2d344e-77e8-43b3-a6f3-bfba577d6abd',
  name: 'Some name'
}

and

object2 = {
  id: 3,
  array_ref: 'ff2d344e-77e8-43b3-a6f3-bfba577d6abd',
  value1: 'Additional Data',
  value2: 'Some other additional data'
}

As you see, the only reference between these Objects is the Oid and the Array_ref. Now I want to bind these Objects together to one where the Oid and the Array_ref matches. In the end I have to do this multiple times (with multiple objects).

Is there some easy way to do this?

This Code is realized in JavaScript using angular partly. I'm not able to use anything else.

drummercrm
  • 77
  • 2
  • 11
  • Are you able to use Lodash? I appreciate your question says you "can't use anything else", but I want to be sure. – Dan Sep 03 '15 at 09:51
  • I'm sorry, I even don't know, what Lodash is. So I'm afraid of using a Technology I don't know. I might also have to say, that I'm new at JavaScript. But I have to finish this task today. – drummercrm Sep 03 '15 at 09:55
  • Lodash is a helper library that will contain a function that makes this super easy (much easier than creating the solution yourself). If you're new to javascript I would recommend standing on the shoulders of giants and using it. – Dan Sep 03 '15 at 09:58
  • If you combine object then what about ID? do you want to remain both ID? or some other idea? – Roli Agrawal Sep 03 '15 at 09:59
  • I should mention that you only want to use lodash if you have multiple values, otherwise @Ismael's answer will work just fine. – Dan Sep 03 '15 at 10:04
  • I'm sorry, but I can't use Lodash. The IDs should remain as they are. But this obvisiously will cause a Problem when binding them together, right? – drummercrm Sep 03 '15 at 10:04
  • Okay, I should have mentioned, that the Code I posted is only an example and I have to do this with multiple objects. I Edited the original post – drummercrm Sep 03 '15 at 10:14
  • OK I don't think it is a hard problem but I am not sure I well understand your question. You have a list of objects with a property called `Oid`, and another list of objects with another property called `array_ref`, am I right? What do you mean by `bind`. Do you want just to link each of the objects in the second list with an object in the first? Does this link could be a property registered in the second object for example ? Or do you need to copy the values of the second object in the first one? Or again do you need a third list containing merged objects? – Quentin Roy Sep 03 '15 at 15:55
  • The best could be that you provide us with an example of the data structure you have (if you have 2 lists of object, give us 2 lists of objects not just 2 objects), and the exact result you are expecting with this data example. – Quentin Roy Sep 03 '15 at 16:00

2 Answers2

1

You are not using arrays (nor JSON) but objects.

If you want to merge two objets, one solution is to use next Javascript API (ES6) Object.assign method through a polyfill (polyfills give you the ability to use standard APIs that are not yet supported):

var obj1 = {
  id: 1,
  Oid: 'ff2d344e-77e8-43b3-a6f3-bfba577d6abd',
  name: 'Some name'
}

var obj2 = {
  id: 3,
  array_ref: 'ff2d344e-77e8-43b3-a6f3-bfba577d6abd',
  value1: 'Additional Data',
  value2: 'Some other additional data'
}

var obj3 = Object.assign({}, obj1, obj2);

console.log(obj3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.js"></script>

Which will gives you:

console

In the above snippet I included a whole es6 polyfill but this polyfill here only contains what you are looking for and is probably better if you want to avoid loading a whole bunch of useless code (I could not easily load it in the snippet). If you use jQuery or underscore, they also both provides an extend method that you can use instead of a polyfill.

Warning! As you can see, this will actually overwrites any content in obj1 that is also in obj2 with the content of obj2. Here, for example, the resulting object has the id of obj2. If you are looking for a more clever kind of merging, you will need to go for more complicated algorithmic.

If you want to merge only the objects with the same id, I would use underscore:

var objectList = [
  { id: 1, val: "val1", otherVal: "val2" },
  { id: 2, val: "val3" },
  { id: 1, val: "val4", differentVal: "val5" }
];

// group objects by ids
var groups = _.groupBy(objectList, 'id');

// merge the groups
var mergedObjects = _.map(groups, function(objects){
  // Adds a new empty object at the beginning of the list
  // to avoid modifying the original objects.
  // It will be the destination of _.extend.
  objects.unshift({});
  // use extend with apply so we can provide it an array to be used as arguments
  return _.extend.apply(_, objects);
});

console.log(mergedObjects);
<script src="http://underscorejs.org/underscore-min.js"></script>

Console

Notice that some overwriting still can happen (first object's val is overwritten by third object's val here).

Edit: As pointed out in the comment, lodash is likely to be a better solution than underscore. Read here about lodash vs. underscore.

Community
  • 1
  • 1
Quentin Roy
  • 7,677
  • 2
  • 32
  • 50
  • That implies that 1) The O.P. knows what jQuery or underscore is and 2) that the O.P. loads a giant library just to add properties to an object. In my opinion: total waste. Your answer isn't wrong and that is a correct way to do it. But it has a huge downside. – Ismael Miguel Sep 03 '15 at 10:08
  • You are right. But I think you are a bit hard. I gave all the links so he can see what I am talking about. jQuery is extremely used which is why I proposed it in the first instance. But I did talk about underscore (which is not big and extremely handy for this kind of operations) if he is not using jQuery. And I also talked about `Object.assign` whose polyfill is tiny. – Quentin Roy Sep 03 '15 at 10:11
  • The ES6 alternative should be the first one. Then the polyfill. The jQuery and underscore would be better as alternatives. That's simply my opinion – Ismael Miguel Sep 03 '15 at 10:13
  • 1
    +1 to @IsmaelMiguel. definitely don't use failQuery for this. It's not necessary in 99% of the cases these days. lodash would be a better alternative to underscore (faster, smaller, custom builds). – Dan Sep 04 '15 at 11:02
  • Thanks @ARedHerring. I thought lodash was just a superset of underscore but as it appears, it is a better option. – Quentin Roy Sep 04 '15 at 11:21
0

Here's a simple way.

Your object could look like this:

{
    'ff2d344e-77e8-43b3-a6f3-bfba577d6abd': {
        id: 1,
        name: 'Some name'
    }
}

And then you access it like this:

object[object2.array_ref]

And you would have all the data you want.

Later on, you can add properties to that object using a for( in ) loop:

var obj = object[object2.array_ref];

for(var k in object2)
{
    if( !(k in obj) && object2.hasOwnProperty(k))
    {
        obj[k] = object2[k];
    }
}

This would add the values to the object if they aren't already present.

You could verify other calues by adding conditions to that if.

Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42