This is my array:
data = [{"src": 'a'}, {'src': 'b'}, {'src': 'c'}]; But I want to change key like this:
data = [{"letter": 'a'}, {'letter': 'b'}, {'letter': 'c'}]; Use map
var output = data.map( s => ({letter:s.src}) ); Demo
var data = [{ "src": 'a' }, { 'src': 'b' }, { 'src': 'c' } ]; console.log(data.map(s => ({ letter: s.src }))); But if there are multiple other keys and you only want to change src from it then
var output = data.map( s => { if ( s.hasOwnProperty("src") ) { s.letter = s.src; delete s.src; } return s; }) Demo
var data = [{ "src": 'a' }, { 'src': 'b' }, { 'src2': 'c' } ]; var output = data.map(s => { if (s.hasOwnProperty("src")) { s.letter = s.src; delete s.src; } return s; }) console.log(output); The easiest way is to use map method. Check it out the documentation
data.map(function(item) { return { letter: item.src }; })