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

11
  • 1
    Your code doesn't have any jQuery in it. What are you asking exactly? Can you provide your jQuery code? Commented Nov 15, 2017 at 7:49
  • I mean is basic javascript Commented Nov 15, 2017 at 8:04
  • Did you try to add another value? if you don't use the same key, you won't get it erased. Commented Nov 15, 2017 at 8:06
  • I tried it. It keeps erasing the old one. Commented Nov 15, 2017 at 8:08
  • You are probably redefining your object somewhere in the code you have not shown. Commented Nov 15, 2017 at 8:09

4 Answers 4

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

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

Comments

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

Comments

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.

Comments

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

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.