0

How do I convert the following sample response data to the desired JSON format listed below? Thank you.

My logic

arr = arr.map((e) => { e.title = JSON.parse(e.title.replace(/'/g, '"')).title; return e; })

My current response data

arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
     "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"]

Desired JSON format

arr = [
        {
            id: 0,
            title: "{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'YAHSHUA', 'instruction': '?', 'created_at': '2019-03-06'}",
        },
        {
            id: 1,
            title: "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Exam2', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"
        }
    ]
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Jhon Caylog
  • 483
  • 8
  • 24

9 Answers9

6

Um, just

 arr.map((title, id) => ({ title, id }))
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
3

You can use map() to return the desired output:

const arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
     "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"];
     
const result = arr.map((item, index) => ({
  id: index,
  title: item
}));

console.log(result);

In this way, you return an array of objects that have a title property of type string. If you want to output the title value as an object, you can parse the value with JSON.parse():

const result = arr.map((item, index) => ({
  id: index,
  title: JSON.parse(item)
}));
kapantzak
  • 11,610
  • 4
  • 39
  • 61
2

It's not difficult, you have to map the array as you were doing, and return the new object:

arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
     "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"]

mapped = arr.map((elem, index) => {
  return ({
    id: index,
    title: elem    
  });
});

console.log(mapped);
JV Lobo
  • 5,526
  • 8
  • 34
  • 55
2

Using forEach loop create an object with required properties and replace the original object in the array

var arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
  "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"
]
arr.forEach((e, i) => {
  arr[i] = {id:i,title:e};
})
console.log(arr)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
2

Try below code,

Because if you are using forEach or something it will affect your code performance.

I will recommend this way if you want to handle large data's. If you have less data then use other answers above to achieve your output with less code.

For your reference Javascript efficiency: 'for' vs 'forEach'.

var arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
  "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"
];

var result = [];

for(let i=0;i<arr.length;i++){
  result.push({id: i, title:arr[i]}); 
}

console.log(result);

For loop analysis report : https://github.com/dg92/Performance-Analysis-JS

Dinesh
  • 6,500
  • 10
  • 42
  • 77
1

You can use .map where the index is the id:

const arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}", "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"],

res = arr.map((title, id) => ({id, title}));
console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

use currElement, index as the parameter of map then return the new object of {"id":index,"title":currElement}

let arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
     "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"];
     
let newArr =arr.map((currElement, index) => {
  return {"id":index,"title":currElement};
});

console.log(newArr);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33
1
 arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
 "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"]

 for (var k = 0; k < arr.length; k++){
  arr[k] = {'id':k, 'title': arr[k] };
 }
 console.log(arr);
Murali Nepalli
  • 1,588
  • 8
  • 17
1
 var arr = ["{'department': 'YOWI', 'timelimit': '01:05:02', 'sub_title': 'PRE-EMPLOYMENT ASSESSMENT  TEST PART 8', 'id': 185, 'random_code': '50-76997961114', 'title': 'Hey', 'instruction': '?', 'created_at': '2019-03-06'}",
        "{'department': 'Department Exam 2', 'timelimit': '01:05:08', 'sub_title': 'Just a test exam 2', 'id': 142, 'random_code': '50-3910111611011', 'title': 'Assessment', 'instruction': 'Hey Jude', 'created_at': '2019-02-27'}"];

    var new_arr = [];

    for(var i=0; i< arr.length; i++){
        new_arr[i] = {'id':i, 'title':arr[i]}
    }

    console.log(JSON.stringify(new_arr));
Kausha Shah
  • 309
  • 3
  • 9