Remove single quotes from an array using Java script / Node JS - arrays

I am having an issue of single quotes around the array value.
I have an array which I have define as:
var latLongArray=[];
and the value I am getting is as below:
['{latitude:43.73747, longitude:7.163546}',
'{latitude:50.127339, longitude:8.60512}',
'{latitude:30.267, longitude:-97.743}' ]
I don't want single quotes, I tried using above all solution but not working for my issue.
I tried using below solutions to remove single quotes:
var newLatLongArray = latLongArray.join(',').replace(/'([^']+(?='))'/g, '$1');
// Remove single quotes
I am able to remove the single quotes but the newLatLongArray I am getting as a String but actually I want an array.
Any suggestion to get the value as an array with removing single quotes, please.

At your line var newLatLongArray = latLongArray.join(',').replace(/'([^']+(?='))'/g, '$1');, you tell to join your array, replace the single quotes but you never split again.
The join() function will transform your array to a string, the replace will replace the wanted characters and give a string so you just need to add a .split(',') at the end like this:
var newLatLongArray = latLongArray.join(',').replace(/'([^']+(?='))'/g, '$1').split(',');
Hope I helped you!

I'll go under the assumption you want an array of lat/long objects.
In that case, you could use:
var data = latLongArray.map(item => {
item = item.replace('latitude', '"latitude"').replace('longitude', '"longitude"');
return JSON.parse(item);
}));

Related

Swift: How do you use a variable in an array?

I am using a pod called iOSDropDown to display a dropdown selection menu for a textfield. I am getting a list of data from php to populate that selection menu that Im storing in a variable.
The PHP data passed stored in a swift variable looks like this when printed -> "option01","option02","option03"... and so on. This is dynamic data that will change that is why I am retrieving from PHP/MYSQL Database instead of just manually typing in the options in the Swift array.
Below is my code. What I am trying to do is use the "dropdownData" variable that holds the options for the array. Each option should be in its own row and separately selectable. What I am getting is one option, one string of coding with all my options as shown in the picture below.How would I use the dropdownData variable to display options instead of one string, one option?
dropdownData = "option01","option02","option03"... etc. ALL OPTIONS STORED IN THIS ONE ARRAY
let dropdownData : String = (dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as! String
.
cell.nameField.optionArray = [dropdownData]
Image
In the image above there should be no comma after styrofoam cooler... the next product should be the next option displaying under the styrofoam cooler and separately selectable.
Seems like dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") returns a String where names are comma separated,
so
if let nameString = dumpsArray[indexPath.row] as AnyObject).value(forKey: "dropdownData") as? String {
let namesArray = nameString.components(separatedBy: ",")
cell.nameField.optionArray = namesArray
}
That should do

Reactjs convert input string to upper case

I'm creating an app in Reactjs using react-strap. I would like to convert an input field to upper case.
From googling, it looks like simply appending "toUpperCase()" to the field would work, but this doesn't appear as an option in Visual Studio code.
I had a similar issue with doing a replace all, but finally got that to work using "const" field:
// replace ":" with "-"
const phrase = item.macs;
const replaced = phrase.replace(/:/g, '-')
item.macs = replaced;
However, converting to a const field doesn't work for making the "toUpperCase()" available.
What should I do to turn this into a string so I can call the "toUpperCase()" function?
Edit: change references from "toUpper" to "toUpperCase". The problem is this is not available as a function.
For example of I do
'myString'.toUpperCase();
it works. But it I can't get it to bring that up in Visual Studio Code, and it's ignored if I code it anyway.
I believe you are looking after toUpperCase.
To make a string uppercase in javascript you can call .toUpperCase() method on it. For example
const foo = 'foo'
const fooUpper = foo.toUpperCase()
console.log(fooUpper) // expected result 'FOO'
I got around this problem by forcing the input item to be regarded as a string by prepending it with a '', like so:
item.macs = '' + item.macs;
item.macs = item.macs.replace(/:/g, '-');
item.macs = item.macs.toUpperCase();
After that, all the string functions were available.

Include a regex string in an array

I have an array made up of several strings that I am searching for in another array, like so:
strings_array = ["string1", "string2", "string3"]
main_array = [ ## this is populated with string values outside of my script ## ]
main_array.each { |item|
if strings_array.any? { |x| main_array.include?(x) }
main_array.delete(item)
end
}
This is a simplified version of what my script is actually doing, but that's the gist. It works as is, but I'm wondering how I can make it so that the strings_array can include strings made out of regex. So let's say I have a string in the main_array called "string-4385", and I want to delete any string that is composed of string- + a series of integers (without manually adding in the numerical suffix). I tried this:
strings_array = ["string1", "string2", "string3", /string-\d+/.to_s]
This doesn't work, but that's the logic I'm aiming for. Basically, is there a way to include a string with regex like this within an array? Or is there perhaps a better way than this .any? and include? combo that does the job (without needing to type out the complete string value)?
Thank you for any help!
You can use methods like keep_if and delete_if, so if you want to delete strings that match a regex you could do something like this:
array = ['string-123', 'test']
array.delete_if{|n| n[/string-\d+/] }
That will delete the strings in the array that do not match your regex. Same thing with keep_if method.
Hope it helps!
A good way to do this is with Regexp.union, which combines multiple regular expressions into a single regex handy for matching.
patterns = [/pattern1/, /pattern2/, /string-\d+/]
regex = Regexp.union(patterns)
main_array.delete_if{|string| string.match(regex)}

Find Array index that contains string

I have file with tags and targets, this is example:
TAG1|TARGET1,TARGET2
TAG2|TARGET3,TARGET4
I start by creating String Array using File.ReadAllLines
Dim MAIN As String() = File.ReadAllLines("")
At some point I have one of targets and I need to know what was the tag index (which array line is it), so for example if I have TARGET3 I want to know it's in second line so it's in MAIN(1) and then I can grab TAG = TAG2.
I can't get it working, I tried few methods:
Array.IndexOf(MAIN,"TARGET3")
always returned -1, it worked with full string tho,
Array.IndexOf(MAIN,"TAG2|TARGET3,TARGET4")
returned 1. I tried with Array.FindIndex, was the same.
So my question is: how to get index of partial array item. Thank you for any help.
You can use Linq to search your array in this way
Dim search = "TARGET3"
Dim line = MAIN.FirstOrDefault(Function(x) x.Contains(search))
This will return directly the line with the matching word

How to remove Double Quote in JSON Array

I searched through this forum but did not get similar case of removing double quotes in JSON array.
This is my part request format
"customer_address_list":[{"email_address" :{"email_to" : ""},
"method" : "EMAIL",
"customer_id" : ""}]
As this is dynamic values I putting values with push of JSON in for loop. Something like this in my js
Request_Format.customer_address_list.push( XXXXXXXXXXX) ;
Every time there is unwanted double quotes appending in every entry like
"customer_address_list":[
"{"email_address":"email_to":"xyz.pqqr#companyname.com"},"method":"EMAIL","customer_id":"1"}",
"{"email_address":"email_to":"zzz.aaaa#companyname.com"},"method":"EMAIL","customer_id":"2"}",
"{email_address":"email_to":"www.aaaa#companyame.com"},"method":"EMAIL","customer_id":"3"}"]
Due to this additional double quote at start and end of every entry, Final JSON became invalid.
All above code shared is in java script
Is there any work around to remove this double quotes
Thanks in advance
If you are using Python, you can convert each string to a dictionary literal using the eval function
customer_as_dict = eval(customer_as_string)
To remove the quotes around the name customer_address_list, you may be able to use
eval("customer_address_list") = [eval(customer_as_string) for customer_as_string in dict_as_string]

Resources