0

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

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
kevin_marcus
  • 277
  • 1
  • 5
  • 19

4 Answers4

1

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);
prasanth
  • 22,145
  • 4
  • 29
  • 53
1

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);
Icepickle
  • 12,689
  • 3
  • 34
  • 48
1

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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

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.

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38