gpt4 book ai didi

javascript - 如何获得: Array of object literals with help of an template from a bigger data set of object literals?

转载 作者:行者123 更新时间:2023-11-30 14:04:20 28 4
gpt4 key购买 nike

是否有单行,类型 mapfilter ... es5+ higherfunction 或 ...?希望解决方案描述如何以及为什么和引用。如果有人知道为什么曾经不起作用。

timeTableKeys:是输出模板数组,按键排序。runers:对象文字数组。parseStringify: JSON.parse - JSON.stringify 带有简单的第二个参数 (timeTableKeys) 来过滤。manualNewObject:创建 Manual 对象文字。multilineMapNewObj:使用 timeTableKeys 创建对象文字。

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() );

请记住,我对这些思考方式还很陌生。这是我尝试过的其中一些,但没有用,但可以转换或展平:

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]}) ) );

最佳答案

你有近三分之一的人没有工作。

runers.map(o => Object.assign({}, timeTableKeys.map( key => ({[key]:o[key]})  )));

只需将 ...spread 运算符添加到模板过滤器数组“timeTableKeys”即可。

runers.map(o => Object.assign({}, ...timeTableKeys.map( key => ({[key]:o[key]})) ));

关于javascript - 如何获得: Array of object literals with help of an template from a bigger data set of object literals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55726578/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com