0

I am trying to parse string to object array in my React application.

const {newsEntity} = props;
const contentInString = {newsEntity.content}; <== not working
// const contents = JSON.parse(contentInString); <== hoping to use this someday

I got the following error ESLint: Parsing error: ',' expected.

I tried to removed the curly braces but it gives undefined

Content :

[{"content":"Umi Kalsum berharap kondisinya tetap baik","type":"text"},{"content":"Dream - Setelah menempuh perjalanan darat cukup panjang untuk menemui wanita diduga telah menghina keluarganya, Umi Kalsum dan Ayah Rozak akhirnya kembali rumahnya di Depok, Jawa Barat. Perjalanan panjang itu ternyata menguras tenaga orang tua Ayu Ting Ting tersebut.","type":"text"}

The content of {newsEntity} I notice only visible during the rendering

return (<div> {newsEntity.content}</div>  );
abiieez
  • 3,139
  • 14
  • 57
  • 110
  • 1
    Can you show example data of `newsEntity.content`? – Viet Jul 31 '21 at 13:57
  • do you have value in `newsEntity` ? – Amruth Jul 31 '21 at 13:58
  • `{newsEntity.content}` is a syntax error. Perhaps you meant `newsEntity.content`? – jtbandes Jul 31 '21 at 13:58
  • looks like you have to use map since format is array, so that you can render – Amruth Jul 31 '21 at 14:06
  • Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – VLAZ Jul 31 '21 at 14:14
  • In a way that is similar to my question, but for context this issue appeared during rendering when the value initially is not defined therefore `?` needed to safeguard it. – abiieez Jul 31 '21 at 15:02

3 Answers3

2

const contentInString = {newsEntity.content}; this leads to syntax error.

you should extract this way const contentInString = newsEntity?.content

Amruth
  • 5,792
  • 2
  • 28
  • 41
2

If you want to assign new object use:

const contentInString = {content: newsEntity.content};

If you want to get content from newsEntity use:

const contentInString = newsEntity.content;

Regarding the last part of the question - that's TypeScript error and it gives you the hint that something went wrong with your types. You can or

  • create new variable and type will be inferred automatically
  • Or fix the type manually
Drag13
  • 5,859
  • 1
  • 18
  • 42
  • btw when I tried to parse using JSON it gives me `Argument of type '{ content: string; }' is not assignable to parameter of type 'string'.` – abiieez Jul 31 '21 at 14:07
  • That's TypeScript complains about incorrect types. It's saying that assigning object is not allowed into string variable. Simply fix the type of the variable or create new one – Drag13 Jul 31 '21 at 14:21
0
//Checkout this hopefully this will help you
let GblVar=100;  
 
function Fn() {          //function
 
  console.log(GblVar)
 
  if (true) {         //code block
    console.log(GblVar)
  }
 
  function nested() {         //nested function within someFn1
    console.log(GblVar)
  }
 
}
 
for (let c = 0; c < 3; c++){    //code block
  console.log(GblVar);
}
 
function OthrFn1() {       //another function
  console.log(GblVar)
}
 
console.log(GblVar)