Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have the following:

sample collection of objects

var myObjects = [
    { name: "Object1", shape: "circle", color: "red" },
    { name: "Object2", shape: "square", color: "orange" },
    { name: "Object3", shape: "triangle", color: "yellow" },
    { name: "Object4", shape: "circle", color: "green" },
    { name: "Object5", shape: "sphere", color: "blue" },
    { name: "Object6", shape: "hexagon", color: "indigo" },
    { name: "Object7", shape: "square", color: "violet" }
];

I get an array of just the objects matching specific criteria.return an array with only red objects

var myRedObjects = $filter('filter')(myObjects, { color: "red" });

Now my problem is that how I can update filter object name in array myObjects.

share|improve this question
1  
I can't get what you want to do: you need to change the value of the property "name" of your array? – Luxor001 37 mins ago
    
Yes i want to change the myObject Array name That have the red color – Muhammad irfan Mayo 34 mins ago
    
But i want to update it after Filter result i got match – Muhammad irfan Mayo 33 mins ago
    
When you use filter, it will return a new array and references of object. You can manually loop over array and set if matching value is found – Rajesh 31 mins ago
    
bro i want to know any way here now how i can update if filter have the object then update the upper myObjects.name = 'any name' – Muhammad irfan Mayo 28 mins ago

Change your filter usage to this:

var myRedObjects = $filter('filter')(myObjects, { color: "red" }).map(function(currentObject){
   currentObject.name = "myNewName",
}

map will iterate your filtered array (much like a foreach) and will let you change the name of the current iterated object.

Bear in mind that map it's compatible with all browser from IE <= 9. If you are targeting older browser, you'll need to add a polyfill or not use map at all (a simple for will do the work). You can find more on map function here.

share|improve this answer
    
are you saying on the currentObject place i can put myObjects? variable – Muhammad irfan Mayo 23 mins ago
    
currentObject is a single iterated element of your myObjects array. – Luxor001 22 mins ago
    
Yup it's working Good bro thanks for this help. i need one more help bro i want to save this myObjects into local storage via angular can i do it? mean complete object i want to use – Muhammad irfan Mayo 15 mins ago
    
Follow this answer stackoverflow.com/questions/18247130/… Remember to accept the answer if your issue is solved – Luxor001 11 mins ago
    
Briefly, to save: sessionStorage.setItem('key', angular.toJson(myObject)); And to retrieve: var object = angular.fromJson(sessionStorage.getItem('key')); – Luxor001 10 mins ago

To update an array item in AngularJs we first need to find the item index then assign new value using the index. Here is an example of updating an item in array.

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
$scope.myArray = ['a','b','c','d','e'];
$scope.updateElement = function(item) {
	 var index = $scope.myArray.indexOf(item);
	 if(index > -1){
          $scope.myArray[index] = '100'; // will update item 
         }	 
};
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
<p>Update b in array = {{myArray}}</p>
<input type="button" value="Update b " ng-click="updateElement('b')"> 
</div> 

</body>  
</html>                            

share|improve this answer
    
no bro it's not like that i want – Muhammad irfan Mayo 23 mins ago

Try this

var myObjects = [
{ name: "Object1", shape: "circle", color: "red" },
{ name: "Object2", shape: "square", color: "orange" },
{ name: "Object3", shape: "triangle", color: "yellow" },
{ name: "Object4", shape: "circle", color: "green" },
{ name: "Object5", shape: "sphere", color: "blue" },
{ name: "Object6", shape: "hexagon", color: "indigo" },
{ name: "Object7", shape: "square", color: "violet" }
];

var myRedObjects = $filter('filter')(myObjects, { color: "red" });

myRedObjects = myRedObjects[0].name= "RedObjectNewName";
share|improve this answer
    
sambath Kumar s thanks to reply but Luxor001 solution meet my requirements any how thank u. – Muhammad irfan Mayo 9 mins ago

$filter gives you the filtered elements by reference, so you can update the members of $filter('filter')(myObjects, { color: "red" }) directly and it would make changes in myObjects variable.

Try following code:

var myRedObjects = $filter('filter')(myObjects, { color: "red" });
for (var index in myRedObjects) {
    myRedObjects[index].name = 'newName';
}

Or, if you are using underscore.js, then:

_.each(
    $filter('filter')(myObjects, { color: "red" }),
    function(item){
        item.name = 'newName';
    }
);
share|improve this answer
    var myObjects = [
            { name: "Object1", shape: "circle", color: "red" },
            { name: "Object2", shape: "square", color: "orange" },
            { name: "Object3", shape: "triangle", color: "yellow" },
            { name: "Object4", shape: "circle", color: "green" },
            { name: "Object5", shape: "sphere", color: "blue" },
            { name: "Object6", shape: "hexagon", color: "indigo" },
            { name: "Object7", shape: "square", color: "violet" }
        ];

        var myRedObjects = $filter('filter')(myObjects, { color: "red" }); 
        // filter returns Array so
        myRedObjects.forEach(function(Obj) {
            var idx = myObjects.indexOf(obj);
            myObjects[idx].name = "Update name";
        });
share

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.