Skip to content Skip to sidebar Skip to footer

How To Convert String To Array In React Js?

I have a JSON object that I cannot handle without Uncaught SyntaxError: Unexpected token o in JSON at position 1. So i made a string out of it var array = JSON.stringify(this.props

Solution 1:

You already have an array of objects. typeof the variable is returning Object because Arrays are Objects.

So you can do whatever you want with the array like map, filter, etc.

Solution 2:

I've figured out that the JSON is already valid, I was just too confused and irritated by the errors with different usages of Object.keys etc.!

I've solved the issue with:

const arr = Object.entries(this.props.user);
const mapped = [];
arr.forEach(([key, value]) => mapped.push(value));

This way I am cutting out the {} entries inside the object and saving them to an array, with which I can map and render the data inside as follows:

..

return(
<div>
 {mapped.map((user)=>
   <div>
     <b>{user.name}</b>

...

Post a Comment for "How To Convert String To Array In React Js?"