array[index] == 'undefined' compares the value of the array index to the string "undefined".
You're probably looking for typeof array[index] == 'undefined', which compares the type.
array[index] == 'undefined' compares the value of the array index to the string "undefined".
You're probably looking for typeof array[index] == 'undefined', which compares the type.
You are checking it the array index contains a string "undefined", you should either use the typeof operator:
typeof predQuery[preId] == 'undefined'
Or use the undefined global property:
predQuery[preId] === undefined
The first way is safer, because the undefined global property is writable, and it can be changed to any other value.
If I am accessing an index of an array based on a variable, I would first want to check that the index exists.
My understanding is that if I try to access an index outside of the array's range, I will get a value of 'undefined.' So,
(typeof array[i] === 'undefined')
should return true if array[i] does not exist.
Would it also be possible, because undefined is falsey, to get the same result with
!array[i]
?
Edit: Thanks for the help!
Don't use a for..in loop to iterate arrays. If you are interested in the reasons, please read this StackOverflow question. They should only be used for traversing objects.
Use a simple oldschool for loop instead, it will solve your problem.
for (var i = 0, l = arrayResults.length; i < l; i++) {
if (typeof(arrayResults[i])=='undefined') {
// ask user to repeat
};
};
jsFiddle Demo
You can use indexOf method on array.
function hasUndefined(a) {
return a.indexOf() !== -1;
}
hasUndefined([1,2,3, undefined, 5]);
You can do this in one statement with Object.assign like this:
let myArray = [{id: 1, name: 'Boris'}, {id: 2}]
// id 3 does not exist no changes would be made to myArray
Object.assign(myArray.find(x => x.id == 3) || {}, {name: 'Foo'})
// id 1 is there so name would be changed to 'john'
Object.assign(myArray.find(x => x.id == 1) || {}, {name: 'john'})
console.log(myArray)
The idea is to provide Object.assign with an object literal if Array.find does not find an object with the provided id.
The other benefit of this is that you now pass an object for the update so you can easily update more than one property at a time:
let myArray = [{id: 1, name: 'Boris'}, {id: 2}]
// id 1 is there so name would be changed to 'john'
Object.assign(myArray.find(x => x.id == 1) || {}, {name: 'john', state: 'FL'})
console.log(myArray)
You need to run find as it's own statement and check if the result is defined. If so, assign the name, otherwise don't:
let myarray = [
{id: 100},
{id: 10},
{id: 5}
]
let found = myarray.find(colDef => colDef.id === 10)
if (found !== undefined){
found.name = "some name"
}
console.log(found)
// no id 11 found will be undefined
found = myarray.find(colDef => colDef.id === 11)
console.log(found)
As a single line:
this.myarray.find(colDef => colDef.id === checkid).name = newname;
there's no chance to check if find() returned undefined and trying to assign a property to undefined will be an error.
The array what you created is called a sparse array. You can read more about it in this answer.
The check
if (field !== undefined)
might not help if the actual value stored in the array itself is undefined. And this check
if (field)
will evaluate to be truthy if field is any one of the Truthy values. You can read more about the Truthiness and Falsiness of different values in this answer.
So, if you want to know if the array has a particular index, then you can use Object.prototype.hasOwnProperty, like this
var array = [];
array[1] = undefined;
console.log(array[0]);
// undefined
console.log(array[1]);
// undefined
console.log(array.hasOwnProperty(0));
// false
console.log(array.hasOwnProperty(1));
// true
Here the element at 1 is defined to be undefined, but since 0 is not defined at all, by default, JavaScript returns undefined.
The hasOwnProperty call checks if the current object really has the index 0 and 1.
"Best" is a fairly subjective term, so let's throw some objective criteria at it:
Which is faster?
In a way that you'll notice in the real world, neither. But if you really want to know, measure your specific code on the engines you want to support.
Which is easier to read?
This is probably subjective as well. The second form is extremely common in JavaScript, FWIW.
Which is less typing?
The second form is obviously a lot shorter than the first.
Which is more reliable?
Given your use case, they're equally reliable, because you're either not storing an entry in the array at all, or storing a non-
nullobject reference.In similar but different use cases, you might need to be wary of the difference: The first form will only be true for an entry that exists and doesn't have the value
undefinedin it. The second form will be true for anything that isn't falsey. There are several falsey values:null,undefined,0,"",NaN, and of course,false. So you wouldn't use the second form to decide whether there was a number at a position in the array, since0is a number butif (0)won't go into the body of theif.Speaking of ambiguity, note that comment about "...true for an entry that exists and doesn't have the value
undefinedin it..." Neither of the examples you gave differentiates between an entry that doesn't exist and an entry that exists with the valueundefined. If you need to differentiate those at some point (again, not for the use case you mentioned), you'd usehasOwnPropertyon the array:if (array.hasOwnProperty(row))
The second solution is shorter, so it could be quicker to execute, because JavaScript is an interpreted scripting language
This is a mistaken assumption. Most modern JavaScript engines are just-in-time optimizing compilers. V8 (the engine in Chrome), for instance, is a two-stage optimizing compiler: On the first pass, it turns your JavaScript into machine code quickly, and then if it identifies hotspots (bits of code that run a lot), it goes back and does more aggressive optimization there.