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 initialiseselement['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
orobject
?element['obj'] = 'one';
create attrobj
onelement
, 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 doelement['obj']['bg'] === 'undefined'
it's equals to'one'['bg']
. I think It has no sense. – Sapikelio Commented Jul 9, 2015 at 10:25
5 Answers
Reset to default 2var 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
版权声明:本文标题:jquery - JavaScript check if array object exists or undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243421a2438977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论