kevin_marcus
kevin_marcus

Reputation: 277

Possible to add Index to a javascript Object

Is it possible for me to create an Index into an Object? Here is my object:

var objName = {};
var array1 = ['apple','sugar'];
var array2 = ['spoon','fork'];
var array3 = ['spoon2','fork2'];
var array4 = ['spoon3','fork3'];

objName['main'] = array1;
objName['submain'] = array2;
objName['main'] = array3;
objName['submain'] = array4;

console.log(objName);

question is, I want to add another value to the object without my previous object being erased. Assuming that the objName is a global object variable and I'm going to use it again and again and pushing different values into it. I am expecting an output where in I can see also my previous records.

JSFiddle

Upvotes: 0

Views: 92

Answers (4)

prasanth
prasanth

Reputation: 22510

Same key in single object is not working.so create the array and pass with objects same key name also

var objName = [];
var array1 = ['apple', 'sugar'];

var array2 = ['spoon', 'fork'];

var array3 = ['spoon2', 'fork2'];

var array4 = ['spoon3', 'fork3'];

objName.push({
  main: array1
});
objName.push({
  submain: array2
});
objName.push({
  main: array3
});
objName.push({
  main: array4
});
console.log(objName);

Upvotes: 1

Sebastian Kaczmarek
Sebastian Kaczmarek

Reputation: 8515

Imagine you have 4 children and you named them 2x John and 2x Kate. Now, you gave something to one of John's and now you want it back. So you are calling "Jooooohn! Come here for a sec!". Which John should come? The first one or the second one? Even more, which one is the first one?

Do you see now, what you did wrong? You cannot assign two identical keys to the Javascript object because none would know which property you want to access at the time. That's why keys have to be unique.

If you want to have also previous records in your object, then use an array, as other guys suggested.

Upvotes: 1

Hassan Imam
Hassan Imam

Reputation: 22574

You can check if your object contains a given key, then you can array#concat your array, otherwise simply assign array to that key.

obj[name] = obj[name] ? obj[name].concat(array) : array; where obj is your object, name is the name of your key and array is the new array you want to add.

var objName = {};
var array1 = ['apple','sugar'];
var array2 = ['spoon','fork'];
var array3 = ['spoon2','fork2'];
var array4 = ['spoon3','fork3'];

var addInObject = function(obj, name, array){
  obj[name] = obj[name] ? obj[name].concat(array) : array;
}

addInObject(objName, 'main', array1);
addInObject(objName, 'submain', array2);
addInObject(objName, 'main', array3);
addInObject(objName, 'submain', array4);

console.log(objName);

Upvotes: 1

Icepickle
Icepickle

Reputation: 12806

I am assuming you wish to keep all the values you added already, so I added getter and setter for main``andsubmain` variables.

This will return the last entry when a new entry was added, and you would have some kind of history afterwards by the _main and _submain properties.

var objName = {
  get main() {
    if (this._main === undefined) {
      return undefined;
    }
    return this._main[this._main.length-1];
  },
  set main(value) {
    if (this._main === undefined) {
      this._main = [];
    }
    this._main.push( value );
  },
  get submain() {
    if (this._submain === undefined) {
      return undefined;
    }
    return this._submain[this._submain.length-1];
  },
  set submain(value) {
    if (this._submain === undefined) {
      this._submain = [];
    }
    this._submain.push( value );
  },
};
var array1 = ['apple','sugar'];
var array2 = ['spoon','fork'];
var array3 = ['spoon2','fork2'];
var array4 = ['spoon3','fork3'];

objName['main'] = array1;
objName['submain'] = array2;
objName['main'] = array3;
objName['submain'] = array4;

console.log(objName);

Upvotes: 1

Related Questions