将对象的json数组转换为其他格式

时间:2019-03-07 06:39:18

标签: javascript arrays json object

如何将以下示例响应数据转换为下面列出的所需JSON格式?谢谢。

我的逻辑

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

我当前的响应数据

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

所需的JSON格式

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 个答案:

答案 0 :(得分:6)

嗯,只是

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

答案 1 :(得分:3)

您可以使用map()返回所需的输出:

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

通过这种方式,您将返回具有title类型的string属性的对象数组。如果要将title值作为对象输出,则可以使用JSON.parse()解析该值:

const result = arr.map((item, index) => ({
  id: index,
  title: JSON.parse(item)
}));

答案 2 :(得分:2)

这并不困难,您必须像做的那样映射数组,然后返回新对象:

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

答案 3 :(得分:2)

使用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'}"
]
arr.forEach((e, i) => {
  arr[i] = {id:i,title:e};
})
console.log(arr)

答案 4 :(得分:2)

尝试以下代码,

因为如果您使用的是forEach或其他东西,将会影响您的代码性能。

如果您要处理大数据,我将推荐这种方式。如果数据较少,则使用上面的其他答案以更少的代码获得输出。

供您参考,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);

对于循环分析报告:https://github.com/dg92/Performance-Analysis-JS

答案 5 :(得分:1)

您可以使用.map,其中索引为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);

答案 6 :(得分:1)

使用currElement,index作为map的参数,然后返回{"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);

答案 7 :(得分: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);

答案 8 :(得分: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));