-1
var names = {};

// PUTTING DATA TO tmpChatters output example is.

[ { name: 'aaa', age: '', sex: 'man'},
  { name: 'bbb', age: '', sex: 'female'} ]


function deleteFunction(currentName) {

}
deleteFunction('aaa');

So deleteFunction must empty where names eqauls the name inside the object. How do i need to do this ?

DLeh
  • 23,806
  • 16
  • 84
  • 128
Inna
  • 423
  • 1
  • 4
  • 13
  • 1
    So you don't want to EMPTY the array, you want to delete the object where `name` is `aaa`, correct? – PlantTheIdea Mar 25 '15 at 18:34
  • 2
    can you show us what you've tried? – dfperry Mar 25 '15 at 18:35
  • 1
    possible duplicate of [javascript find and remove object in array based on key value](http://stackoverflow.com/questions/21659888/javascript-find-and-remove-object-in-array-based-on-key-value) – Tom Mar 25 '15 at 18:36
  • So you are deleting a single object from an array of objects based on the value contained in an arbitrary field in the the object? – Mike Brant Mar 25 '15 at 18:36

2 Answers2

1

Just loop over it and slice the object out of the array:

for(var i = yourArray.length; i--;){
    if(yourArray[i].name === currentName){
        yourArray.splice(i,1);
        break;
    }
}

Should give you what you want. If there can be multiple names that match, just remove the break to loop over them all.

PlantTheIdea
  • 16,061
  • 5
  • 35
  • 40
1

Try it as a forEach();

   names.forEach(function(a, b){
        if(curretNick === a.name){
            names.splice(b, 1);
        }
    });
Hakan Kose
  • 1,649
  • 9
  • 16