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'}"
        }
    ]

9 Answers 9

6

Um, just

 arr.map((title, id) => ({ title, id }))
Sign up to request clarification or add additional context in comments.

Comments

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)
}));

Comments

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

Comments

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);

Comments

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)

Comments

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);

Comments

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);

Comments

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);

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.