0

I am having records like below in array:

$scope.skills = [];

$scope.skills['et']= s1
$scope.skills['et']= s2
$scope.skills['et']= s3

$scope.skills['gf']= t1
$scope.skills['gf']= t2
$scope.skills['gf']= t3

$scope.skills['po']= b1
$scope.skills['po']= b2
$scope.skills['po']= b3

Now I want to delete all records from array based on below value:

$scope.value ='gf';

Now I would like to delete all records from array whose index is other than 'gf':

So skills array should contain only record of 'gf' like below:

Expected output:

$scope.skills['gf']= t1
$scope.skills['gf']= t2
$scope.skills['gf']= t3
3
  • 1
    You are not using the array as an array. Giving it non-natural answer keys makes it basicly behave like an object. Besides that, you seem to overwrite the value over and over again. Commented Dec 8, 2016 at 14:52
  • @MoeSattler: my array is associative array in which I am dynamically creating index with et,gf etc storing corresponding skills .so that was just an example which I shown you. Commented Dec 8, 2016 at 16:04
  • I suggest that since all of the answers below were unsatisfactory, this question can be closed under the official 'Unclear' reason: Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Apr 5, 2018 at 18:27

3 Answers 3

1

You should be able to loop over all of the keys in the array object and delete the ones you don't want.

for(var k in Object.keys($scope.skills)){
    if(k !== "gf"){
        delete $scope.skills[k];
    }
}

As a side note for every assignment of the key you are overwriting the value so after the following

$scope.skills['gf']= t1
$scope.skills['gf']= t2
$scope.skills['gf']= t3

the value of $scope.skills['gf'] will be the value of the variable t3.

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

1 Comment

With this condition this is the match : 0 != 'gf' which is not matching and hence records except gf are not getting removed
0

I think there is something conceptually wrong with your example. Assigning multiply values to the same property will override previous assignments.

Anyway, if you only need a single property from an object you can create a new object with only that property:

$scope.skills = $scope.skills['gf'];

4 Comments

my array is associative array in which I am dynamically creating index with et,gf etc storing corresponding skills .so that was just an example which I shown you.so data are correct
Yeah, that's fine but assigning are['gf'] twice will overwrite the previous values. In JS, these are called just simple objects or maps, people don't usually talk about assoc arrays here.
But I am doing something like this:$scope.skills['qf']=response.skills.so here this response.skills is the data coming from server.like wise same for po and et.i hope you got my point.so this is how I am creating array.now tell me how my values are overwriting
In this example, they don't overwrite each other but in your original example they do. Just try to run that code in a console. But my answer is still the same as above. Each key is unique, so if you want to filter to a specific one, just access that element of the map.
0

You need to change the structure of the "array" to be able to store multiple items with the same key.

Example:

$scope.skills = [];

$scope.skills.push({key: 'et', value: 's1'});
$scope.skills.push({key: 'et', value: 's2'});
$scope.skills.push({key: 'et', value: 's3'});

$scope.skills.push({key: 'gf', value: 't1'});
$scope.skills.push({key: 'gf', value: 't2'});
$scope.skills.push({key: 'gf', value: 't3'});

$scope.skills.push({key: 'po', value: 'b1'});
$scope.skills.push({key: 'po', value: 'b2'});
$scope.skills.push({key: 'po', value: 'b3'});

Now you can delete like this:

$scope.keyToDelete = 'gf';

for(var i = $scope.skills.length - 1; i >= 0; i--) {
    if($scope.skills[i].key === $scope.keyToDelete) {
        $scope.skills.splice(i, 1);
    }
});

2 Comments

Sorry my array structure is not like this.my array is associative araay like dictionary
So why don't you change it so that you can do what you want? And as it has been stated earlier your example is a bit confusing since you override your previous assignments.

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.