I have an array.
messages = ['hi', 'heelo', 'apple'];
Write a function to return:
[
{ msg: messages[0], type: 'A', label: 'msg' },
{ msg: messages[2], type: 'A', label: 'msg' },
{ msg: messages[2], type: 'A', label: 'msg' }
]
You can use Array#map:
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
var messages = ['hi', 'heelo', 'apple'];
var messages_1 = JSON.stringify(messages.map(function(el) {
return {
msg: el,
type: "A",
label: "msg"
};
}));
console.log(messages_1);
[0]and then double[2]? Doesn't make any sense. And these hardcodedtypeandlabelkeys...