IndexOf is missing a value in an array - javascript

I realize that IndexOf will return a -1 when it doesn't find a value within an array, however, the value that I have it searching for is in the array. I have confirmed this by returning the value in the array with console.log
Here is some of my code (paraphrased for simplicity):
var xtiles = [];
function assignValue(tile) {
xtiles.push(tile); //tile is 1 at this point
checkArray();
}
function checkArray() {
var temp = xtiles.indexOf(1);
console.log(temp); //this returns a -1
console.log(xtiles[0]); //this returns a 1
}
The variable 'temp' should return a 0 since there is a 1 in index 0 of the array xtiles. What am I missing here?

That's because the number 1 is not strictly equal to the string '1'.
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).
References:
Array.prototype.indexOf()

If the content of the array are strings then you can perhaps search as follows:
var temp = xtiles.indexOf(1+""); // force 1 into a string

Related

How does find method works with the array?

Suppose I have an array and I want to find first number in an array which is greater than 25. It checks for the condition i.e. whether the number is greater than 25 and should return true when the condition satisfies but it returns the value.Help me debug how it is working.
const numbers=[10,20,30,40,50]
function findNumJS(numbers,num){
const output=numbers.find(function(n){
return n>num
})
return output
}
console.log(findNumJS(numbers,25))
The find() method returns the value of the first element in an array that pass a test (provided as a function).
The find() method executes the function once for each element present in the array:
If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)
Otherwise it returns undefined
Note: find() does not execute the function for empty arrays.
Note: find() does not change the original array.
For more : https://www.w3schools.com/jsref/jsref_find.asp
Array​.prototype​.find()
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
If you want to return Boolean value then you can use Array​.prototype​.some()
const numbers=[10,20,30,40,50]
function findNumJS(numbers,num){
const output=numbers.some(function(n){
return n>num
})
return output
}
console.log(findNumJS(numbers,25))
Actually find method go through each and every array item one by one and check the provided condition. If the provided condition is satisfied then the find method returns the matched item immediately.
const numbers=[10,20,30,40,50]
numbers.find(number => number > 25)
//In first iteration number -> 10 ; which is not greater than 25
//In second iteration number -> 20 ; which is not greater than 25
//In third iteration number -> 30 ; which is greater than 25, so the condition satisfied and it returns the value
if you find the first element in the arrays you can use .find
but you want to check is there any element that satisfy your condition. so you should use .some.
for your example you can change it like this:
const numbers=[10,20,30,40,50]
function findNumJS(numbers,num){
const output=numbers.some(function(n){
return n>num
})
return output
}
console.log(findNumJS(numbers,25))
you can decrease your code like this or just user .some:
const numbers=[10,20,30,40,50]
function findNumJS(numbers,num){
return numbers.some(function(n){
return n>num
})
}
console.log(findNumJS(numbers,25))
for more information see this:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
function isBigEnough(value) {
return function(element, index, array) {
return (element >= value);
}
}
var filtered = numbers = [10, 20, 30, 40, 50].filter(isBigEnough(25));
console.log(filtered)
The javascript Array.find(callback) the function returns the first item of the array, where the function returns true.

How to use 'in' keyword in Javascript

I have a question here. I know my code have plenty of problems and I need your help.
The problem is, To return an object which have numbers of repetition information of the string.
The given string is
var r = countFreq(["a", "b", "b", "c", "c", "c", "d"]);
and the result have to be
{"a":1, "b":2, "c":3, "d":1 }
by console.log(r);
All that I know is, key(properties) have to be element and value(value of property) have to be the number of repetition.
AND, I must use 'in' key world to solve this problem.
Like,
if('property' in 'object') {
//...
}else {
//...
}
(if there's no property initialize as 1, and if there's a same property, add 1 each time)
I really appreciate your help.
(This post may have grammatical errors. I really feel sorry about that...)
function countFreq(array) {
var i;
for(i=0; i<array.length; i++)
{
if(array[i] in array)
{
return i += 1;
}else
{
return i = 1;
}
console.log(array[i]+": "+i+", ");
}
}
var r = countFreq(["a","b","c","c","c","d"]);
console.log(r);
According to MDN - The 'in' operator returns true if the specified property is in the specified object or its prototype chain.
Prop is a string or symbol representing a property name or array index (non-symbols will be coerced to strings).
Object is to check if it (or its prototype chain) contains the property with specified name.
So in your case, it depends what your object is? if you object is an array, you need to use prop as properties of array. All index values up to length of array will return true.
MDN Example of arrays
var trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
0 in trees // returns true
3 in trees // returns true
6 in trees // returns false
'bay' in trees // returns false (you must specify the
// index number, not the value at that index)
'length' in trees // returns true (length is an Array property)
Symbol.iterator in trees // returns true (arrays are iterable, works only in ES2015+)
I think you are misunderstanding the in operator. In can be used in 2 cases, as a boolean operator to check for the presence of an index in an array or to iterate over the indexes of an array with a for loop. You are using it to check for the presence of a value in an array directly, which you cannot do. Also you are returning from the function after each iteration so you will only ever get 1 or 0.
I presume you want something like the following:
countFreq(array) {
var results = { a: 0, b: 0, c: 0, d: 0 };
for (var index in array) {
results[array[index]] ++;
}
return results;
}
Now you can access each result with results[‘a’] for instance, after calling countFreq. I think you need to read up on return and loops in JavaScript.

Java Script: Array.length expression evaluate to boolean

How to check array length, if I need the expression to evaluate to boolean value as a result? For example:
var myArray = [];
if(!myArray.length){
return;
}
Or:
vr myArray = [];
if(myArray.length == 0){
return;
}
Both of the examples work, however I’d like to understand what is the difference?
Here !myArray.length will return true in two cases:
If myArray.length===0 because 0 is a falsy value, so !0 will return true.
If myArray.length is undefined, in other words myArray is not an array so it doesn't have the length property, where undefined is falsy value too.
And the first one explains why both !myArray.length and myArray.length==0 are equivalent in your case.
This is because length function returns a Number and when you use Number as condition, 0 and NaN are evaluated as false (in other case is evaluated as true).

return object length which includes key is true not false

I want to write some filter logic where an object returns its length which includes only key having true value.
$scope.generated.codesWithBalance = [A:true, B:true, C:false];
so for above object it should return length as 2. Since C is false so want to exclude in the count.
But now whenever i try to get the length it returns total length
Object.keys($scope.generated.codesWithBalance).length
Any way that i can avoid key having false value?
Use Array.prototype.filter() to get only the keys with true values.
Object.keys($scope.generated.codesWithBalance).filter(function(key, i, array) {
return array[key];
}).length;

Array Out of Bounds: Comparison with undefined, or length check?

this seems to be a common javascript idiom:
function foo (array, index) {
if (typeof array[index] == 'undefined')
alert ('out of bounds baby');
}
as opposed to the more prevalent (in other languages) and conceptually simpler:
function foo (array, index) {
if (index >= array.length)
alert ('boo');
}
I understand that the first case will also work for arrays which have 'gaps' in them, but is that a common enough case to warrant the idiom?
The code sample that prompted this question can be seen here. In this case, when using the 'argument' variable inside a function, isn't it sane to assume that it will be a contiguous array?
The only correct way is to check the index vs. the length.
An element may be assigned the value undefined. It is just silly to use it for a sentinel here. (There may be other, valid and possibly overlapping, reasons for checking for undefined, but not "for an out of bound check" -- the code in the other question will present arguably wrong results when the value of the given arg is really undefined.)
Happy coding.
You can also write:
if (index in array) {
which will return true even if array[index] is set to undefined.
Do not test for undefined. You should use the length of the array. There are cases where it simply does not work to test for undefined because undefined is a legal value for legitimate array entry. Here's a legal JS array:
var legalArray = [4, undefined, "foo"];
And you can access it like this:
var legalArray = [4, undefined, "foo"];
var result = "";
for (var i = 0; i < legalArray.length; i++) {
result += legalArray[i] + "<br>";
}
$("#result").html(result);
Generates this output:
4
undefined
foo
As seen in this jsFiddle: http://jsfiddle.net/jfriend00/J5PPe/
It's not common as far as I know, much more common is:
for (var i=0, iLen=array.length; i<iLen; i++) {
// do stuff
}
You should not compare to undefined, since a member of the array may have been assigned a value of undefined, or may not have been assigned any value.
e.g.
var a = [0,,,,];
alert(a.length); // 4 (or 5 in buggy IE);
a[1] === undefined; // true but not out of bounds
The main reason for using a for loop is that array properties may not be returned in numeric order if a for..in loop is used.
A for..in loop is much more efficient in sparse arrays but the possible out-of-order access must be dealt with if it matters (as must inherited and non-numeric enumerable properties if they should be avoided).
In JavaScript arrays can be sparse - they can contain "holes". For example
const array = new Array(3);
results in an array of three "holes" - not values. So while
const isInBounds = 0 <= index && index < array.length;
correctly identifies whether index is within bounds on array it does not indicate whether there is a value at array[index].
Object.prototype.hasOwnProperty() can be used to determine whether a value exists at an index. It also needs to be noted that different parts of the language can behave quite differently in the presence of "holes".
// ESLint: no-prototype-builtins)
const hasOwnProperty = Object.prototype.hasOwnProperty;
function show(array, index) {
const msg =
0 > index || index >= array.length
? `index ${index} is out of bounds`
: !hasOwnProperty.call(array, index)
? `index ${index} is a hole`
: `index ${index} holds ${array[index]}`;
console.log(msg);
}
const subject = [undefined, , 1];
show(subject, -1);
// "index -1 is out of bounds"
for (let i = 0; i < subject.length; i += 1) show(subject, i);
// "index 0 holds undefined"
// "index 1 is a hole"
// "index 2 holds 1"
show(subject, 3);
// "index 3 is out of bounds"
const toString = (value) =>
value !== undefined ? value.toString() : 'undefined';
// for..of doesn't skip holes
const byForOf = [];
for (const value of subject) byForOf.push(toString(value));
console.log(`Values found by for..of: ${byForOf.join(', ')}`);
// "Values found by for..of: undefined, undefined, 1"
// .forEach skips holes
const byForEach = [];
subject.forEach((value) => byForEach.push(toString(value)));
console.log(`Values found by .forEach: ${byForEach.join(', ')}`);
// "Values found by .forEach: undefined, 1"
// .reduce skips holes
const reducer = (acc, value) => {
acc.push(toString(value));
return acc;
};
const byReduce = subject.reduce(reducer, []);
console.log(`Values found by .reduce: ${byReduce.join(', ')}`);
// "Values found by .reduce: undefined, 1"
// .map preserves holes
const byMap = subject.map(toString);
console.log(`Values found by .map: ${byMap.join(', ')}`);
// "Values found by .map: undefined, , 1"
In that case, the test it to make sure that it does not accidentally add the String "undefined" to the calling String. In the case below, it will actually do just that:
var undefd;
"{0} is dead, but {1} is alive! {0} {2}".format("ASP", undefd, "ASP.NET")
// output as "ASP is dead, but {1} is alive! ASP ASP.NET"
Personally, though, I would probably simply cache the length and then do a numeric comparison.
EDIT
Side note: his method also avoids a NaN check but forces a strict parallel:
// this will fail unless 0001 is cast to a number, which means the method
// provided will fail.
"{0} is dead, but {1} is alive! {0001} {2}".format("ASP", "ASP.NET")

Categories

Resources