var array = [];
function createObject(name, username, password) {
return {
name: name,
user: username,
pwd: password,
}
}
var obj1 = createObject("alice", "alice123", "abc")
var obj2 = createObject("dave", "davey", "pass123")
array.push(obj1, obj2);
and then you can choose between:
var resultMessage = 'array'.concat(':', ' ', JSON.stringify(array, null, 2))
alert(resultMessage)
/*
array: [
{
"name": "alice",
"user": "alice123",
"pwd": "abc",
},
{
"name": "dave",
"user": "davey",
"pwd": "pass123",
}
]
*/
or this...
array.forEach((arrItem) => {
var message = 'object'.concat(':', ' ', JSON.stringify(arrItem, null, 2))
alert(message)
})
/*
object: {
"name": "alice",
"user": "alice123",
"pwd": "abc",
}
*/
/*
object: {
"name": "dave",
"user": "davey",
"pwd": "pass123",
}
*/
or even this one...
var msg = array.reduce((_message, arrItem) => {
var objectMsg = 'object'.concat(':', ' ', JSON.stringfiy(arrItem, null, 2), '\n')
_message = _message.concat(objectMsg)
return _message
}, ''}
alert(msg)
/*
object: {
"name": "alice",
"user": "alice123",
"pwd": "abc",
}
object: {
"name": "dave",
"user": "davey",
"pwd": "pass123",
}
*/