{status: 'approved', message: '123'}
{status: 'declined', message: '3123123'}
{status: 'approved', message: '3434342'}
{status: 'approved, message: 'asdsdfdad'}
{status: 'approved', message: '123\n 3434342\n asdsdfdad'}
const objects = [
{status: 'approved', message: '123'},
{status: 'declined', message: '3123123'},
{status: 'approved', message: '3434342'},
{status: 'approved', message: 'asdsdfdad'},
];
const result = objects.reduce(function(acc, el, i) {
if (el.status === 'approved') {
acc.message += (!i ? '': '\n') + el.message;
}
return acc;
}, {
status: 'approved',
message: '',
});
const arr = [
{status: 'approved', message: '123'},
{status: 'declined', message: '3123123'},
{status: 'approved', message: '3434342'},
{status: 'approved', message: 'asdsdfdad'}
]
const approvedMessages = arr
.filter(n => n.status === 'approved')
.map(n => n.message);
const obj = {
status: 'approved',
message: approvedMessages.join('\n'),
}
const obj = arr
.filter(n => n.status === 'approved')
.reduce((p, c) => ({ ...p, message: `${p.message}\n${c.message}` }));
const grouped = arr.reduce((acc, { status, message }) => {
acc[status] = acc[status] || { status, messages: [] };
acc[status].messages.push(message);
return acc;
}, {});
Object.keys(grouped).forEach(n => grouped[n].messages = grouped[n].messages.join('\n'));
const obj = grouped.approved;
let your_array = [ // Положим объекты в массив
{status: 'approved', message: '123'},
{status: 'declined', message: '3123123'},
{status: 'approved', message: '3434342'},
{status: 'approved', message: "asdsdfdad"},
]
// Результат будет объектом со свойствами === статусами
// result.approved = [ 123, 3434342, asdsdfdad , ...]
let result = concatAtStatus(your_array);
function concatAtStatus ( arrayOfObjects = [] ) {
let result = {};
arrayOfObjects.forEach( elem => {
let { status, message } = elem;
result[status] ? result[status].push(message) : result.status = [ message ];
});
return result;
}