3

I have a variable which is as follows.

let response = [
 {}
];

Which is an array with an empty object. What kind of check should I put in place to determine if this response is exactly equal to this. i.e,

[{}] === response; // returns false

I want to have a check that returns false if the response is [{}] how do i do that.

There are already questions on stack-overflow which asks on how to check for an empty object. My Question is that how do I check for an array with an empty object. So completely different. This is no duplicate of any question.

12
  • 1
    FWIW, if an array contains an object then it is not empty. Commented Dec 20, 2017 at 14:58
  • 1
    I would try something like that: Array.isArray(response) && response.length === 1 && typeof response[1] === 'object' && Object.keys(response[1]).length === 0 Commented Dec 20, 2017 at 15:00
  • 1
    @FelixKling this is not a duplicate of that question. As this question ask an empty object inside an array. That question asks only for an empty object. :) Commented Dec 20, 2017 at 15:01
  • 2
    @AdeelImran: I would expect that you know how to check the length of the array and how to access the first element :) "So completely different." Not quite. Checking the emptiness of an object is a part of your problem. Commented Dec 20, 2017 at 15:03
  • 1
    arr.length === 1 && isEmptyObject(arr[0]) where isEmptyObject is an implementation suggested in the other question. Commented Dec 20, 2017 at 15:07

3 Answers 3

1

First check if it is an array, with Array.isArray, then check the .length to see if it's one, then check if the first element is an empty object (typeof and Object.keys().length === 0). That way you'll be pretty sure it's what you wanted.

Another option is to use json: JSON.stringify(response) ==== '[{}]'. Just beware of cyclic dependencies in response.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir. This helped.
just an FYI, typeof [] is object, and Object.keys([]).length is 0, so you might want to do a different check for object
1

You can simply do:

let response = [{}];

console.log(JSON.stringify([{}]) === JSON.stringify([{}])); // true,

But if your response is cyclic, this will fail.

Another way you can do is:

let response = [{}];


console.log(
  Array.isArray(response) &&
    response.length === 1 &&
    Object.prototype.toString.call(response[0]) === '[object Object]' &&
    Object.getOwnPropertyNames(response[0]).length === 0
); // true

1 Comment

For the cyclic problem, I believe there is an option for stringify that will go just n-levels deep, fixing the problem (if the structure is cyclic, it certainly going to be different 2-level deep anyway). I'd need to look it up, though
0

How about:

Object.keys(response[0] || {}).length === 0

4 Comments

It could be an array with more than one element, but might work for most use-cases ;)
Wont work in case of [[]] as Object.keys([]).length is 0
@AyushGupta But that's not what the OP is asking for.
Perhaps, but if the OP needs to check for that response exactly, and [[]] is a valid response , yours wont be 100% reliable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.