var obj = {
x: 45,
b: {
func: function(){alert('a')},
name: "b"
},
a: "prueba"
};
var nw = moveProp(obj, "a", "x"); //-- move 'a' to position of 'x'
console.log(nw, obj);
//--
function moveProp(obj, moveKey, toKey){
var temp = Object.assign({}, obj)
var tobj;
var resp = {};
tobj = temp[moveKey];
delete temp[moveKey];
for(var prop in temp){
if (prop===toKey) resp[moveKey] = tobj;
resp[prop] = temp[prop];
}
return resp;
}
Move object property to another position
1 Response
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.