I want a solution to extract object & push into an array. but can not able to extract object because
const data = [ "userData": ["{ name: 'Test 1', age: '25'}, { name: 'Test 2', age: '26'}, { name: 'Test 3', age: '27'},"] ]
Tried code,
const test = data[0]['userData'];
const converted = JSON.parse(test);
Above code doesn't work.
Expected Output:
[
{
name: 'Test 1',
age: '25'
},
{
name: 'Test 2',
age: '26'
},
{
name: 'Test 3',
age: '27'
},
]
JSON.parseonly accepts strings, and you are passing an array of strings. You must iterate through the array of strings and parse each element individually, and then push each result to an array.mapanswer should do the trick for you. I'm not going to add the exact code so you try to understand what is happening and how to overcome it, as an excercise, as it seems that you are learning and that's important ;-)