Reputation: 1347
This is my code
var data={};
data={stdId:"101"};
data={empId:"102"};
data={deptId:"201"};
I have a data object getting data from services with one key only. but key names are different like stdId
or empId
,etc.
I want to assign empty value to stdId
or empId
,etc. like data={stdId:""}
.
The key names are changed dynamically based on services.
Upvotes: 2
Views: 13363
Reputation: 1095
You can use for..in
loop to iterate over data object using key.
for(var key in data){
if(data.hasOwnProperty(key)){
data[key] = '';
}
}
data
object to ''
Upvotes: 2
Reputation: 2961
As I understand your question, you won't know the name of the key in each object and there is only ever one. To solve your problem:
data[Object.keys(data)[0]] = ''
This will assign the value of the key to null.
Upvotes: 0
Reputation: 71911
Not entirely sure what you are trying to achieve, but if you know the name of the property you could use:
data['stdId'] = '';
or
data.stdId = '';
If you do not know the property name, but you still want to keep the property names on the object you could use:
for(var prop in data) {
if(data.hasOwnProperty(prop)) {
data[prop] = '';
}
}
Upvotes: 4
Reputation: 5957
data.stdId = null;
or
data.stdId = '';
or
data = { stdId };
Have you tried those?
Upvotes: 0