Я работаю под углом 8 и хочу преобразовать мой массив данных в ключи объекта, как предусмотрено. Это моя логика и она отлично работает .. Есть ли короткий способ сделать это?
// --- some.service.ts ---
// Map object (or array) having object with so many keys and reduce it to provided format i.e. newDefinition
reduceObjectOrArray(data: any, newDefinition: any): any {
const isDataArray = Array.isArray(data);
data = isDataArray ? data : [data];
const resData: any[] = [];
data.forEach(item => {
const obj: any = {};
newDefinition.forEach(dataKey => {
if (newDefinition.indexOf(dataKey) !== -1) {
obj[dataKey] = item[dataKey];
}
});
resData.push(obj);
});
return isDataArray ? resData : resData[0];
}
// Convert Data
reduceObjectOrArray() {
console.log(this.some.reduceObjectOrArray([
{ Key1: '1', Key2: '2', Key3: '5' },
{ Key1: '2', Key2: '3', Key3: '6' },
{ Key1: '3', Key2: '4', Key3: '7' }
], ['Key1', 'Key3']));
}