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.

Adeel Imran
  • 8,751
  • 5
  • 45
  • 71
  • 1
    FWIW, if an array contains an object then it is not empty. – Felix Kling Dec 20 '17 at 14:58
  • one way `JSON.stringify(response) === '[{}]'` – amd Dec 20 '17 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` – dhilt Dec 20 '17 at 15:00
  • See https://coderwall.com/p/_g3x9q/how-to-check-if-javascript-object-is-empty – PM 77-1 Dec 20 '17 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. :) – Adeel Imran Dec 20 '17 at 15:01
  • Setting this kind of single element array with an empty object is something I’ve never done, it feels like bad practice for some reason. You will be forever referring to response[0] in your code. imo response should either be an empty array or empty object which is later wrapped in an array. – James Dec 20 '17 at 15:02
  • 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. – Felix Kling Dec 20 '17 at 15:03
  • Yes sir I do know on how to do that @FelixKling I just wanted to see what was the best way on how to do that. – Adeel Imran Dec 20 '17 at 15:05
  • 1
    `arr.length === 1 && isEmptyObject(arr[0])` where `isEmptyObject` is an implementation suggested in the other question. – Felix Kling Dec 20 '17 at 15:07
  • Thank you. Yes I have checked. I think that does help me. – Adeel Imran Dec 20 '17 at 15:08
  • I always try to split a problem into sub-problems and search for solutions for those first (such as how to test whether an object is empty in this case). Sure, there might be "better" solutions for the complete problem, but I might get to a solution faster by solving the sub-problems first. – Felix Kling Dec 20 '17 at 15:12
  • Sir I am new to the industry, a recent graduate. Just still learning on how to be a great problem solver. I do know I have a very long way to go. I strongly believe you can achieve greatness if you have a mentor. I am a self taught dev. So just trying to learn on my own as much as I can. @FelixKling – Adeel Imran Dec 20 '17 at 15:17

3 Answers3

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.

Luan Nico
  • 4,792
  • 2
  • 27
  • 50
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
Ayush Gupta
  • 6,976
  • 5
  • 43
  • 78
  • 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 – Luan Nico Dec 20 '17 at 15:09
0

How about:

Object.keys(response[0] || {}).length === 0
Ivan Velichko
  • 5,581
  • 3
  • 35
  • 72