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'),
}
function apiRequest(success) {
return new Promise(function (resolve, reject) {
if (success) {
resolve(success);
} else {
reject('err');
}
})
.catch(function(err){
return err;
});
}
var p1 = apiRequest('1');
var p2 = apiRequest();
var p3 = apiRequest('2');
var p4 = apiRequest();
Promise.all([p1, p2, p3, p4])
.then(function(res){
console.log('Promise.all', res);
})
.catch(function(err){
console.error('err', err);
});
Promise.all (4) ["1", "err", "2", "err"]
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;
}
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;
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: '',
});
for (var key in YourObject) {
var obj = YourObject[key];
if (obj.status === "error") {
console.log(obj.text);
}
}
const obj = {
operation: {
status: "success",
text: "Approved"
} ,
anotherOperation: {
status: "success",
text: "Approved"
},
op: {
status: "error",
text: "Declined, because..."
}
};
const textsArray = Object.values(obj).filter(el => el.status === 'error').map(el => el.text);
// результат: ["Declined, because..."]
class ScrollToTop extends Component {
onClick = () => {
if (this.element && this.element.parentNode) {
scrollIntoView(this.element.parentNode.parentNode, {
time: 500,
align: {
top: 0.1,
},
})
}
}
onSetRef = (element) => {
this.element = element
}
render() {
const { intl } = this.props
return (
<div
ref={this.onSetRef}
style={{ width: '100%' }}
>
<Layer
align='mc-mc'
constraints={[{
to: 'window',
attachment: 'none',
pin: ['top'],
}]}
classes={{
pinned: styles(),
}}
>
<div style={{ display: 'none', marginTop: 60, paddingRight: 40, paddingBottom: 20 }}>
<Text color='blue400'>
↑
</Text>
<Space />
<Link onClick={this.onClick}>
<Text color='blue400'>
{intl.formatMessage(messages.up)}
</Text>
</Link>
</div>
</Layer>
</div>
)
}
}
var a = [{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}]
var b = a.map(obj => {
let newObj = {}
newObj[obj.id] = obj.name
return newObj
})
const oldData = [{id: 1, name: 'Vasya'}, {id: 'anyString', name: {}}];
const newData = oldData.map(function(item) {
const newItem = {};
newItem[item.id] = item.name;
return newItem;
});
var n = 300.239235
alert((parseInt(n * 100)) / 100); // Вернет 300.23