I have a following string in javascript:
var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114";
I want to fetch the numbers and store it in an array.
I tried following code:
var x=xyz.match(/\d+/g);
And I got the following output:
0: "429"
1: "100"
2: "504"
3: "5"
4: "100"
5: "504"
6: "5"
7: "106"
8: "580"
9: "106"
10: "570"
11: "98"
12: "580"
13: "106"
14: "570"
15: "114"
As you can see the floating point values such as 504.5 has come up seperately.
How can I fetch this properly?
You can simply change your regex to this one :
var x=xyz.match(/[0-9.]+/g);
It will allow you to capture the number and the float as well.
=> http://www.regexr.com/3b46a
You can change your regEx to this to get floating point values also
var x = xyz.match(/\d+\.*\d/g)
Try this one.
var x=xyz.match(/\d+(\.\d+)?/g);
Related
I have this string block with a unique structure:
ABBV08E201902162221 99927781608
ABH5684529845212JB19015037B0984565HCPP
ABTB034T 0084300130310325294 789657
ABTC095C 0084399845285165654 852
ABE05190000000752
Each line is broken down further. For example:
Line 1 "ABBV08E201902162221 99927781608" would be:
ID Type 1: A
ID Type 2: BB
ID Type 3: V08
ID Type 4: E
ID Type 5: 20190216
ID Type 6: 2221
ID Type 7: 999277
ID Type 8: 81608
Line 2 "ABH5684529845212JB19015037B0984565HCPP" would be:
ID Type 1: A
ID Type 2: BH
ID Type 3: 568452
ID Type 4: 9845212
ID Type 5: JB1
ID Type 6: 9015037
ID Type 7: B09
ID Type 8: 4565
ID Type 9: HCPP
Line 3 "ABTB034T 0084300130310325294 789657" would be:
ID Type 1: A
ID Type 2: BH
ID Type 3: TB
ID Type 4: 034T
ID Type 5: 0084
ID Type 6: 3001
ID Type 7: 30310325
ID Type 8: 294
ID Type 9: 789
ID Type 10: 657
Line 4 "ABTC095C 0084399845285165654 852" would be:
ID Type 1: A
ID Type 2: BH
ID Type 3: TC0
ID Type 4: 95C
ID Type 5: 008439
ID Type 6: 9845
ID Type 7: 285165
ID Type 8: 654
ID Type 9: 852
Line 5 "ABE05190000000752" would be:
ID Type 1: A
ID Type 2: BH
ID Type 3: E
ID Type 4: 051900
ID Type 5: 00000
ID Type 6: 752
Note: There could also be multiple blocks with different values back to back. NOTE that the block ends withe "ABE" string.
ABBV08E201902162221 99927781608
ABH5684529845212JB19015037B0984565HCPP
ABTB034T 0084300130310325294 789657
ABTC095C 0084399845285165654 852
ABE05190000000752
ABBV09E333302437771 00027521741
ABH6813597541492QC85469872B6512368HCPP
ABTB852T 7594597557282746984 854
ABTC000C 2172625769517957617 866962
ABTB549T 7865047863545657453 904
ABTC557C 3446574656854656574 763234
ABTB549T 7865047863545657453 V87
ABTC557C 3446574656854656574 985987654475
ABE05190000000752
ABBV09E201902165641 99921484568
ABH5684529845212JB19015037B0984565HCPP
ABTB549T 7865047863545657453 852
ABTC557C 3446574656854656574 852
ABE05190000000752
What I'm looking to do is create a function that can:
Loop over each block
Within each block, it breaks it down to each line
Within each line, it breaks it down to their subcomponents
I can't tell if it's better to put the subcomponents into an array, or an object but regardless, I need the subcomponents grouped and to be easily searchable.
I know that there is a for loop to go over each block but I can't figure out how to tell one block from another (much less the other steps).
I know that regex may be involved, and that's fine. But my regex isn't that strong so I'm still working on it.
Also, I believe that substring() is also a key component.
Any suggestions would be appreciated.
I am trying to split a string with split() and for some reason, it is giving me duplicate values this is driving me mad can someone please assist me
The string is a comma separated response from a database here is the echo
echo $Title1 . ',' . $link. ','.$value. ','.'number'.','.'$' .',';
Here is my code:
function displayMatches(response2){
var str = response2;
var str_array = response2.split(',');
}
This is the console log from str_array
0: "Butter cauliflower & paneer"
1: "https://link1.com"
2: "4"
3: "friends5"
4: "$"
5: "Butter cauliflower & paneer"
6: "https://link1.com"
7: "4"
8: "friends5"
9: "$"
10: "Butter cauliflower and coconut sambal"
11: "https://link2.com"
12: "3"
13: "friends5"
14: "$"
15: "Butter cauliflower and coconut sambal"
16: "https://link2.com"
17: "3"
18: "friends5"
19: "$"
Here is the value of response2
Butter cauliflower & paneer,https://link1.com,4,friends5,$,Butter cauliflower & paneer,https://link2.com,4,friends5,$,Butter cauliflower and coconut sambal,https://link3.com,3,friends5,$,Butter cauliflower and coconut sambal,https://link4.com,3,friends5,$,Creamy vegetarian pumpkin curry,https://link5.com,5,friends5,$,
Why is `split()` doing this?
This one had also driven me mad.
Try using this Inside the function
let endArray = new Set([]);
var str_array = response2.split(',');
for(var i = 0;i < str_array.length - 1;i++){
endArray.add(str_array[i]);
}
console.log(endArray)
Also, It would be easier if you would have added the string that you are splitting, This will help more viewers find better options and in less time.
If this didn't work get back to me on this answer as a comment
//##// Function Zone //##//
function out(type, ...strings) {
var str = strings.join(' ');
switch (type) {
case "log":
console.log("\x1b[37m", '[LOG]\t', str, "\x1b[0m");
break;
case "error":
console.log("\x1b[31m",'[ERROR]\t', str, "\x1b[0m");
break;
case "info":
console.log("\x1b[36m", '[INFO]\t', str, "\x1b[0m");
break;
default:
throw new Error('Bad output type');
}
}
When I start the program it crashes with this error:
FATAL ERROR: Error::New napi_get_last_error_info<br/>
1: 00007FF6F206CF2F napi_wrap+112799<br/>
2: 00007FF6F200CF26 v8::base::CPU::has_sse+55702<br/>
3: 00007FF6F200DDB3 v8::base::CPU::has_sse+59427<br/>
4: 00007FF6F200D509 v8::base::CPU::has_sse+57209<br/>
5: 00007FF6F2032410 napi_fatal_error+160<br/>
6: 00007FFC74541DA3 <br/>
7: 00007FFC74541CF7 <br/>
8: 00007FFC7454CB69 <br/>
9: 00007FF6F203032C node_module_register+1548<br/>
10: 00007FF6F20BD340 uv_timer_stop+560<br/>
11: 00007FF6F20BD417 uv_timer_stop+775<br/>
12: 00007FF6F20B9ECB uv_async_send+331<br/>
13: 00007FF6F20B966C uv_loop_init+1212<br/>
14: 00007FF6F20B9834 uv_run+244<br/>
15: 00007FF6F1FC9681 v8::internal::interpreter::BytecodeArrayWriter::source_position_table_builder+31713<br/>
16: 00007FF6F2036223 node::Start+275<br/>
17: 00007FF6F1EB6A9C RC4_options+340380<br/>
18: 00007FF6F2D2F3F8 v8::internal::SetupIsolateDelegate::SetupHeap+1300536<br/>
19: 00007FFCB7CA7034 BaseThreadInitThunk+20<br/>
20: 00007FFCB7DDCEC1 RtlUserThreadStart+33<br/>
Any Idea?
Thank you for helping me!
I finally found the problem, I forgot to modify a function in my code, the error is because of that. I called the method info() that is a standard node.js method an that caused the error.
Thank you anyway :D
As part of my project, I need to write some model data to JSON object and download it to file.
then I need to load that file and deserialize the JSON to model object.
That part is not working
demo project
https://stackblitz.com/edit/angular-wpg5gx
for repro, click on the export button you will get JSON file downloaded and try to import the exported file
expected
{"name":"usa","orgAddress1":"123 broadway","orgAddress2":"2D","city":"new york","system":[{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"},{"name":"sap"}]}
actual result:
Partner {0: "{", 1: """, 2: "n", 3: "a"…}
0: "{"
1: """
10: "s"
100: "a"
101: "p"
102: """
103: "}"
104: ","
105: "{"
106: """
107: "n"
108: "a"
109: "m"
11: "a"
110: "e"
...................
Update your conversions to the following
let json = fileReader.result.toString();
let obj = JSON.parse(json);
var convert = Object.assign(new Partner(), obj);
That should get you what you're looking for.
You are calling JSON.stringify() on a string before parsing it. If you remove your JSON.stringify() call, it will work as expected.
let object = JSON.parse(fileReader.result as string);
So I have been working on a Discord bot for my server for a few days using Node.js and am brand new to JS. I am trying to convert some of the code from a Magic 8 Ball completed after a model for a tutorial to JS to Implement as a function (just the random number and case switch).
This is from the C# Project:
switch (random.Next(4))
{
case 0:
Console.WriteLine("YES");
speechSynthesizer.Speak("Yes");
break;
case 1:
Console.WriteLine("NO");
speechSynthesizer.Speak("No");
break;
case 2:
Console.WriteLine("HELL NO");
speechSynthesizer.Speak("Hell no");
break;
case 3:
speechSynthesizer.Speak("Hell yes");
Console.WriteLine("HELL YES");
break;
}
and this is in what I am trying to implement:
switch( Math.random.Next(4)) {
case 0:
msg.channel.send('Yes');
break;
case 1:
msg.channel.send('No');
break;
case 2:
msg.channel.send('Hell yes!');
break;
case 3:
msg.channel.send('Hell No!');
break;
}
I am essentially trying to add a part where you can send "Magic 8 Ball", and it will return a Yes/No/Hell Yes/Hell No.
Edit:
So I followed the advice of a comment, and switched "case" to all lowercase in each instance. The error I recieve now is that math is undefined. Another article just suggested switching math to Math. next is not a function.
If your problem is generating a random int to use in your switch, you'll need to know how to generate a pseudorandom int in the first place.
Math.random generates a a float in the range of [0, 1)
So you'll need to take that value, multiply it by your max, and then floor it to turn it in to an integer.
Math.floor(Math.random() * max)
Your final code would look like this:
switch(Math.floor(Math.random() * 4))) { // 4 is maximum n of options
case 0:
msg.channel.send('Yes');
break;
case 1:
msg.channel.send('No');
break;
case 2:
msg.channel.send('Hell yes!');
break;
case 3:
msg.channel.send('Hell No!');
break;
}