0

I have this array of objects:

$scope.frequencies = [{Id:124,clientId:3,name:'qqq'}, 
                      {Id:589,clientId:1,name:'www'}, 
                      {Id:45,clientId:3, name:'eee'},
                      {Id:567,clientId:1,name:'rrr'},
                      {Id:45,clientId:3,name:'eee'},
                      {Id:567,clientId:7,name:'rrr'}]

I need to remove all items from array above except where clientId = 3.

How can I implement it?

2

4 Answers 4

4

Don't remove but re-assign with

var $scope = {};

$scope.frequencies = [{Id:124,clientId:3,name:'qqq'}, 
                      {Id:589,clientId:1,name:'www'}, 
                      {Id:45,clientId:3, name:'eee'},
                      {Id:567,clientId:1,name:'rrr'},
                      {Id:45,clientId:3,name:'eee'},
                      {Id:567,clientId:7,name:'rrr'}];

$scope.frequencies = $scope.frequencies.filter(item => item.clientId === 3);

console.log($scope.frequencies);

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

2 Comments

Please, you should let OP share his effort. Its a very common issue and you should not answer such question.
Array#splice for in-place changes - just sayin, because you can't always re-assign an array without unwanted side effects
2

You can simply use this code snippet:

 $scope.frequencies = $scope.frequencies.filter((item)=> item.Id!=3);

4 Comments

Why are you using .splice()? What is the a in a.length?
It was by mistake, I edit that, instead of a.length it's $scope.frequencies.length
OK, so that brings me back to "Why are you using .splice()? It's redundant, because with params (0, $scope.frequencies.length) its return value will be all of the items from the array produced by .filter(), so you'd get the same result without it...
Yes, you are right it's redundant,there is no need to splice. We can simply assign the filters value to $scope.frequencies.
1

In lodash, you could use _.remove(array, [predicate=_.identity]).

Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. The predicate is invoked with three arguments: (value, index, array).

var $scope = {};

$scope.frequencies = [{ Id: 124, clientId: 3, name: 'qqq' }, { Id: 589, clientId: 1, name: 'www' }, { Id: 45, clientId: 3, name: 'eee' }, { Id: 567, clientId: 1, name: 'rrr' }, { Id: 45, clientId: 3, name: 'eee' }, { Id: 567, clientId: 7, name: 'rrr' }];

_.remove($scope.frequencies, a => a.clientId !== 3);

console.log($scope.frequencies);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

Comments

0

Use filter method which based on a comparison it returns an array of elements that meet the filter. On ES5:

$scope.frequencies.filter(function(element){
   return element.clientId == 3; 
})

On newer, ES6:

$scope.frequencies.filter( element => element.clientId != 3)

Check filter documentation: filter

Comments

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.