Currently I have a PHP page returning some values. The data is something like this:
08-30-2018, in
08-29-2018, out
08-28-2018, in
08-27-2018, in
How can I create a custom array in Javascript with the values above to be similar as this array below:
var system = [
['08-30-2018', 'in'],
['08-29-2018', 'out'],
['08-28-2018', 'in'],
['08-27-2018', 'in']
];
I have tried array.push
, but it does not create an array like above. What should I do? Can you help me? Thank you!
You can use multi-dimensional arrays in JavaScript
var system = []; var output = "08-30-2018, in\\n08-29-2018, out\\n08-28-2018, in\\n08-27-2018, in"; var items = output.split("\\n"); for(i=0; i<items.length; i++){ var data = items[i].split(","); var item = []; item.push(data[0].trim()); item.push(data[1].trim()); system.push(item); } console.log(system);
You could also parse this kind of input using regular expressions:
const input = '08-30-2018, in\n08-29-2018, out\n08-28-2018, in\n08-27-2018, in';
const regex = /(\d{2}-\d{2}-\d{4}), (in|out)/g;
let system = [];
let match;
while ((match = regex.exec(input)) !== null) {
system.push([match[1], match[2]]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.