1

This should be pretty easy but I'm a little confused here. I want to fill this object:

var obj = { 2:some1, 14:some2, three:some3, XX:some4, five:some5 };

but in the start I have this:

var obj = {};

I´m making a for but I don't know how to add, I was using push(), but is not working. Any help?

2
  • So your keys are not in order, and have different name schemes? Commented Apr 15, 2013 at 17:31
  • Yes, They are not the same Commented Apr 15, 2013 at 17:39

3 Answers 3

3

You can't .push() into a javascript OBJECT, since it uses custom keys instead of index. The way of doing this is pretty much like this:

var obj = {};

for (var k = 0; k<10; k++) {
    obj['customkey'+k] = 'some'+k;
}

This would return:

obj {
    customkey0 : 'some0',
    customkey1 : 'some1',
    customkey2 : 'some2',
    ...
}

Keep in mind, an array: ['some1','some2'] is basicly like and object: { 0 : 'some1', 1 : 'some2' } Where an object replaces the "index" (0,1,etc) by a STRING key. Hope this helps.

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

Comments

1

push() is for use in arrays, but you're creating a object.

You can add properties to an object in a few different ways:

obj.one = some1;

or

obj['one'] = some1;

Comments

0

I would write a simple function like this:

 function pushVal(obj, value) {
     var index = Object.size(obj);
     //index is modified to be a string.
     obj[index] = value;
 }

Then in your code, when you want to add values to an object you can simply call:

  for(var i=0; i<someArray.length; i++) {
      pushVal(obj, someArray[i]);
   }

For info on the size function I used, see here. Note, it is possible to use the index from the for loop, however, if you wanted to add multiple arrays to this one object, my method prevents conflicting indices.

EDIT

Seeing that you changed your keys in your questions example, in order to create the object, you can use the following:

function pushVal(obj, value, key) {
     //index is modified to be a string.
     obj[key] = value;
 }

 or 

 obj[key] = value;     

I'm not sure how you determine your key value, so without that information, I can't write a solution to recreate the object, (as is, they appear random).

3 Comments

Sounds good, but you could go "prototype" for that matter. Something like Object.prototype.pushVal = function(index,value) { this[index] = value; return this; } and the you just have to go: var obj = {} obj.pushVal('some1','some value for some1'); The "return this" is only to make sure you can queue things up like this: obj.pushVal('1','test').pushVal('2','another test')
I avoid prototype simply because Object.prototype is sensitive to change and one should always be very careful when modifying it.
true. Actually this one would've been problematic but it was just to show some other possible solutions.

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.