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?

4
  • 1
    This is very confusing... Why do you want to name a object "ARRAY"? Are you trying to intentionally mislead other developers? :D Commented Feb 14, 2017 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. Commented Feb 14, 2017 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. Commented Feb 14, 2017 at 21:12
  • I did it because of this question, I created a CompositeObject like this other asnwer Commented Feb 14, 2017 at 22:31

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

3 Comments

Really should not name that object Array.
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.
@kfm, don't forget to accept answer in order to help other people.
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);

Comments

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>

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.