1

I have 2 arrays like below:

$scope.Stat.Statistics =[{val:10} , { val:11} , { val:13} ,{ val:14}]
$scope.selected = [{ val:12} , { val:13} ] //based on checkbox  selection

Now i just want to add selected value to Statistics array and remove those which doesnt exist in selected but ignoring 1st record of statistics.

Expected output

$scope.Stat.Statistics ={[ val:10]} , {[ val:12]} , {[ val:13]}

Code:

for (var i = 0; i < selected.length; i++) {
    for (var j = 1; j <= $scope.Stat.Statistics.length; j++) {
        //ignore 1st record for comparision
        if (selected[i].val != $scope.Stat.Statistics[j].val) {
            $scope.Stat.Statistics.splice(j, 1);
            $scope.Stat.Statistics[j].push(selected[i]);
        }
    }
}

Update:In case of same value in Statistics and selected i would like to keep statistics value.

Error:$scope.Stat.Statistics[j] is undefined

8
  • You just spliced out j, it doesn't exist in the array anymore Commented Oct 3, 2016 at 15:27
  • 2
    {[ val:10]} is not valid javascript. Commented Oct 3, 2016 at 15:27
  • please verify that definition... : {[ val:10]} , {[ val:11]} , {[ val:13]} ,{[ val:14]}... could be [ { val:10}, { val:11}... ] Commented Oct 3, 2016 at 15:27
  • Where you define the arrays is not valid javascript. I think you meant $scope.Stat.Statistics = [ {val:10}, {val:11}, ...]; Commented Oct 3, 2016 at 15:27
  • @NinaScholz Sorry i was in little hurry to post question so that was a mistake.but updated my question and thanks for pointing out that mistake Commented Oct 3, 2016 at 15:31

2 Answers 2

2
$scope.Stat.Statistics ={[ val:10]} , {[ val:11]} , {[ val:13]} ,{[ val:14]}

This is not valid Javascript. If you want an array, you should be defining it like:

$scope.Stat.Statistics =[{val:10} , { val:11} , { val:13} ,{ val:14}]

var Statistics = [{
  val: 10
}, {
  val: 11
}, {
  val: 13
}, {
  val: 14
}];

var selected = [{
  val: 12
}, {
  val: 13
}];

var found = selected;
Statistics.map(function(statistic) {
  selected.map(function(selectedItem) {
    if(selectedItem.val === statistic.val) {
      found.push(selectedItem); 
    }
  });
});

console.log(found);

Of course, I've factored out the Angular element out of it, but this same logic should work.

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry i was in little hurry to post question so that was a mistake.but updated my question and thanks for pointing out that mistake
This answer is now invalid after the edit. Please remedy
Your output is incorrect because everytime i need first record of statistics and all other records of selected and removing unmatched items from Statistics.So final output will be : 10,12,13
When we will have common val from both statistics and selected then in that case i would like to preserve data from statistics
"When we will have common val from both statistics and selected then in that case i would like to preserve data from statistics": In that case shouldn't the output be 10, 13?
|
1

You could take the first element of $scope.Stat.Statistics, you want to keep and concat $scope.selected to the first element.

var $scope = { Stat: {} };

$scope.Stat.Statistics = [{ val: 10 }, { val: 11 }, { val: 13 }, { val: 14 }];
$scope.selected = [{ val: 12 }, { val: 13 }];
$scope.Stat.Statistics = [$scope.Stat.Statistics[0]].concat($scope.selected);

console.log($scope.Stat.Statistics);

Edit: keeping common items with same val and append the rest to the array.

var $scope = { Stat: {} },
    hash = {},
    i;

$scope.Stat.Statistics = [{ val: 10 }, { val: 11 }, { val: 13, extra: 42 }, { val: 14 }];
$scope.selected = [{ val: 12 }, { val: 13 }];

$scope.selected.forEach(function (a) {
    hash[a.val] = a;
});

i = $scope.Stat.Statistics.length;
while (i--) {
    if (hash[$scope.Stat.Statistics[i].val]) {
        delete hash[$scope.Stat.Statistics[i].val];
        continue;
    }
    if (i === 0) {
        continue;
    }
    $scope.Stat.Statistics.splice(i, 1);
}
Object.keys(hash).forEach(function (k) {
    $scope.Stat.Statistics.push(hash[k]);
});

console.log($scope.Stat.Statistics);

6 Comments

But i want to remove those value from statistics which doesnt exist in $scope.selected
@Learning And that's exactly what you will get. At the end $scope.Stat.Statistics will equal [{val:10} , { val:12} , { val:13}]
Ok this is working fine but will it be possible that suppose if i have val i.e 13 which is common in both so is that case i would like to preserve it instead of overwriting 13 with selectedvalue
you couold keep common values, but with fany filtering, you get the same, if the object inside of the arrays are really identical.
Actually objects are identical but but i have 2 extra properties in Statictics which i would like to preserve in case of same value in both the array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.