I am facing an issue with the indexOf
function in my code. I have a collection called placeCollection
which contains an array named locFrnd
. Since I couldn't use the indexOf
function directly on the locFrnd
array of objects, I created a new array called dup
to store the data for performing the desired operations. The incoming data source is $scope.Friend
, which is also an array that may contain multiple values. My goal is to compare each value in $scope.Friend
with the values in the locFrnd
array. If a value is not present in locFrnd
, I want to push it into the array. The issue I am facing is that the indexOf
operation is only referring to the last value in dup
, which is causing unexpected behavior.
//if country exist
else if (cnt_exist == 1) {
alert("country exist");
var len = $scope.placeCollection[cnt_i].locFrnd.length;
for (var j = 0; j < len; j++) {
var dup = [];
dup[j] = $scope.placeCollection[cnt_i].locFrnd[j].name;
}
//check for friend now
alert("checking for friend");
//some code has to be inserted here to handle Friends as it is an array
alert($scope.Friend.length);
for (var k = 0; k < $scope.Friend.length; k++) {
var frnd_exist = 0;
alert($scope.Friend[k]);
alert(dup.indexOf($scope.Friend[k]));
if (dup.indexOf($scope.Friend[k]) != -1) // friend exist
{
alert("entered friend comparison");
frnd_exist = 1;
}
if (frnd_exist == 1) // if friend does not exist
{
alert("friend exist");
} else if (frnd_exist == 0) {
var eachFriend = {
name: $scope.Friend[k]
}
$scope.placeCollection[cnt_i].locFrnd.push(eachFriend);
}
}