You need to operate on the objects in your array, not the strings containing their indices in the array.
You should also use a regular loop to iterate over the array.
Your JSFiddle, fixed:
var x = [ {id:'1', img:'img1'}, {id:'2', img:'img2'}, {id:'3', img:'img3'} ]; var resp = {}; for( var i = 0 ; i < x.length ; i++ ){ var obj = x[i]; resp[obj.id] = obj.img; } document.write( JSON.stringify(resp, undefined, 2) );
source share