Home > Net >  How to add double quote with number when getting from API in react js?
How to add double quote with number when getting from API in react js?

Time:10-04

I'm getting JSON data from a API but in that API I'm getting number without quote now I need to add quotes how can I added quotes with numbers?

Right now I'm getting:-

[{"name":"Hakim","age":32},{"name":"Girish","age":35}

I want to get:-

[{"name":"Hakim","age":"32"},{"name":"Girish","age":"35"}

Demo URL:

https://codesandbox.io/s/double-quote-issue-nkhkye?file=/src/App.js:0-555

My Code:-

import React from "react";
import "./styles.css";
const real_data = [
  {
    name: "Hakim",
    age: 32
  },
  {
    name: "Girish",
    age: 35
  }
];
const update_data = JSON.stringify(real_data);
console.log(update_data);
export default function App() {
  //  const [data, setData] = React.useState([]);
  // setData(real_data);
  return (
    <div className="App">
      {/* {data
        ? data.map((opt) => (
            <h2 key={opt.age} value={opt.age}>
              {opt.name}
            </h2>
          ))
        : ""} */}
    </div>
  );
}

Thanks for your efforts!

CodePudding user response:

const stringAges = real_data.map(e => ({...e, age: e.age   '' }));
  • Related