In this tutorial, you’ll learn how to create JSON Array dynamically using JavaScript. This is one of most common scenarios and you’ll see two ways of creating JSON array dynamically.

The first method will use for loop for creating JSON array from an input array.

let output = [];
let input = ["John","Hari","James"]
let tmp;

for(let i = 0; i < input.length; i++){
  tmp = {"name" : input[i]};
  output.push(tmp);
}

console.log(output)

The second method will make use of the JavaScript reduce method to create dynamic JSON array.

let output;
let input = ["Roy","Lee","Haris"]

output = input.reduce((accumulator, currentValue, index, array) => {
  accumulator.push({"name" : currentValue});
  return accumulator;
},[])

console.log(output)