Create object with duplicate keys

In Javascript, how can I create this object without overwriting the keys with equal names. When the object is displayed, the keys override..

var dip = {
    qtd: 6,
    lce: {
        'oct': {
            'sgs': 1,
            'ucs': 1
        },
        'oct': {
            'sgs': 2,
            'ucs': 2
        }
    }
};
console.log(dip);

//Result of console.log
{
    qtd: 6,
    lce: {      
        'oct': {
            'sgs': 2,
            'ucs': 2
        }
    }
}

Upvotes: 0

Views: 3840

Answers (2)

Douglas Tyler Gordon
Douglas Tyler Gordon

Reputation: 569

You can't. Maybe lce should point to an array of objects. If the oct stuff is essential it could look like this:

var dip = {
  qtd: 6,
  lce: [
    { 'sgs': 1,
      'ucs': 1,
      'oct': true
    },
    { 'sgs': 2,
      'ucs': 2,
      'oct': true
    }
  ]
}

Upvotes: 0

Jordan Soltman
Jordan Soltman

Reputation: 3883

Short answer, you can't. And it wouldn't make sense if you could either. The problem is that if you have multiple objects that have the same key, and you go back to access one of them later, which one would it be pointing too? The idea behind keys is that it references one, and only one object.

Upvotes: 1

Related Questions