Reputation:
What is the problem with my code below ? I did map based on the key that i wanted to get but i get an undefined values?. I wanted to get each random code. It did return that there are 6 total random code but it is undefined. Any problem? Thank you.
let result = me.record.job_detail.questionaires.map(a => a.random_code);
result
Array(6) [ undefined, undefined, undefined, undefined, undefined, undefined ]
[ "{'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'}" ]
Upvotes: 1
Views: 63
Reputation: 17190
Another alternative (since you have strings and they are not valid JSON) could be looking on the string for the patron of your random_code
numbers using 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]));
However I don't recommend this unless you are really sure that there will not be data with similar format on the strings
.
Upvotes: 1
Reputation: 50759
me.record.job_detail.questionaires
is returning an array of strings. Not an array of objects. One solution would be to parse your string objects, however, at the moment they're not considered valid JSON, and so they cannot be passed using JSON.parse()
.
Thus, you can make your string valid JSON using .replace()
Using .replace
with 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);
Above, I am using .replace
to replace all single quotes ('
) with double quotes (so it can be parsed using JSON.parse()
) I am also then making sure all values (like None
) are strings, hence the second replace. Doing this will make sure your strings in your array are valid to be parsed which then allows you to access the .random_code
property.
Upvotes: 2