Convert a 1d array into an object in Google Apps script

Viewed 86

I want to be able to convert an array of values like this : ["apple", "pear", "orange","banana" ] into an array in this format

[
{"value": "apple"},
{"value": "pear"},
{"value": "orange"},
{"value": "banana"}
]

I'd be grateful for any advice.

1 Answers

Use .map :

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ 
const input = ["apple", "pear", "orange","banana"],
  output = input.map(fruit => ({value: fruit}));
console.log(output);
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Related