Skip to content

Instantly share code, notes, and snippets.

@alanhoff
Created July 16, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanhoff/a97717c0b734aa10e3c8 to your computer and use it in GitHub Desktop.
Save alanhoff/a97717c0b734aa10e3c8 to your computer and use it in GitHub Desktop.
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