javascript - Check if object is in AngularJS array that is in localStorage -


I have this angular array: $ scope.fav = [] where items (objects) Meet the function call added with it. An example of these items is {citation: "some text", controller: some_controler} when I add a new object to the array, the array is saved in the local store. The array really sets me how to print it in console.log (), how does it actually work to drop it? I get many [objects, objects] that I have just taken, the way it has been printed. This is not the main issue, because the array works how to do it.

The problem I am trying to figure out whether an object is already in an array or not. I have tried

if ($ scope.fav.indexOf ({citation: q, controller: c}) == -1)

and It did not seem that because every object was also index-1, even if it was already in the array. I guess this is because this object is not reading properly.

Finally I have supported this function:

  $ scope.containsObject = function (obj, list) {Var i; (I = 0; i & lt; list.length; i ++) for {if (list [i] === obj) {return true; } } return false;   

}

who checks in the object array how do I call it:

  $ scope.addToFav = function (Q, c) {$ scope.value = $ scope.containsObject ({citation: q, controller: c}, $ scope.fav) console.log ($ scope.value);   

}

I'm getting a negative value for $ scope.value, even if the object is in the array

Thank you for your insight, Array.indexOf () and === operator Comparison of object references and only true when comparing context of the same instance of an object. There is a completely different object example compared to those in the {quote: q, controller: c} array, even if its properties match an object in an array.

The assistant code is called angular.equals () that the check for 2 objects is equivalent to you === ...

  $ scope.containsObject = can replace the function (obj, list) {var i; (I = 0; i & lt; list.length; i ++) {if (conular.quals (list [o], obg)) {true; } } return false; };    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -