-4
[ { id: '5b3a223296fb381a29cf6fd9',
number: 1,
name: 'Tablet White EliteBook  Revolve 810 G2',
dprice: '0',
image: '' } ]

This is a response from the angular application. When I checked for its type, it is giving result 'object'.

var savedCart = JSON.parse(req.body.cart);

When I am using this query, it is still an 'object'. How can I convert this to an array?

Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
  • That is an array with 1 element that is an object. `typeof new Array()` does return object, as that's what an array is. – Reinstate Monica Cellio Jul 04 '18 at 08:17
  • 3
    its an array and arrays are objects. If you want to check if its an array use `Array.isArray(variable)` – Fuzzyma Jul 04 '18 at 08:17
  • This *is* an array. However, an array is also an object, so if you did `typeof savedCart` you'd get `object`. https://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – VLAZ Jul 04 '18 at 08:18
  • 1
    `typeof []` === `"object"` use `Array.isArray([])` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray – Francis Leigh Jul 04 '18 at 08:18
  • Then how you will maintain the key and value pair? I mean you can store only values or properties in the array! – Rohit Sharma Jul 04 '18 at 08:20
  • 1
    How you wish to keep it in an array? Share expected output please – Isaac Jul 04 '18 at 08:21
  • What do you want to do? I see that you are using JSON.parse() which expects a string as a parameter, but you already have the JSON object in the array. What is your final goal? What are you going to to with savedCart? – svet Jul 04 '18 at 08:36
  • I want to use it in exact same way, that the response is. The issue here is when I am using array specific functions like reduce on this, it is giving a message that this isn't a function – Mayank Nauriyal Jul 04 '18 at 08:38
  • This will get put on [Hold] if you do not specify exactly what you need. Such expected output, or the ideology. Refer to [`How do I ask a good question?`](https://stackoverflow.com/help/how-to-ask) – Alex Jul 04 '18 at 08:41

1 Answers1

0

Unfortunately in JavaScript both objects and arrays have the typeof of 'object'. In fact even the NULL value has the typeof of 'object', what makes things even worse. If you want to distinguish all those "wannabe-objects" existing in JS, you should use something like:

function smartTypeOf(x) {
    if (typeof x !== 'object') return typeof x;
    if (x === null) return 'null';
    if (Array.isArray(x)) return 'array';
    return 'object';
}

You may alternatively want to use instanceof instead, e.g.:

if (x instanceof Array) return 'array';
if (x instanceof Promise) return 'promise';
if (x instanceof YourCustomClassInstance) return 'yourcustomclassinstance'

By the way, if it ever happened that you have something being an object and want an array (it is not case here, thou!):

  • If the object is iterable, e.g. a Set, or an arguments instance:

    var x = new Set([1, 2, 1]);
    Array.from(x); // [1, 2]
    
  • If the object is any (non-NULL) object and you want array of its properties values or keys respectively:

    var x = {a: 2, b: 4, c: 7};
    Object.values(x); // [2, 4, 7]
    Object.keys(x); // ['a', 'b', 'c']
    
jaboja
  • 2,178
  • 1
  • 21
  • 35