Array with two objects and string how to remove Quotes - object

I had to .concat two objects and a string, the string didn't have surrounding quotes and now it does, I have tried .replace in every different way, please, is there a way to remove the quotes surrounding a string inside array with other objects
var keywrdrtrn = {city_name:'${city_name}'}
var obj =v[0].concat(v[1]).concat(keywrdrtrn);
}
obj [
{
city_name: 'Cincinnati',
},
{
city_name: 'Mumbai',
},
"{city_name:'',state: ''}"
]
I have removed other index/objects for space but the format is same. I just cannot figure out how to concat the setup without quotes being added or figuring out how to remove quotes after concat
I tried .replace and I tried to convert string to object prior to concat

Related

How to create a custom string using map value - Terraform

I struggle to create a custom string in Terraform when I loop through a list of map which contains a map.
The problem isn't the looping itself but creation of a custom string.
E.g
locals {
for var1 ... : [
for var2 ... : {
myString = "${var2.map["key1"]_${var2.map["key2"]}" <-- This does NOT work as im using '"'
}]
}
Aware that the example does not work as I need to use double quote " to access the map value, but also because I need to use " for the creation of the string.
You are missing } and it should be var, not var2:
myString = "${var2.map["key1"]}_${var2.map["key2"]}"

Getting all words out of a string

I have this arrayList that receives data dynamically from a database
val deviceNameList = arrayListOf<String>()
Getting the index 0 of the arraylist ie deviceNameList[0] prints a string of such a format:
[Peter, James]
How can i list all names in deviceNameList[0] individually.
Assuming your input string is [Peter, James], you could try removing the square brackets at both ends, then regex splitting on comma followed by optional whitespace.
String input = "[Peter, James]";
String[] names = input.substring(1, input.length()-1).split(",\\s*");
System.out.println(Arrays.toString(names));
This prints:
[Peter, James]
Note that Java itself places square brackets around the array contents in Arrays.toString. They are not part of the actual data.

Mongodb text search not working with string Flask

I am trying to make text search with Flask.
For one word it works, but when I pass a string with multiple words it doesn't work.
But when I pass that string as hardcoded it works:
Suppose that string is this:
str = "SOME TEXT HERE"
if I pass it as variable like this:
newText= ' '.join(r'\"'+word+r'\"' for word in str.split())
result = app.data.driver.db[endpoint].find({"$text":{"$search":newText }}, {"score": {"$meta":"textScore"}}).sort([("score", {"$meta": "textScore"})])
it doesn't work.
But if I pass it as hardcoded like this:
result = app.data.driver.db[endpoint].find({"$text":{"$search":" \"SOME\" \"TEXT\" \"HERE\" " }}, {"score": {"$meta":"textScore"}}).sort([("score", {"$meta": "textScore"})])
It works.
The contents of variable newText are different from the contents in your hardcoded string.
Try removing 'r' during creation of newText to generate a string similar to the hardcoded string, as follows:
newText= ' '.join('\"'+word+'\"' for word in str.split())

How to handle semicolons when generating a CSV file?

I am using the NPM module json-csv to generate a CSV file from an array of objects. However, some fields might contain a semicolon (;), and apparently the CSV gets split at the semicolon, despite the fact that this field is quoted. Can anyone make any suggestions as to how to fix this issue?
The code I use for the options is the following:
var options = {
fields: [
{
name : 'paragraphId',
label : 'ParagraphID'
},
{
name : 'paragraph',
label : 'Paragraph',
quoted : true
}
]
};
According to CSV specification, you can have delimiters in values, as long as you surround these values with double quotes. From CSV specification:
Fields with embedded commas must be delimited with double-quote characters.
And:
Fields may always be delimited with double quotes.
The delimiters will always be discarded.
Option to trigger this behavior on when exporting data using json-csv library is quoted: true in the options for a given field - I see that you've already included it, so you're good.
Also - it's worth to note that this library uses comma (,) as delimiter by default, not semicolon (;). To use different delimiter, alter your options properly:
var options = {
fields: [
{
name: 'paragraphId',
label: 'ParagraphID'
},
{
name: 'paragraph',
label: 'Paragraph',
quoted: true
}],
fieldSeparator: ';' // the important part!
};

Couch DB escape characters

I have a list function for CouchDB, but I can't get it into the DB because I'm constantly getting syntax erros. Here is the function:
function(head,req) {
var headers;
start({'headers':{'Content-Type' : 'text/csv; charset=utf-8; header=present'}});
while(r = getRow()) {
if(!headers) {
headers = Object.keys(r.value);
send('"' + headers.join('","') + '"\n');
}
headers.forEach(function(v,i) {
send(String(r.value[v]).replace(/\"/g,'""').replace(/^|$/g,'"'));
(i + 1 < headers.length) ? send(',') : send('\n');
});
}
}
Can anyone show me an example of this function formatted that can be inserted into CouchDB?
List functions are stored in design documents. Design documents are JSON documents, so you need to make sure they conform to the JSON standard. Since List functions are string values in JSON, you need to make sure that you:
Escape any double quotes in the function, so " becomes \". Where possible, you should use single quotes instead of double quotes.
Make sure you replace any line breaks with \n or just remove line breaks as Javascript ignores them anyway.
Escape any backslashes, so \ becomes \\.

Resources