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');
}
Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • How about `element.push({'bg':'red'});` ? It's a bit unclear what you're asking. Could you be more specific? – Daniel B Jul 09 '15 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)? – SDekov Jul 09 '15 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. – fuyushimoya Jul 09 '15 at 10:14
  • What structure are you expecting? – lshettyl Jul 09 '15 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. – Sapikelio Jul 09 '15 at 10:25

5 Answers5

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.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
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');
}
R3tep
  • 12,512
  • 10
  • 48
  • 75
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');
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
buræquete
  • 14,226
  • 4
  • 44
  • 89
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/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
NiRUS
  • 3,901
  • 2
  • 24
  • 50
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');
}
Adersh M
  • 596
  • 3
  • 19