Running a loop for each mysql query result fields - mysql

queryResultArr is the array that olds mysql queries results, let say i have in my table two columns: doc_1_hits and doc_2_hits, but i don't know prior how much docs i have and i want to run a loop like you can see below: queryResultsArr[0].doc_i_hits instead of doc_1_hits.
i tried a: var str = 'doc_'+i+'_hits'; and then queryResultsArr[0].str but nothing..
for(var i = 1; i < 3; i++){
if(queryResultsArr[0].doc_i_hits > 0 && queryResultsArr[1].doc_i_hits == 0){
console.log(i);
}
}

I think you could use the array notation and do it like below because they work similar in this case.
var a = 'doc_'+i+'_hits';
queryResultsArr[0][a]

Related

Angular: Posting Data to a Table with a loop and giving a problem when posting the i of the loop into the table

Good day,
I am trying to get data into a table, with the tour_id and every single media_id (the station_id i am getting from somewhere else), the ordernumber is what is giving me a headache:
I am trying to get every station one number for every media i am posting.
For example:
station 1 has 2 medias
and station 2 has 3
then the odernumbers should be like this: 0, 0, 1, 1, 1
I am using the following Code at this moment:
for(var i = 0; i < this.currentStations.length; i++){
this.http.get("http://localhost:3000/mediasforstation/" + this.currentStations[i].id).subscribe((res) => {
medias = res;
for (var j = 0; j < medias.length; j++){
this.http.post("http://localhost:3000/posttourstations",
{"tour_id": id, "media_id": medias[j].id, "ordernumber": i}).subscribe(function(res) {
console.log(res);
}.bind(this));
}
});
}
Everything but the ordernumber works, however, the ordernumber always takes the number of stations involved, in our example above it would be 2.
How do I fix this?
Thank you very much for your help.
As I understand, you need to keep the index value. The type of variable i is var which is function scoped. Within outer loop, you are calling an API that returns some response, meanwhile the value of i is updated and for next index/counter, the API call has been sent. When you get response from API calls, you get the value of i where the outer loop has been called of.
In other words, you need to understand the difference between var and let. Your problem can be solved by replacing
for(var i=0;...)
with
for(let i=0;...)
Here's providing you the sample code.
//block scoped - retains value of i
for (let i=0;i<10;i++){
this.http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}
//function scoped - gets updated value of i
for (var i=0;i<10;i++){
http.get('https://jsonplaceholder.typicode.com/users').subscribe(res=>{
for(var j=0;j<5;j++){
console.log(`i=>${i}`)
}
})
}

in mysql we have SOUNDEX() or we can use SOUNDS LIKE for related / wrong spells or words matching so is their any thing like in BIGQUERY

in mysql we have
SOUNDEX()
and
SOUNDS LIKE
for related / wrong spellings or words matching in query
Is there anything like that or similar in BigQuery? Can BigQuery be used for such queries?
Using BigQuery User-Defined Functions (UDF) in JavaScript you can simply implement SOUNDEX function yourself.
Here is SQL statement for BigQuery that calculate SOUNDEX:
#standardSQL
CREATE TEMP FUNCTION SOUNDEX(name STRING)
RETURNS STRING
LANGUAGE js AS """
if (name == null) return null;
let s = [];
let si = 1;
let c;
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
let mappings = "01230120022455012623010202";
s[0] = name[0].toUpperCase();
for(let i = 1, l = name.length; i < l; i++) {
c = (name[i].toUpperCase()).charCodeAt(0) - 65;
if(c < 0 || c > 25) { continue; }
if(mappings[c] == '0') { continue; }
if(mappings[c] != s[si-1]) {
s[si] = mappings[c];
si++;
}
if(si > 3) { break; }
}
if(si <= 3) {
while(si <= 3) {
s[si] = '0';
si++;
}
}
return s.join("");
""";
SELECT SOUNDEX("John Doe"), SOUNDEX("Jon Do")
Credit: I took the original JavaScript code from Chris Webb blog post here and slightly change the coding syntax.
I've checked in the "Google BigQuery Analytics" book, and no, BigQuery does not have anything like "SOUNDEX()" (at least at the time of publishing).
You might want to check cloudSQL, which is a mysql server hosted on the cloud. I know there are some functions from mysql you can't use in the cloudSQL, but it would be worth looking into.
I realize this question was asked years ago, but for those who wander here as I did looking for answers, here you go.
BigQuery does now have SOUNDEX()
Here is the documentation

How do i make my pickNum array just one index instead of 3 different indexes.

this will random my numPool and push the random three numbers to my array called pickNum. I need that pickeNum to be just one index instead of three indexes. Thanks and i will appreciate any help thanks.
var numPool:Array = [1,2,3];
var pickNum:Array = [];
var randomCount:Number = 3;
var r:Number;
for (var i = 0; i < randomCount; i++)
{
r = Math.floor(Math.random() * numPool.length);
pickNum[pickNum.length] = numPool.splice(r,1);
}
trace("Number Picked " + pickNum);
Can't say I completely understand what you are saying, but the issue with the code above is that splice returns an Array containing the values that were spliced from the array. Also, would be easiest to just do a push. So the proper line for getting that value and pushing it into the pickNum array would be :
pickNum.push(numPool.splice(r,1).pop());
And if you are saying that you want only 1 random number from the pool, then why do you need a loop ?

Difficulty outputting an array of indexes from an existing array consisting of strings

So I am trying to create a function that searches through an array based on a searchTerm. If the elements within the array have the searchTerm in it, it should output ALL of indexes inside of MyArray[];.
I hope I have explained clearly, thanks in advance.
Here's a corrected version:
var colours = ["I like the colour red", "I hate the colour yellow", "I love the colour blue"];
function myFunction(colours, searchTerm) {
var myArray = [];
searchTerm = searchTerm.toLowerCase();
for (var i = 0; i < colours.length; i++) {
if (colours[i].toLowerCase().indexOf(searchTerm) >= 0) {
myArray.push(i);
}
}
return myArray;
}
alert(myFunction(colours,"colour")) //Should return indexes 0,1,2 in myArray
And a working demo here: http://jsfiddle.net/jfriend00/GDM9R/.
I had to fix a lot of issues:
You weren't adding results to myArray properly.
You weren't adding the index to myArray.
You weren't testing the results of .indexOf() properly (it returns -1 when no match).
You were iterating over the length of the search phrase, not the number of items in the array.
You didn't declare i as a local variable so it was an implicit global variable.
myArray = colours[i] does not append to the array.
myArray.push(a);

Linq To Sql 'Where Or' operator

I need to create a query which checks if a field (string) contains one or more words supplied at run time.
Basically I need to be able to ask a WhereOr question. This seems like it should be a common issue when dealing with LinqToSql.
I found the following reference but can't make sense out of it - and have no idea how to use it in my project.
i've tried the following loop:
var query = from d in context.Domains select d;
for (int i = 0; i < words.Length; i++)
{
query = query.Where(d => d.Name.Contains(words[i]));
}
but this builds a SQL query with WHERE AND Clauses NOT Where OR
I use PredicateBuilder for such things.
The predicate construction looks like this:
var query = from d in context.Domains select d;
var predicate = PredicateBuilder<Domains>.False();
for (int i = 0; i < words.Length; i++)
{
predicate = predicate.Or(d => d.Name.Contains(words[i]));
}
query = query.Where(predicate);