Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var insert = function(object, index, key, value) {
var rebuilded = {};
var keys = Object.keys(object);
keys.splice(index, 0, key);
keys.forEach(function(k) {
if (k === key)
rebuilded[key] = value;
else
rebuilded[k] = object[k];
});
return rebuilded;
};
var foo = {
a: 1,
b: 2
};
// Inserir a chave 'c' contendo 3 na posição 0 do objeto foo
console.log(insert(foo, 0, 'c', 3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.