Is there singleline, type mapfilter ... es5+ higherfunction or ... ? Hopefully solution with decription how and why and ref. If some one knows why the once didn't work.
timeTableKeys: is the output Template array, order with keys. runers: array of object literals. parseStringify: JSON.parse - JSON.stringify with easy 2nd argument (timeTableKeys) to filter. manualNewObject: Creating Manual an object literal. multilineMapNewObj: Using timeTableKeys to create an object literal.
const runers = [{'Name':'Joe Bennet', 'Seasons':3, 'Time':'46m:12s:431ms', 'Place':12, 'Age':46, 'Gender':'M', 'Qual':'Pre', 'State':'TX', 'Adress':'Little Cow Middle Town, 2nd Main 5th Street 14C App 38','Mail':'joeB46TX@littelecowMT.org','Phone':'555-1845-LCMT','Group':'Semi-Senior', 'Doping-Test':'Passed', 'NR':7289},{'Name':'Allan Jenkin', 'Seasons':5, 'Time':'52m:29s:894ms', 'Place':153, 'Age':52, 'Gender':'M', 'Qual':'2nd Heat', 'State':'CA', 'Adress':'Redmond River, Downunder H12C ','Mail':'senior.jenkinA@qwredmond.net','Phone':'555-327-REDMOND','Group':'Semi-Senior', 'Doping-Test':'Passed', 'NR':925}],
timeTableKeys = ['State','Name','NR','Place','Time','Mail'],
parseStringify = () => JSON.parse( JSON.stringify( runers, timeTableKeys ) ),
manualNewObject = () => runers.map(o => ({ 'State':o.State,'Name':o.Name,'NR':o.NR,'Place':o.Place,'Time':o.Time })),
multilineMapNewObj = () => runers.map(o => { let newO = {}; timeTableKeys.forEach( key => newO[key] = o[key]); return newO; });
console.log('--= Start =--' );
console.log('1: parseStringify', parseStringify() );
console.log('2: manualNewObject', manualNewObject() );
console.log('3: multilineMapNewObj', multilineMapNewObj() );
Pls: Remember I'm new to these way to think. Here is some of these I've tried, that didn't work, but could maybe be converted or flatten:
runers.map(o => $.extend(true,{}, timeTableKeys.map( key => (o[key] = o[key]) )));
runers.map(o => $.extend(true,{}, Object.assign({}, timeTableKeys.map( key => (o[key] = o[key])) )));
runers.map(o => Object.assign({}, timeTableKeys.map( key => ({[key]:o[key]}) )));
runers.map(o => timeTableKeys.map( key => (o[key] = o[key])));
runers.map(o => timeTableKeys.flatMap( key => ({[key]:o[key]}) ) );
You where so close to third of those that didn't work.
runers.map(o => Object.assign({}, timeTableKeys.map( key => ({[key]:o[key]}) )));
Just add the ...spread operator on to template filter array "timeTableKeys".
runers.map(o => Object.assign({}, ...timeTableKeys.map( key => ({[key]:o[key]})) ));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.