如何将以下示例响应数据转换为下面列出的所需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'}"]
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'}"
}
]
答案 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);
答案 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));