About JavaScript console.log

Asked 1 years ago, Updated 1 years ago, 56 views

As for js, I am not good at taking out values as I expected when displaying objects and arrays in console.log.
For example, if there is an array called test and the following data is displayed in concole.log,

0:Item
attributes:
1—Item
attributes:
.
.

I'd like to check if the attributes value is null, what should I do?
Or, I'd like to know if there's any good way to think about it because it stumbles with an array or object.
The operating system is macOS High Sierra. The browser is located in chrome69.0.3497.92
That's it

javascript array

2022-09-30 17:33

2 Answers

For your information:

letobj={};
obj.attr_1 = null;
obj.attr_2 = 2;
// check if obj has each attribute
obj.hasOwnProperty("attr_1")?console.log("obj has attr_1"):console.log("obj doesn't have attr_1");
obj.hasOwnProperty("attr_2")?console.log("obj has attr_2"):console.log("obj does not have attr_2");
obj.hasOwnProperty("attr_3")?console.log("obj has attr_3"):console.log("obj does not have attr_3");
// Check if each attribute of obj is null
obj.attr_1 === null?console.log("attr_1 is null"):console.log("attr_1 is not null");
obj.attr_2===null?console.log("attr_2is null")—console.log("attr_2is not null");
obj.attr_3 === null?console.log("attr_3 is null"):console.log("attr_3 is not null"; 


2022-09-30 17:33

I'm not sure, so
I reproduced the contents of the Console in the questionnaire and compared them in IF.
Isn't it like this?

function Item() {this.attributes;}
// Initialize
vartest={};
for(varn=0;n<10;n++){
    test[n] = new Item();
    test[n].attributes=n;
}
console.log(test);
// Demo Null
test[3].attributes=null;
console.log(test);
// Check Values
if(null==test[0].attributes) {console.log("0 is null");}
else {console.log("0 is Not null");}
if(null==test[3].attributes) {console.log("3 is null");}
else {console.log("3 is Not null";}


2022-09-30 17:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.