admin管理员组

文章数量:1327996

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

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

Share Improve this question edited Jan 6, 2020 at 20:14 Goran Stoyanov 2,3111 gold badge22 silver badges32 bronze badges asked Jul 9, 2015 at 10:07 tonoslfxtonoslfx 3,44216 gold badges68 silver badges107 bronze badges 5
  • How about element.push({'bg':'red'}); ? It's a bit unclear what you're asking. Could you be more specific? – Daniel B 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)? – SD. 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. – fuyushimoya Commented Jul 9, 2015 at 10:14
  • What structure are you expecting? – lshettyl 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. – Sapikelio Commented Jul 9, 2015 at 10:25
Add a ment  | 

5 Answers 5

Reset to default 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');
}

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.

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');
}
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/o66uhd05/3/

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

本文标签: jqueryJavaScript check if array object exists or undefinedStack Overflow