Akanksha Iyer Akanksha Iyer - 2 months ago 12
AngularJS Question

Convert objects present in an object to NULL value

I have the following object names $scope.parameters. When i execute console.log as shown below, i get this result

console.log($scope.parameters);


Result

Object { Name: "Diana", id: 234, Size: Object, Location: Object, Details: "none" }
Name: "Diana"
id: 234,
Size: Object
Location: Object
Details: "none"


As the result shows the elements Size and Location as Object, i want it to be replaced with null. Also, i want it to be dynamic. for e.g. if any of the above elements are Object, it should replace it to null automatically.

Can someone please let me know how to achieve this.

Answer

Test out each key of the object with its type if object make them null

if (Object.getPrototypeOf($scope.parameters.Size) === Object.prototype) {
    // True if its object change it to null
    $scope.parameters.Size = null;
} else {
    // do nothing 
}

Make a function which takes parameters to test it out and return.

Comments