I rewrote your code:

$scope.newProductKeyArray = [];

// I know this looks silly, but it works great. Basically, we are 
defaulting down the line
// if none the variables exist. If `value` doesn't exist, then an empty 
object is created.
// If `productKey doesn't exist, then an empty array is created. What this 
does is stop you
// from having to check if `productKey` exists because it will always exist 
now
var key = (value || {}).productKey || [];

// I highly suggest using strict equals (===)
if (key.indexOf($scope.productKeyId) === -1) {
// Push `productKeyId` onto the `productKey` property
key.productKey.push(productKeyId);
$scope.newProductKeyArray = key.productKey;

// I changed `albumObj != undefined` to `angular.isDefined()` because I 
feel it's,
// well, I'm not sure why haha. I just like it (feel free to change it back 
- using strict equals though)
if (albumObj !== null && angular.isDefined(albumObj)) {
albumObj["shared"] = true;
}
$scope.newProductKeyArray.push(albumObj);
//After selected the user move the checkedUser into the allowBUserObjects 
$(event.currentTarget).parents(".albumSection").hide("slow");

// We want to store what we've just done onto the `value` object. However,
// `value = key` won't work because we would be saving only the reference 
of `key` and not the actual 
// values. So, a simple trick to assign objects to variables without 
keeping reference is 
// by converting the object to a JSON string and then parsing that string 
back into an object
value = JSON.parse(angular.toJson(key));
// Calling inner function.
updateArray();
} else {
console.log("Product already in album.");
}

I hope this works for you. I put comments in the code explaining the 
changes.

The reason you got that error was because you were trying to use a method, 
indexOf, on something that wasn't an array.

Let me know if you have any questions!


On Tuesday, July 4, 2017 at 12:16:05 AM UTC-4, Kapil Soni wrote:
>
>                                           // Adding new product key to 
> existing productKey of album.
> $scope.newProductKeyArray =[];
>
> // Push only if not found in array.
> if(value.productKey != undefined) {
>  // product key found adding another key..
>  if(value.productKey.indexOf($scope.productKeyId) == -1) {
>
>                                                                           
>          //Here value.productKey.indexOf($scope.productKeyId) this line 
> will be generate the error 
>                                                                           
>           //value.productKey.indexOf is not a function
>   // Adding productKeyId to add this new productKeyId.
>  value.productKey.push(productKeyId);
>  $scope.newProductKeyArray = value.productKey;
>  
>  if(albumObj != null && albumObj != undefined) {
> albumObj["shared"] = true;  
>  }
>  $scope.newProductKeyArray.push(albumObj);
> //After selected the user move the checkedUser into the allowBUserObjects 
> $(event.currentTarget).parents(".albumSection").hide("slow");
>   // Calling inner function.
>   updateArray();
>  } else {
>   console.log("Product already in album.");
>  }
> }}
>
>                                                                       
>  //Sir tell me how to fix this error?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to