UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

44 lines 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Gets a list of values indexed by field based on a list of records * * @example * const records = [ * { * id: 1, * title: "Lorem Ipsum", * views: 254, * user_id: 123, * }, * { * id: 2, * title: "Sic Dolor amet", * views: 65, * user_id: 456, * }, * ]; * getValuesFromRecords(records); * // { * // id: [1, 2], * // title: ['Lorem Ipsum', 'Sic Dolor amet'], * // views: [254, 65], * // user_id: [123, 456], * // } */ exports.default = (records) => records.reduce((values, record) => { Object.keys(record).forEach(fieldName => { if (!values[fieldName]) { values[fieldName] = []; } if (record[fieldName] != null) { const value = Array.isArray(record[fieldName]) ? [record[fieldName]] : record[fieldName]; values[fieldName] = values[fieldName].concat(value); } }); return values; }, {}); module.exports = exports.default; //# sourceMappingURL=getValuesFromRecords.js.map