Checking if an array is empty with Coffeescript [duplicate] - javascript

This question already has answers here:
Checking if an array has some items in coffeescript
(3 answers)
Closed 8 years ago.
I am trying to figure out if an array is empty. I tried this and it does not work.
a = []
if a == []
alert "empty"
I know I can check the length of the array but am curious why this does not work.

There is no direct empty check for arrays. You could do something like this though:
a = []
alert("empty") unless a.length

Related

The same array has different lengths? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
console.log(array) returns filled array, but console.log(array.length) is 0? [duplicate]
(1 answer)
Closed 3 years ago.
Can someone please explain to me, because I don't understand something here, why the same array has two lengths? I assume I understand something wrong here, it must have the same length, right?
I have this call:
console.log(eventsBeforeRender);
console.log(eventsBeforeRender.length);
and in browser console i see this:
What I want to achieve, is to access the element [0] of the root array, so then from this element I can access other elements, of which from what I see there should be 17.
Calling this:
console.log(eventsBeforeRender[0]);
returns undefined.

Comparing two empty array not working in javascript [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
[] is not identical to [] [duplicate]
(4 answers)
Closed 4 years ago.
In my current project I am taking a request parameter with a value of either array or a string. But if I get an array it would be an empty array. So what I did is I checked the type first and then I worked with the value. But then I did something like this
const reqParam = []
if (reqParam === []) {
console.log('empty array')
} else {
console.log('string')
}
But reqParam despite of being an empty array is giving me false while comparing with []. Why it is behaving like this? Thanks in advance.
Your code creates two different arrays, that's why the comparison is returning false. === will only compare their references and not their content.

javascript differences between if (value in item) and if (item.hasOwnProperty(value)) [duplicate]

This question already has answers here:
if (key in object) or if(object.hasOwnProperty(key)
(9 answers)
Closed 4 years ago.
I just want to know if those 2 statements are equals or not, JS makes me mad and its behavior remain too esoteric. Within my app, it occurs some strange results ...
value in item and item.hasOwnProperty(value)
Thanks in advance
in also looks up the prototype tree while hasOwnProperty only checks the object itself.

Does an array contain a specific item? [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Check if an element is present in an array [duplicate]
(9 answers)
Closed 4 years ago.
I recently starting learning JS so a beginner level answer would be great! Thanks.
function contains(arr, item) {
// check to see if item is inside of arr
// return true if it is, otherwise return false
}
What about something like this?
const arr = [1,2,3];
const el = 2;
console.log(arr.includes(el)) // true

test for a propertie in JSON project to be empty? [duplicate]

This question already has answers here:
Testing for an empty array object in JSON with jQuery
(5 answers)
Closed 8 years ago.
I have the following JSON object that is passed to one of my functions …
{"action":"setProjectAll", "application":{"proId":"new","zoomPosition":35,"scrollerPosition":0,"language":"en","timelinePosition":0}, "clips":[]}
How can I test this object for the propertie "clip" to be "[]" (empty)?
Right now in the example above it is empty, however since the object is dynamic I need to test for that property if it contains values or not.
How can that be done?
thank you
How about
if (x.clips && x.clips.length === 0)

Categories

Resources