I have this Array ["2.900000F02A_1313_01","2.600000F02A_1315_03","2.900000F02A_1354_01"]
And I want to split it like this:
JavaScript
x
6
1
[
2
{"name":"F02A_1313_01", "Voltage":"2.900000"},
3
{"name":"F02A_1315_03", "Voltage":"2.600000"},
4
{"name":"F02A_1354_01", "Voltage":"2.900000"}
5
]
6
This is my Code that doesn’t work:
JavaScript
1
4
1
for (var i in msg.strg) {
2
array.push(i.split(/[a-zA-Z].*/g));
3
}
4
Does somebody know how I can do this?
Advertisement
Answer
You could split with a group.
JavaScript
1
8
1
const
2
data = ["2.900000F02A_1313_01", "2.600000F02A_1315_03", "2.900000F02A_1354_01"],
3
result = data.map(string => {
4
const [Voltage, name] = string.split(/([a-z].*$)/i);
5
return { name, Voltage };
6
});
7
8
console.log(result);