Syn*_*rik 2 javascript json node.js
I am trying to check in JavaScript if a value from a json is an empty list or not. I tried obviously json.bundleProductSummaries != []. This doesn't work, however this works:
JSON.stringify(json.bundleProductSummaries) != JSON.stringify([])
Run Code Online (Sandbox Code Playgroud)
My json :
{
"bundleProductSummaries": [
]
}
Run Code Online (Sandbox Code Playgroud)
My code :
fs = require('fs')
body = fs.readFileSync('./json.txt',{encoding : 'utf-8'})
// ---- Parsing de body ----
let json = JSON.parse(body);
let colored = null;
if(json.bundleProductSummaries != []){
json = json.bundleProductSummaries[0];
}
Run Code Online (Sandbox Code Playgroud)
Thank you for your future answers ! Syndorik
You need to check length
json.bundleProductSummaries.length === 0
Run Code Online (Sandbox Code Playgroud)
Because in JS
json.bundleProductSummaries.length === 0
Run Code Online (Sandbox Code Playgroud)
as they are two different memory reference.
But when two array will evaluate to true ?
When they both the value you're comparing is having same reference
console.log([] === [] )Run Code Online (Sandbox Code Playgroud)