Javascript array value is undefined ... how do I test for that
and
How to check a not-defined variable in JavaScript
these are wrong as far as I'm concerned :
I ONLY get :
when trying to :
console.log(!fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7].value.firstInput);
console.log(fromToParameters[7].value.firstInput === undefined);
console.log(typeof fromToParameters[7].value.firstInput == 'undefined');
console.log(fromToParameters[7].value.firstInput !== undefined);
console.log(typeof fromToParameters[7].value.firstInput != 'undefined');
but this (the entry exists) works fine :
console.log(!fromToParameters[0].value.firstInput);
console.log(!!fromToParameters[0].value.firstInput);
console.log(fromToParameters[0].value.firstInput === undefined);
console.log(typeof fromToParameters[0].value.firstInput == 'undefined');
console.log(fromToParameters[0].value.firstInput !== undefined);
console.log(typeof fromToParameters[0].value.firstInput != 'undefined');
false
true
false
true
false
true
is it a question of react being different from js? why can't I do the same as in these stackoverflow threads?
UPDATE :
so you cannot point to a missing array element at all.
Check answers below.
I think I'll be using an array.lenght stored in a const that I then check my increment against it within my "for" loop to allow or disallow modifying my array entries on a case-by-case basis.
it's really annoying that you can't just ask js if a damn var of an unexisting array index exists or not.
it seems like this would be straightforward stuff : can't point to the index? then NO. no not this variable nor any other variable exists at this index. end of.
the guys at js definitely should put in a note for adding something as simple as this.
I'm tempted to post my code as I have something that allows for me to do what I want (calling a index with lots of undefineds and getting a object with ""s instead) but it's a bit monstrous.
You should do like this:
console.log(fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log(!!fromToParameters[7] && fromToParameters[7].value.firstInput);
console.log( fromToParameters[7]&& typeof fromToParameters[7].value.firstInput == 'undefined');
I have just added check. So if fromToParameters[7] is undefined or null, your code will not break.
Check for the index first (ex: console.log(!yourArray[x]), then depending if that test passes or fails, access/add the indexes/properties you want.
Related
I have a list of objects that gets returned from the font end as
item=["false","true"]
i take these items to check a column from my records to see which values contain true or false as follows
this.records.filter(x=> items.includes(x.column))
but my records are always returning empty.
if i hard code it as example this.records.filter(x=> x.column == false) it works.
But i am using includes() because a user can select both true or false options and i cant go hard code it cause i wont know what the user might select.
how do i convert the string ["true","false"] into [true,false]?
i tried declaring items as
public items:boolean[]=[];
but still no luck
You can convert it using items.map(item => item === "true")
I am fetching an array of objects from an RX/JS call from an http backend. It returns an object which I am then trying to work with. I am making changes to this object using a for loop (in this example I am trying the .forEach because I have tried a number of different things and none of them seem to work.
When I run the code, I get a very weird problem. If I return the values of the properties, I get the new values (i.e. correctionQueued returns as true, etc.) but in the very next line, when I return the object, those same values are the same as the original (correctionQueued === false, etc.) HOWEVER, correctionStatus (which does not exist on the original object from http) sets just fine.
I don't understand how
array[index].correctionQueued can return true, but
array[index] returns an object with correctionQueued as false.
After the loop, the original array (checklistCopy) is identical to the object before the forEach loop, except the new property (correctionStatus) is now set, but all properties that I changed that were part of the original object remain as they were.
I have tried using a for of, for in, and .forEach. I have used the index to alter the original array, always the same result. Preexisting properties do not change, new properties are added. I have even tried working on a copy of the object in case there is something special about the object returned from rxjs, but to no avail.
checklistCopy.forEach((checklistItem, index, array) => {
if (checklistItem.crCode.isirName === correctionSetItem) {
array[index].correctionQueued = true;
array[index].correctionValue = mostRecentCorrection.correctionValue;
array[index].correctionStatus = mostRecentCorrection.status;
console.log(array[index].correctionQueued, array[index].correctionValue, array[index].correctionStatus);
console.log(array[index]);
}
}
);
I don't get an error, but I get..
Original object is:
correctionQueued: false;
correctionValue: JAAMES;
--
console.log(array[index].correctionQueued, array[index].correctionValue, array[index].correctionStatus);
true JAMES SENT
but when I print the whole object:
console.log(array[index]);
correctionQueued: false;
correctionValue: JAAMES;
correctionStatus: "SENT'; <-- This is set correctly but does not exist on original object.
console.log(array[index]) (at least in Chrome) just adds the object reference to the console. The values do not resolve until you expand it, so your console log statement is not actually capturing the values at that moment in time.
Change your console statement to: console.log(JSON.stringify(array[index])) and you should discover that the values are correct at the time the log statement runs.
The behavior you are seeing suggests that something is coming along later and changing the object properties back to the original value. Unless you show a more complete example, we can't help you find the culprit. But hopefully this answers the question about why your logs show what they show.
Your output doesn't make sense to me either but cleaning up your code may help you. Try this:
checklistCopy.forEach(checklistItem => {
checklistItem.correctionQueued = checklistItem.crCode.isirName === correctionSetItem;
if (checklistItem.correctionQueued) {
checklistItem.correctionValue = mostRecentCorrection.correctionValue;
checklistItem.correctionStatus = mostRecentCorrection.status;
console.log('checklistItem', checklistItem)
}
}
);
I am trying to use IF statement in $http.post {} section.
I have written the code as below:
[controller.js]
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
//step_one start
if(step_number == 1){ // This is where I get an error.
project_id : $scope.project_data.project_id,
project_title : $scope.project_data.project_title
}
})
.then(function(response){
// do something here
});
However, I get an error on (step_number == 1) part with red underline on '==' part.
I thought it would be working in a simple IF statement form.
Perhaps, I am not using the IF statement in correct comparison syntax..
I have no idea why it is giving me a red line on the '=='.
Does anyone know what could possibly wrong? Please advise me how to fix this error.
Thank you so much in advance!!!
The problem here is that the second parameter for the $http.post function is an object, and you can't use if statements when you are creating an object literal.
That's not an AngularJS thing - it's flat out invalid JavaScript.
There are many possible ways you could do what you're trying to do.
One possible solution is:
$http.post("../crud/projects_update.php",{
step_number : $scope.step_number,
project_id : $scope.step_number === 1 ? $scope.project_data.project_id : undefined,
project_title : $scope.step_number === 1 ? $scope.project_data.project_title : undefined
})
I'm trying to filter a list of users from a Firebase query so that I only get users that are NOT listed in one of two arrays. The code I'm using doesn't work:
let users = snapshot.childSnapshots.map {
User(snapshot: $0)
}.filter{
guardiansArray.contains($0.key) == false || dependentsArray.contains($0.key) == false
}
If I remove the ==false code, I get the opposite effect of what I want: I get a list of users that ARE in either of the two arrays. How can I get the reverse effect?
Thanks!
It looks like you want to use && instead of ||.
I guess this means there is a circular reference somehwere but for the life of me I can't guess how to fix it.
Anyone have any ideas?
http://plnkr.co/edit/aNcBcU?p=preview
Check the debug console in Chrome (for example) and you'll see the error.
The offending line is
scope.map = map;
scope.map is being "$watched" on the controller via
$scope.$watch("options.map", function (map) { ... }, true);
It's because you're comparing for object for equality rather than for reference. Change your $watch statement to this:
$scope.$watch("options.map", function (map) {
if (map === undefined) {
alert("map has no value");
} else {
alert("map is defined");
}
});
I also had this issue and found out that the objects I was comparing had circular references. (Tried JSON.stringify() which threw 'TypeError: Converting circular structure to JSON').
When I edited my object so that it didn't have circular structure, I fixed this problem and compared objects not by reference but by properties value which was what I needed.
The third parameter of $watch function tells how to compare the watched object. False to reference comparing only. True to recursive equality comparing, if an object contains circular references, then over maximum stack size. For example:
var a = {ref:b};
var b = {ref:a};
$scope.$watch('b', function(){
//code here will never called, because stack overflow when comparing the object b.
}, true)