Michael Michael - 3 months ago 6
Javascript Question

Removing items from javascript array

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?

Answer

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);