3

How do I add push a new object into the array if it doesn't exist?

I check this link: How to check if array element exists or not in javascript?

but not sure how to push the new object though!

var element = [];

element['obj'] = 'one';

if (typeof element['obj']['bg'] === 'undefined') {

  console.log('not defined');

  element['obj']['bg'] = 'red';

  console.log(element);

} else {
  console.log('defined');
}

5
  • How about element.push({'bg':'red'}); ? It's a bit unclear what you're asking. Could you be more specific? Commented Jul 9, 2015 at 10:11
  • What exactly are you trying to achieve? element['obj'] = 'one'; --> this initialises element['obj'] to string, and then you want to add a key/value pair to that (string)? Commented Jul 9, 2015 at 10:12
  • 1
    You want to use array or object? element['obj'] = 'one'; create attr obj on element, but its not account to length. Commented Jul 9, 2015 at 10:14
  • What structure are you expecting? Commented Jul 9, 2015 at 10:15
  • I don't understand what your code do. But if element['obj'] = 'one', when you do element['obj']['bg'] === 'undefined' it's equals to 'one'['bg']. I think It has no sense. Commented Jul 9, 2015 at 10:25

5 Answers 5

2

var element = []; defines an array and not an object. To push a new value into an array you need to use the push method :

element.push({'obj' : 'one'});

But I think you do not need to create an array here, but just create an object. Declare your object like var element = {};

Like this the line element['obj'] = 'one'; works, you have an object with the key obj and the value one.

When you write element['obj']['bg'] you try to access on an object inside an object. So before set the value red into you need create the object :

element['obj'] = {};
element['obj']['bg'] = 'red';

Full example :

var element = {};

element['obj'] = {};

if (typeof element['obj']['bg'] === 'undefined') {

  console.log('not defined');

  element['obj']['bg'] = 'red';

  console.log(element);

} else {
  console.log('defined');
}

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

Comments

2

The element is of type string, not an object or an array.

Change the particular variable to an array:

var element = {};
element['obj'] = ['one'];
if ( typeof element['obj']['bg'] === 'undefined' ) {
    console.log('not defined');
    element['obj']['bg'] = 'red';
    console.log(element);
} else {
    console.log('defined');
}

Or better an object:

element['obj'] = {};
element['obj']['id'] = 'one';

The string objects are immutable objects.

3 Comments

But is set bg on an array is a good practice? Maybe we should wait OP to explain about what structure he expect.
@fuyushimoya Understand the situation... He's just setting a value. That's all! The OP doesn't mean any CSS or specific stuff here. :)
Yeah.. I agree you, this is the expect of the OP from his code.
1

Try inserting an empty array beforehand;

var element = [];

element['obj'] = 'one';

if ( typeof element['obj']['bg'] === 'undefined' ) {

    console.log('not defined');

    element['obj'] = [element['obj']];

    element['obj']['bg'] = 'red';

    console.log( element);

} else {
    console.log('defined');
}

5 Comments

Original value goes off!
But you cannot have both element['obj'] = 'one'; and element['obj']['bg'] = 'red';
You can have in a way... See my answer.
First one means the "obj" indexed value is a string, and the second one requires it to be an array. You have to choose!
I updated your answer to save the original value. If not, just revert it. Thanks.
1
var element = [];

element['obj'] = 'one';

if ( typeof element['obj']['bg'] === 'undefined' ) {

    console.log('not defined');

    element['obj'] = {'bg':'red'};

    console.log("My value:"+element['obj']['bg'] );

} else {
    console.log('defined');
}

http://jsfiddle.net/o66uhd05/3/

Comments

0

You could try this way.

var element = [],item = [];

item['obj'] = 'one';
element.push(item);

if ( typeof element['obj']['bg'] === 'undefined' ) {

    console.log('not defined');

    item['bg']='red';

    element.push(item);

    console.log( element);

} else {
    console.log('defined');
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.