JSFiddle - React, Tailwind, and code Playground
by murray_3
JavaScript
var tmp1 = {
Tom: 'first',
Jane: 'second',
Sally: 'third',
Bill: 'forth',
Lydia: 'fifth',
Carl: 'sixth',
Stan: 'seventh',
Earl: 'eighth',
Paul: 'ninth',
Tray: 'tenth'
};
ObjectInject = function(object, newKey, newVal, fn, bind){
var prevKey=null, prevVal=null, inserted=false, newobject={};
for(var currKey in object){
if (object.hasOwnProperty(currKey)){
var currVal = object[currKey];
if( !inserted && fn.call(bind, prevKey, prevVal, currKey, currVal) ){
newobject[newKey] = newVal;
inserted=true;
}
newobject[currKey] = currVal;
prevKey = currKey;
prevVal = currVal;
}
}
if(!inserted) newobject[newKey] = newVal;
return newobject;
};
var tmp2 = ObjectInject(tmp1, 'Trent', 'NEW ITEM', function(pk, pv, nk, nv){
return (nk=='Earl');
});
printfun = function(object){
for(var i in object){
document.write(i);
}
};
printfun(tmp1)
document.write("<br>");
printfun(tmp2)