8

i have an array

data = [38589, 3, __ob__: Observer];

i want to send a put request API with the body sending this array. and got a 400 error? is it this __ob__: Observer thing is being a problem for me to sending this data? if it's the one that affect the error, can i know how to remove it from my array?

Khairul Habib
  • 452
  • 1
  • 12
  • 29
  • Does this answer your question? [Vue JS returns \[\_\_ob\_\_: Observer\] data instead of my array of objects](https://stackoverflow.com/questions/52873516/vue-js-returns-ob-observer-data-instead-of-my-array-of-objects) – T. Short Jan 06 '20 at 10:46
  • It probably isn't the `__ob__` that is causing your problem. You can inspect the details of the request using the Network tab of your browser's developer tools. That should allow you to see exactly what is being sent and from there you can figure out what the problem is. – skirtle Jan 06 '20 at 10:49

3 Answers3

13

You can convert it to JSON and then back, if you want to get the final value of the array without the observer:

const finalData = JSON.parse(JSON.stringify(data));
Terry
  • 63,248
  • 15
  • 96
  • 118
  • 2
    I don't know if anyone tried to measure which method was more efficient, so I used a simple comparison model. If someone confirms it in a more appropriate way, it would be welcome. My test criterion was to compare between 3 ways I know how to convert: `Object.assign`, `JSON.stringify into JSON.parse`, `const unwrapped = { ...this.vars }`. It looping 100000 iterations, in my computer `const unwrapped = { ...this.vars }` it was much faster than the others, a total of `1462.4199999962002 ms`. The others, `5084.745000000112 ms` and `8934.004999988247 ms` respectively. – Magno Alberto Feb 06 '21 at 18:47
  • What about nested objects and arrays? Does it work? – Petar Vasilev Sep 30 '22 at 12:46
8

I know this is already 3 months old but you also can use this

const final_data = Array.from(data);
qreidt
  • 91
  • 1
  • 3
  • 4
    This did not work for me, nor did the spread operator or object.assign(). The accepted answer was the only one I could get to work. – Brent Nov 04 '20 at 20:40
1

You can do something like this:

const finalData = { ...data }
devielu
  • 188
  • 1
  • 2
  • 15