TransWikia.com

How to convet this data to array?(Typescript )

Stack Overflow Asked by Pyarel on December 23, 2021

I have data coming from api where one of the attribute has value like,

[{
......
attr_name: "["abcd"]"
attr_names:"{abcd}"
.......
}]

How do i convert this value to array ["abcd"] ?
Thank you in advance.

One Answer

Try with JSON.parse which will convert json string to object. Loop over data array and convert it and assign back it itself as below.

data.forEach(d => d.attr_name = JSON.parse(d.attr_name));

let data = [{
  attr_name: '["abcd"]',
  attr_names: "{abcd}"
}];

data.forEach(d => d.attr_name = JSON.parse(d.attr_name));
console.log(data);

Answered by Karan on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP