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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With