Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 remove properties prefixed with dollar sign ($) from objects

Consider the following HTML

<some-component [input]="something" (change)="update(event)"></some-component>

and the update() function

update(event) { 
    console.log(event);
    this.firebaseRef.update(event.$key, event);
  }

this logs:

$exists: function()
$key: "initial"
end: "1.20"
options: Object
start: "0.20"
text: "Dit is een test"

And throws this error

Firebase.update failed: First argument contains a function in property

How do I pass the data from the even without the dollar-sign prefixed properties that have been appended by Angular? I'd prefer to do this without manually constructing a new data object.

like image 452
matthiasdv Avatar asked Feb 13 '26 03:02

matthiasdv


1 Answers

It's not a good ideia at all, but you can loop through the object properties and filter those which doesn't start with $ or are functions as well. But I recomend you to change the way your event is sending data to the handler, so then you don't have to sanitize extraordinary cases.

function getObjectWithoutDollarSignNameAndFunctions(obj){

  var keys = Object.keys(obj).filter(function(key){
    return typeof obj[key] !== 'function' && key.indexOf('$') !== 0;
  });

  return keys.map(function(key){ return obj[key] });    
}

var myObject = {
  $angularKey: 1,
  normalFunction: function(){},
  normalKey: 1,
  normalKey2: {a:12}
};

var myNewObject = getObjectWithoutDollarSignNameAndFunctions(myObject);

console.log(myNewObject);
like image 79
lenilsondc Avatar answered Feb 15 '26 11:02

lenilsondc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!