var myObj = { child1: "data1", child2: "data2", child3: "data3", child4: "data4" }; var a = (function addAfterChild(data, trigChild, newAttribute, newValue) { var newObj = {}; Object.keys(data).some(function(k) { //iterate through the keys and keep pushing them to the new object newObj[k] = data[k]; if (k === trigChild) { //if the selected attribute is found, push the new value newObj[newAttribute] = newValue; } }); myObj= newObj; })(myObj, "child2", "NEW_KEY", "NEW_VALUE"); document.getElementById("result").innerHTML = JSON.stringify(myObj);
<p> Result after adding new attribute after "child2": </p> <p id="result"> </p>