I have a callback function that retrieves a data object from the DOM. Each time an item is selected, the function returns an object like this:
$scope.fClick = function( data ) {
$scope.x = data;
}
When an item is selected from the dropdown, the object looks like this:
{ name: "English", ticked: true }
When the item is deselected, the object returned is:
{ name: "English", ticked: false }
I have an array called $scope.output to store these returned objects. What I want to achieve is to add an object to $scope.output only if there isn't already an object with the same "name" value present in the array. Currently, both
{ name: "English", ticked: false }
and { name: "English", ticked: true }
are added to the array. I want to update the "ticked" property instead. For example, if $scope.output = { name: "English", ticked: false }
and the function returns { name: "English", ticked: true}, I want it to update the existing object's "ticked" property: $scope.output = { name: "English", ticked: true }
This is what I have attempted:
$scope.fClick = function( data ) {
$scope.x = data;
}
$scope.$watch(function () {
return $scope.y = $scope.x;
},function (newValue, oldValue) {
var id = $scope.output.indexOf(newValue);
if(id === -1){
$scope.output.push(newValue);
}
else {
$scope.output[id].tick = newValue.tick;
}
console.log($scope.output);
},true);
How can I make this implementation work as intended?