I am noob with structured data implementation and don't have any code knowledge. I have been looking for a week how to solve my problem with "availability" in Google structured data testing tool.
My stocks on the front have 3 possibilities :
"Stock : 1234" (text + stock numbers)
"Stock : Coming soon"
"Stock : On demand"
Google Results example
But he problem is Goggle accept only in my case "InStock" or "OutOfStock"
I have a CSS variable element #PdtXQStock named in a variable "Product-stock"
How can have :
if "availability" string contain numbers (not is number) ==> output "InStock"
if "availability" string is "Stock : Coming soon" ==> output "OutOfStock"
if "availability" string is "Stock : On demand" ==> output "InStock"
What is the proper Custom Js function from beginning to end?
function(){
var string = {{Product-stock}};
string = "https://schema.org/InStock";
string = string.replace(/Stock : Coming Soon/g, "OutOfStock");
string = string.replace(/Stock : On demand/g, "InStock");
return string.replace;
}
Related
I have a collection like this;
{
"_id" : ObjectId("5d428c8b0edc602c155929c5"),
"source" : "connection",
"class" : "dns",
"ts" : 1503528301.909886,
"uid" : "C8avTr2cLyrJJxkN9",
}
If I print the key and type of key in mongo shell, I can see that ts actually is a string:
ts string
When I receive a query, I need to get the ts value from the URL and do a .find() query.
GET http://localhost:3300/alerts?ts=1503528332.909886&limit=100&page=1
startTs = req.query.ts
This may have to be converted to a ts without the '.' after the second. I have looked at converting 1503528332.909886 to float, multiply by 1000 1503528332909.886 and truncate to integer 1503528332909. I have also tried using both string and number format.
results.results = await model
.find({"ts": {"$gte": <ts_variable: what format do I use?> }})
.skip(startIndex)
.exec();
res.paginatedResults = results;
If I only use ".find({})" and not try to select based on ts, everything works as expected. Appreciate any tips you have.
I want from this string :
'Paris , Bruxelles , Amsterdam , Berlin'
Get this result in an array :
['Paris_Bruxelles' , 'Bruxelles_Amsterdam' , 'Amsterdam_Berlin' ]
Can anyone help me please ?
You could split the string and slice the array and get the pairs.
var string = 'Paris , Bruxelles , Amsterdam , Berlin',
array = string.split(/\s*,\s*/),
result = array.slice(1).map((s, i) => [array[i], s].join('_'));
console.log(result);
Basically in functional languages you are expected to use a zipWith function, where the accepted answer mimics that in JS with side effects.
However you may also mimic Haskell's pattern matching in JS and come up with a recursive solution without any side effects
var cities = "Paris , Bruxelles , Amsterdam , Berlin , Bayburt".split(/\s*,\s*/),
pairs = ([f,s,...rest]) => s == void 0 ? []
: [f + "_" + s].concat(pairs([s,...rest]));
console.log(pairs(cities));
I am trying to read the values of properties in this string, when i try to parse it , i get invalid character. Can you tell me whats wrong here
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},];
var test = $.parseJSON(data)l
error - Unhandled exception at line 138, column 13 in http://localhost:17765/Loc/index
0x800a03f6 - JavaScript runtime error: Invalid character
In your code, data is not a string. It is an array. It does not need parsing (other than by the JavaScript compiler). Just discard $.parseJSON and work with the data.
data = [{'title' : 'location 1','lat' : '29.769730','lng' : '-95.257181','desc' : 'Apartments',},{'title' : 'location 2','lat' : '29.852264','lng' : '-95.469999','desc' : 'location description',},];
data.forEach(o => console.log(o.title));
It is being returned from an mvc5 controller method as a string
If your code does not accurately reflect the data you have and you do have a string, then it would need parsing.
The code you provided is, however, not valid JSON which:
Requires strings be quoted with " not '
Does not allow trailing , after the last item in an array.
You need to fix the server side code so it returns real JSON.
This will probably involve replacing some code which attempts to generate JSON by mashing together strings with some which uses a JSON aware library function (see this question).
Your JSON is invalid, try this:
var data = '[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]';
var test = $.parseJSON(data);
validate your JSON here
[{"title" : "location 1","lat" : "29.769730","lng" : "-95.257181","desc" : "Apartments"},{"title" : "location 2","lat" : "29.852264","lng" : "-95.469999","desc" : "location description"}]
Based on https://www.plivo.com/blog/Send-templatized-SMS-from-a-Google-spreadsheet-using-Plivo-SMS-API/ I have the following code:
function createMessage(){
data = {
"SOURCE" : "+1234567890",
"DESTINATION" : "+2345678901",
"FIRST_NAME" : "Jane",
"LAST_NAME" : "Doe",
"COUPON" : "DUMMY20",
"STORE" : "PLIVO",
"DISCOUNT" : "20",
}
template_data = "Hi , your coupon code for discount of % purchase at is "
Logger.log(data);
for (var key in data) {
Logger.log(key);
if (data.hasOwnProperty(key)) {
template_data = template_data.replace(new RegExp('+key+', 'gi'),data[key]); // error here
}
}
Logger.log(template_data);
return template_data;
}
When I run createMessage I get :
SyntaxError: Invalid quantifier +. (line 57, file "Code")
From a previous question and if I understand correctly the loop goes through each key, value pair looking for all matches of the key (g) in a case insensitive fashion (i).
I don't understand the pattern '+key+' This causes the error and my attempts to test patterns like '+SOURCE+' also give the same error, although the seem to work while testing at https://regex101.com/r/CF967t/2 .
Can someone give me an explanation of the problem
sign + usually is a repetition operator, and causes the preceding token to repeat one or more times key+ would be expressed as keykey*
You have pass only key
template_data = template_data.replace(new RegExp(key, 'gi'),data[key]);
I have a text (pure text) that has values aligned to right, and field names (keys) aligned to left.
Key and Values have a single blank space Maximum for each, and any number of spaces separating them - minimum 2 spaces.
I need to get them to a tree so I can read those fields and extract values. I don't know how to proceed and parse this text (Example below) to the json like structure.
Example Text Input:
Item Name 1 value 1
Item Name 2 2
Third Item Long Name val 3
Fourth Item value 4
Desired output:
output = { 'ItemName1' : 'value 1',
'ItemName2' : '2',
'ThirdItemLongName' : 'val 3',
'FourthItem' : 'value 4' }
Edit:
I ended up using #Gueras Arnaud answer, with a small modification, becuase apparently my data has lots of blank spaces at the right.
var source = document.getElementById('source').value;
var newsource = "{" + source.replace(/(.+?)(?:\t+|\s{2,})(.+?)\n/g, '"$1":"$2",').replace(/,$/, '') + '}';
var jsonobjtree = JSON.parse(newsource);
for (var key in jsonobjtree) {
if (jsonobjtree.hasOwnProperty(key)) {
jsonobjtree[key] = jsonobjtree[key].replace(/^\s+|\s+$/g, '')
}
}
The best tool for that is regexp and the use of replace.
In this example, I try to match each line with this pattern :
/(.+?)(?:\t+|\s{2,})(.+?)\n
And insert the result to being like this : "key" : "value",
After I add { and } around the string result.
And to obtain an object I use JSON.parse()
var source = document.getElementById('source').value;
var newsource = "{" + source.replace(/(.+?)(?:\t+|\s{2,})(.+?)\n/g, '"$1":"$2",').replace(/,$/, '') + '}';
var obj = JSON.parse(newsource);
document.getElementById('source').value = newsource;
<textarea id="source" rows="10" cols="100">
Item Name 1 value 1
Item Name 2 2
Third Item Long Name val 3
Fourth Item value 4
</textarea>