1

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.

Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • 1
    I can't get what you want to do: you need to change the value of the property "name" of your array? – illeb Oct 07 '16 at 06:56
  • Yes i want to change the myObject Array name That have the red color – Muhammad irfan Mayo Oct 07 '16 at 06:59
  • But i want to update it after Filter result i got match – Muhammad irfan Mayo Oct 07 '16 at 07:00
  • 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 Oct 07 '16 at 07:02
  • 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 Oct 07 '16 at 07:05
  • @MuhammadirfanMayo If I understand, you are trying to fetch objects based on a filter criteria and wish to update name in those objects. Question is do you wish to update original array or create a copy of filtered objects and have temporary update in them? Also, if you wish to tag someone, use `@` followed by his name in comment. – Rajesh Oct 07 '16 at 07:12

5 Answers5

0

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.

illeb
  • 2,942
  • 1
  • 21
  • 35
  • are you saying on the currentObject place i can put myObjects? variable – Muhammad irfan Mayo Oct 07 '16 at 07:10
  • currentObject is a single iterated element of your myObjects array. – illeb Oct 07 '16 at 07:12
  • 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 Oct 07 '16 at 07:19
  • Follow this answer http://stackoverflow.com/questions/18247130/how-do-i-store-data-in-local-storage-using-angularjs Remember to accept the answer if your issue is solved – illeb Oct 07 '16 at 07:23
  • Briefly, to save: sessionStorage.setItem('key', angular.toJson(myObject)); And to retrieve: var object = angular.fromJson(sessionStorage.getItem('key')); – illeb Oct 07 '16 at 07:24
0

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";
Sambath Kumar S
  • 407
  • 5
  • 12
0

$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';
    }
);
0
    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";
        });
nmanikiran
  • 2,866
  • 15
  • 19
0

Luxor001 answer is right. it's i required and working nice basically i want to change the name of original value not filter array value and with the help of Map it's working cool.

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