comparing GUID/UUIDs in javascript - javascript

I have a function that is supposed to filter out messages without the same UUID in javascript. However when comparing, nothing is equal, even the ones that clearly are. They are all lowercase on both sides and when you console log they come out looking identical.
Interesting detail that might lead to whats wrong. I cannot use the trim function on first guid without some kind of string conversion first
function filterReceivedMessages()
{
var neededMessages = [];
for(var i=0; i < messages.length; i++)
{
if((messages[i].orginator+"").trim() != client.GetUserUid().trim())
{
neededMessages.push(messages[i]);
}
}
return neededMessages;
}

Related

Some values in my array are undefined within my function even though the array has finished loading

This might seem similar to some questions, but I don't have the same problems as those posters, so I don't think this is a duplicate.
if(inputSize instanceof NeuralNet){
console.log('NN being copied');
console.log(inputSize);
console.log('Copying neural net...');
this.layerA = [];
for(let i = 0; i < inputSize.layerA.length; i++)
this.layerA[i] = inputSize.layerA[i].slice();
this.layerB = [];
for(let i = 0; i < inputSize.layerB.length; i++)
this.layerB[i] = inputSize.layerB[i].slice();
console.log(this.layerA);
console.log(this.layerB);
console.log(this);
console.log('Finished copying NN');
this.biasA = inputSize.biasA;
this.biasB = inputSize.biasB;
console.log(this.layerA);
this.fitness = 0;
console.log(this);
}
This is the part of my code that has issues. I have valid values in the arrays of this.layerA and this.layerB when I apply console.log on them, so I know that there isn't an issue of me logging the arrays before they are populated with values.
However, when I log "this", I get weird values for layerA and layerB. The arrays still have the right dimensions, but most of the values within them are undefined, with a few numerical values (sometimes, not in this case).
Moreover, when I log this.layerA and this.layerB right after logging this, they still have the correct values. I have absolutely no idea why this is the case. I appreciate any help I can get. Thank you!

Removing array elements that contain a number

I have seen several answers on Stackoverflow but none have helped me. I have a huge array of nearly 100,000 words, of which I am trying to remove all words that contain a number. I am using the following to do that:
for(var i = 0; i < words.length; i++){
if (hasNumbers(words[i]) {
words.splice(i, 1);
}
function hasNumbers(t)
{ return /\d/.test(t); }
It seems to work, but not all the time because I am still getting words that contain numbers. What can I change to make this remove all words that contain any number at all?
(I am using p5.js with my js)
That is because when you delete a word at index i, the next word will have index i, yet you still increase i, thereby skipping a word which you never inspect.
To solve this you can go backwards through your array:
for(var i = words.length - 1; i >= 0; i--){
// etc.
Here is a shorter way to remove words with digits:
words = words.filter(a => !hasNumbers(a));
Finally, you really should call your second function hasDigits instead of hasNumbers. The words "digit" and "number" have a slightly different meaning.
Here is a snippet, using ES6 syntax, that defines the opposite function hasNoDigits and applies it to some sample data:
let words = ['abcd', 'ab0d', '4444', '-)#', '&9ยต*'];
let hasNoDigits = s => /^\D*$/.test(s);
console.log(words.filter(hasNoDigits));
words = words.filter(a => !hasNumbers(a));
I had started writing this and then trincot answered. His answer is correct, though with the popular and widespread usage of ES5 array functions, I feel like you could simplify this down quite a bit.
window.addEventListener('load', function() {
var data = [
'w3.org',
'google.com',
'00011118.com'
]; //This is supposed to be your data, I didn't have it so I made it up.
var no_nums = data.filter(function(item) {
//Tests each string against the regex, inverts the value (false becomes true, true becomes false)
return !/\d/.test(item);
});
var results = document.getElementById('results');
no_nums.forEach(function(item) {
results.innerHTML += item + '<br />';
//Loops through each of our new array to add the item so we can see it.
});
});
<div id="results">
</div>

Returning duplicates in multidimensional Javascript array

I have searched high and low, not only on StackOverflow, but many other places elsewhere on the web. I've tried what seems like everything, but something is fundamentally flawed with my logic. I apologize for introducing another "Duplicates in Array" question, but I am stuck and nothing seems to be working as expected.
Anyway, I have a multi-dimensional JavaScript array, only 2 levels deep.
var array = [[Part #, Description, Qty:],
[Part #, Description, Qty:],
[Part #, Description, Qty:]]; //etc
What I need to do is create a function that searches array and returns any duplicate "Part #" lines. When they are returned, I would like to have the entire inner array returned, complete with description and qty.
The trick with this is that the Part #'s that would qualify as 'duplicate' would end differently (specifically the last 4 characters), so using String.prototype.substr makes sense (to me).
I know there are duplicates in the array in the way that I am looking for, so I know that if I had the solution, it would return those Part #'s.
Here is what I have tried so far that gets me the closest to a solution:
function findDuplicateResults(arr) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i][0].substr(0,5) === arr[++i][0].substr(0,5)) {
result.push(arr[i]);
}
}
return console.log(result);
}
My thinking is that if the element in the array(with substr(0,5) is equal to the next one in line, push that to the result array. I would need the other duplicate in there too. The point of the code is to show only dupes with substr(0,5).
I have tried using Higher Order Functions such as map, forEach, reduce, and filter (filter being the one that boggles my mind as to why it doesn't do what I want), but I have only been able to return [] or the entire array that way. The logic that I use for said Higher Order Functions remains the same (which is probably the problem here).
I am expecting that my if condition is where the most of the problem is. Any pointers or solutions are greatly appreciated.
There is a mistake in your code. When you use ++i, you are changing the value of i, so it is going to skip one item in the next iteration.
Regarding the logic, you are only comparing one item to the next item, when you should really be comparing each item to all items:
function findDuplicateResults(arr) {
var result = [];
for (var i = 0; i <= arr.length - 1; i++) {
for (var k = 0; k <= arr.length - 1; k++) {
if (i !== k && arr[i][0].substr(0,5) === arr[k][0].substr(0,5)) {
result.push(arr[i]);
}
}
}
return result;
}
Although, the 'substr' could be dropped, and 'for' loop could be replaced by a higher order function:
function findDuplicateResults(arr) {
return arr.filter(function(item1){
return arr.filter(function(item2){
return item1[0] === item2[0];
}).length > 1;
});
}

javascript loop string comparison headache

console.log(previousCompetitors);
console.log(competitors);
if(data.isVisible) {
var moveIndexTo = [];
for(var q=0; q<competitors.length;q++) {
moveIndexTo.push(-1);
}
for(var i = 0; i<competitors.length; i++) {
for(var j = 0; j<previousCompetitors.length; j++) {
console.log(competitors[i].name);
console.log(previousCompetitors[j].name);
if(competitors[i].name === previousCompetitors[j].name) {
moveIndexTo[j]= i;
break;
}
}
}
console.log(moveIndexTo);
}
I'm slowly going insane trying to figure out what is happening here. I have an array of competitor data that updates in order. They are both arrays and I want to track the changes from the previous ordering.
I console.log the data and can see that the data order has been changed yet every single time the moveIndexTo array ends up being [0,1,2,3,4,5] implying that previousCompetitors an Competitors have equal order. How can they be changed between when I console.log them at the top of the code block to when I perform the string comparison?
Competitors and previousCompetitors take roughly the form
[{name:'name1'},{name:'name2'},{name:'name3'},{name:'name4'},{name:'name5'},{name:'name6'}]
with a lot more going on in each object. So If that was previousCompetitors then competitors would be something like
[{name:'name6'},{name:'name2'},{name:'name3'},{name:'name4'},{name:'name5'},{name:'name1'}].
Note the switch of name1 and name6. So I would expect moveIndexTo to be [5,1,2,3,4,0].
Just try this : moveIndexTo[i] = j;
fiddle at : https://jsfiddle.net/c9mbbpjj/

Tableau JS Api Filter does not accept variable

I am writing some code, that is supposed to automatically filter a Tableau workbook, based on the values of an array.
The function is called through:
showOnly2('Disponent',filterString);
'Disponent' is hard coded, filterString is an array of this format:
['201','202','203','204','205','206','207','208','209']
In the following block of code:
function showOnly2(filterName, values) {
document.getElementById("header").innerHTML = values;
sheet = viz.getWorkbook().getActiveSheet();
if(sheet.getSheetType() === 'worksheet') {
sheet.applyFilterAsync(filterName, values, 'REPLACE');
} else {
worksheetArray = sheet.getWorksheets();
for(var i = 0; i < worksheetArray.length; i++) {
worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE');
}
}
}
The worksheetArray[i].applyFilterAsync(filterName, values, 'REPLACE'); part does not work when called with the variable "values".
However, if I hard-code the value of the array into the formula:
worksheetArray[i].applyFilterAsync(filterName, ['201','202','203','204','205','206','207','208','209'] , 'REPLACE');
it works like a charm...Unfortunately I cannot hard-code it, as the numbers in the array (the filter values I want to set), change over time.
Does anybody have an idea, why the variable version does not work?
(I also checked for scope issues, by implementing document.getElementById("header").innerHTML = values;
within the function, just to doublecheck that the array value is read properly.
Any pointers are much appreciated.
The answer is straight forward.
The API expectes an array-object not a string (although when displayed as a string both look the same).
Hence, the following code worked flawlessly:
var filterArray = [];
for(var i = 201; i < 300; i++) {
filterArray.push(i);
}

Categories