下面的代码有什么问题?我是根据想要获取的键进行映射的,但是却得到了未定义的值?我想获得每个随机代码。它确实返回了总共6个随机代码,但未定义。任何问题?谢谢。
let result = me.record.job_detail.questionaires.map(a => a.random_code);
结果
Array(6) [ undefined, undefined, undefined, undefined, undefined, undefined ]
[“ {'created_at':'2019-02-07','department':'sme','timelimit':None,'id':16,'sub_title':'这是考试',' title':'exam','random_code':'50 -49361157339'}“,” {'created_at':'2019-02-07','department':'TEest DEP','timelimit':None,'id ':10,'sub_title':'这是一个测试','title':'Hello','random_code':'50 -612511266113'}“,” {'created_at':'2019-02-07','部门”:“ SYSTECH”,“时间限制”:无,“ id”:9,“ sub_title”:“正在尝试测试”,“ title”:“ Test 101”,“ random_code”:“ 50-8381637318'}”, “ {'created_at':'2019-02-07','department':'SME','timelimit':None,'id':8,8,'sub_title':'just to test','title':'Exam ','random_code':'50 -6819378387'}“,” {'created_at':'2019-02-08','department':'test','timelimit':None,'id':35,'sub_title ':'test','title':'test','random_code':'50 -2143566173'}“,” {'created_at':'2019-02-07','department':'Test','timelimit ':无,'id':13,'sub_title':'test','title':'test','random_code':'50-2961010112644'}“]
答案 0 :(得分:2)
me.record.job_detail.questionaires
返回一个字符串数组。不是对象数组。一种解决方案是解析您的字符串对象,但是,目前还不认为它们是有效的JSON,因此无法使用JSON.parse()
来传递它们。
因此,您可以使用.replace()
将.replace
与JSON.parse()
一起使用:
let arr = ["{'created_at': '2019-02-07', 'department': 'sme', 'timelimit': None, 'id': 16, 'sub_title': 'this is an exam', 'title': 'exam', 'random_code': '50-49361157339'}", "{'created_at': '2019-02-07', 'department': 'TEest DEP', 'timelimit': None, 'id': 10, 'sub_title': 'this is a test', 'title': 'Hello', 'random_code': '50-612511266113'}", "{'created_at': '2019-02-07', 'department': 'SYSTECH', 'timelimit': None, 'id': 9, 'sub_title': 'Trying to test', 'title': 'Test 101', 'random_code': '50-8381637318'}", "{'created_at': '2019-02-07', 'department': 'SME', 'timelimit': None, 'id': 8, 'sub_title': 'just to test', 'title': 'Exam', 'random_code': '50-6819378387'}", "{'created_at': '2019-02-08', 'department': 'test', 'timelimit': None, 'id': 35, 'sub_title': 'test', 'title': 'test', 'random_code': '50-2143566173'}", "{'created_at': '2019-02-07', 'department': 'Test', 'timelimit': None, 'id': 13, 'sub_title': 'test', 'title': 'test', 'random_code': '50-2961010112644'}"];
let result = arr.map(a => JSON.parse(a.replace(/'/g, '"').replace(/: (\w+)/g, ': "$1"')).random_code);
console.log(result);
以上,我正在使用.replace
用双引号替换所有单引号('
)(以便可以使用JSON.parse()
进行解析)然后我还要确保所有值(如None
)是字符串,因此第二个替换。这样做将确保数组中的字符串有效以进行解析,然后可以访问.random_code
属性。
答案 1 :(得分:2)
另一种选择(因为您有字符串,并且它们不是有效的JSON)可以使用random_code
在您的match()
数字顾客的字符串上查找:
const input = [
"{'created_at': '2019-02-07', 'department': 'sme', 'timelimit': 'None', 'id': 16, 'sub_title': 'this is an exam', 'title': 'exam', 'random_code': '50-49361157339'}",
"{'created_at': '2019-02-07', 'department': 'TEest DEP', 'timelimit': 'None', 'id': 10, 'sub_title': 'this is a test', 'title': 'Hello', 'random_code': '50-612511266113'}",
"{'created_at': '2019-02-07', 'department': 'SYSTECH', 'timelimit': 'None', 'id': 9, 'sub_title': 'Trying to test', 'title': 'Test 101', 'random_code': '50-8381637318'}",
"{'created_at': '2019-02-07', 'department': 'SME', 'timelimit': 'None', 'id': 8, 'sub_title': 'just to test', 'title': 'Exam', 'random_code': '50-6819378387'}",
"{'created_at': '2019-02-08', 'department': 'test', 'timelimit': 'None', 'id': 35, 'sub_title': 'test', 'title': 'test', 'random_code': '50-2143566173'}",
"{'created_at': '2019-02-07', 'department': 'Test', 'timelimit': 'None', 'id': 13, 'sub_title': 'test', 'title': 'test', 'random_code': '50-2961010112644'}"
];
console.log(input.map(x => x.match(/\d+-\d{3,}/g)[0]));
但是,除非您确实确定strings
上不会有类似格式的数据,否则我不建议您这样做。