Home Why doing someVar in someArray in an If always give false value
Reply: 0

Why doing someVar in someArray in an If always give false value

user1566 Published in April 22, 2018, 12:44 am

I was just working on something in AngularJS. I noticed that by using the attached style of if condition, I always get a false result. Why so? Am I missing something? I tried to google it but didn't find anything.

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    var allowedExtensions = [".jpg",".pdf", ".png"];
    var testExt = ".jpg";

    if(testExt in allowedExtensions){
       $scope.isAllowed = true;
       //will always go here why?
       $scope.isAllowed = false;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
  Is Extension Allowed : {{isAllowed}}

I am from .NET background so we use LINQ in that kinda same like that. Is this type of syntax not supported in JS?

EDIT I know about index of and other ways to find if it is in the array. But I was confused why in operator is not working.

Thank you for your time and help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.345963 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO