arr = [
{"id":"1"},
{"id":"2"}
];
For some reason I want to change the "id" to "uid". I am stuck here
arr.forEach(function(i){
});
Just do like bellow:
arr = [{
"id": "1"
},
{
"id": "2"
}
];
arr = arr.map(function(obj) {
return {
"uid": obj.id
}
});
console.log(arr);
Here you go:
arr.map(function (a) {
a.uid=a.id;delete a.id;
return a;
});
This just goes through the array, renames it, and returns the value.
Snippet:
var arr = [{
"id": "1"
}, {
"id": "2"
}];
arr = arr.map(function(a) {
a['uid'] = a['id'];
delete a['id'];
return a;
});
console.log(arr);
forEach so here's an answer with it.
arr.forEach(function (a) {
a.uid=a.id;delete a.id;
});
arr = [{
"id": "1"
},
{
"id": "2"
}
];
arr = arr.map(function(item, index) {
// forget about the index, e.g. running from 0 to arr.length - 1
return {
uid: item.id
};
});
console.log(arr);