0

I have two objects:

obj1 = { 'id1': 1, 'name1': 'anyone1', 'birth1': 22, 'year1': 1993 };

obj2 = { 'id2': 1, 'name2': 'anyone', 'birth2': 22, 'year2': 1993 };

And I would like to create a Array like:

ARRAY = {

          obj1 = { 
                    'id1': 1, 'name1': 'anyone1', 'birth1': 22, 'year1': 1993                  
                 },   

        obj2 = { 
                  'id2': 1, 'name2': 'anyone', 'birth2': 22, 'year2': 1993 
                }

          }

I would like to call it like 'ARRAY.obj1.id1'

How can I do this?

kfm
  • 143
  • 3
  • 17
  • 1
    This is very confusing... Why do you want to name a object "ARRAY"? Are you trying to intentionally mislead other developers? :D – Canastro Feb 14 '17 at 20:55
  • @Canastro, see associative arrays in PHP for example. They are used there pretty much like we use Object literals in JS. But I agree, names sometimes matter. – Thomas Feb 14 '17 at 21:06
  • Yes in php we have associative arrays, in javascript I prefer to call it `map`. I'm very fond on using maps in javascript, I'm in love with `normalizr`. – Canastro Feb 14 '17 at 21:12
  • I did it because of [this question](http://stackoverflow.com/questions/42216605/pass-multiple-params-from-angular-to-web-api-2?noredirect=1#comment71595040_42216605), I created a CompositeObject like [this other asnwer](http://stackoverflow.com/questions/24874490/pass-multiple-complex-objects-to-a-post-put-web-api-method#) – kfm Feb 14 '17 at 22:31

4 Answers4

2

The solution is to create a new object with two keys:

obj1 = { 'id1': 1, 'name1': 'anyone1', 'birth1': 22, 'year1': 1993 };

obj2 = { 'id2': 1, 'name2': 'anyone', 'birth2': 22, 'year2': 1993 };
var obj={};
obj["obj1"]=obj1;
obj["obj2"]=obj2;
console.log(obj);
console.log(obj.obj1.id1);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • Really should not name that object Array. – chrispy Feb 14 '17 at 20:58
  • I don't know what issue this addresses; if it satisfies OP, great, but then OP's problem is that he doesn't know how objects work in js. You technically don't even need the square brackets and quotes to make an object do this. But let's say we come up with an element called `obj["hasOwnProperty"]`. That's why we have the `Map` class in ES6, which is what OP should be using instead. – sqykly Feb 14 '17 at 22:45
  • @kfm, don't forget to accept answer in order to help other people. – Mihai Alexandru-Ionut Feb 14 '17 at 22:57
0

Another example using Object.assign:

obj1 = { 'id1': 1, 'name1': 'anyone1', 'birth1': 22, 'year1': 1993 };
obj2 = { 'id2': 1, 'name2': 'anyone', 'birth2': 22, 'year2': 1993 };
var obj3 = Object.assign({}, { obj1: obj1 }, { obj2: obj2} );
console.log(obj3);
Matthew Cawley
  • 2,688
  • 1
  • 8
  • 19
0

Also, since it was an angular question, you can also use the angular.extend function:

var obj1 = { 'id1': 1, 'name1': 'anyone1', 'birth1': 22, 'year1': 1993 };
var obj2 = { 'id2': 1, 'name2': 'anyone', 'birth2': 22, 'year2': 1993 };
var obj3 = angular.extend({}, { obj1: obj1, obj2: obj2 } );
console.log(obj3);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Matthew Cawley
  • 2,688
  • 1
  • 8
  • 19
0

Use

var myNamedStuff = new Map;
myNamedStuff.set("obj1", obj1);

etc.

What you're asking for is a Map. We used plain objects as maps before ES6, but we got all sorts of security risks and exceptions related to name clashes, especially when vendor-specific properties show up in Object.prototype. ES6 introduces the Map so that we never do that again and instead have a guaranteed clean name space like we really wanted.

sqykly
  • 1,586
  • 10
  • 16