最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert a json array of objects into a different format - Stack Overflow

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

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'}"
        }
    ]
Share Improve this question edited Mar 9, 2019 at 21:43 georgeawg 49k13 gold badges77 silver badges98 bronze badges asked Mar 7, 2019 at 6:39 Jhon CaylogJhon Caylog 5018 silver badges25 bronze badges
Add a ment  | 

9 Answers 9

Reset to default 6

Um, just

 arr.map((title, id) => ({ title, id }))

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

Try below code,

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

I will remend 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./dg92/Performance-Analysis-JS

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

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)

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

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

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

 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));
发布评论

评论列表(0)

  1. 暂无评论