ThomasReggi
ThomasReggi

Reputation: 59345

Get values types like similar to JSON.stringify

If I console.log my array I get this:

[ 'query', undefined, [Function] ]

However if I JSON.stringify the array I get

['query',null,null]

In most cases this is exactly what you want when converting JSON.

However I was wondering if there was a way to capture a string or array of types as in the first array.

someFunc(arr) // '[ 'query', undefined, [Function] ]' (one big string)
someFunc(arr) // [ 'query', 'undefined', '[Function]' ] (array of strings)

Upvotes: 0

Views: 91

Answers (1)

gibatronic
gibatronic

Reputation: 1981

Well, you can just map the items like this:

['query', undefined, function() { }].map(function(item) {
  if (item instanceof Function) return '[Function]';
  if (item === null) return 'null';
  if (item === undefined) return 'undefined';
  return item.toString();
});

Upvotes: 2

Related Questions