Here the question,
I have an Object
contain several elements in it
Example:
obj = {
"0": { "a": "a1", "b": "b1"},
"1": { "a": "a2", "b": "b2"}
};
And now, i wish to remove/delete the element 0
, so it will be only element 1
left, but instead of the expected result of deleting element from an Object
, i would like it act like an Array
, the second element will replace the first element, with the example above, 1
will rename into 0
as well as all the element following onward.
Note: It may contain more than 2 element in the object, and is not necessary will remove the first element, it could be any element within the object
Here the solution i done so far,
var index = 0 //just for demo purpose
var tempArr = Object.keys(obj).map(function(key){
return obj[key];
});
tempArr.splice(index,1);
obj = tempArr.reduce(function(acc, cur, i){
acc[i] = cur;
return acc;
}, {});
result:
obj = {
"0": { "a": "a2", "b": "b2"}
}
This solution works and gave the result I want, but is there any better to do this?
Edited: As conclusion of the suggestion and discussion
Alternative way to solve the problem, which giving the almost identical result as my solution.
function Delete(index) {
delete obj[index];
var keys = Object.keys(obj);
var update = false;
for (var i = index + 1; i <= keys.length; i++) {
obj[i - 1] = obj[i];
update = true;
}
if (update) delete obj[keys[keys.length - 1]];
}
by @Anthony McGrath
delete obj[index];
obj = Object.keys(obj).reduce(function(newobj,key,i){
newobj[i]=obj[key];
return newobj;
},{});
by @Alexandru-Ionut Mihai
obj.length = Math.max.apply(null, Object.keys(obj))+1;
Array.prototype.splice.call(obj, index, 1);
delete obj.length; // optionally delete the length again
by @t.niese
Appreciated the helps once again.