繁体   English   中英

修改对象数组中的值

[英]Modify values in an array of objects

我有一个对象数组,我想使用另一个对象数组根据“类型”值修改它们。 我得到一个很大的对象列表,但我应该只得到 2 个。

这就是我所拥有的:

 const convertData = (type, value) => { switch (type) { case "date": { return new Date(Number(value)).toDateString(); } case "currency": { return value? `$${value}`: null; } default: return value; } }; const headers = [{ field: 'amount', type: 'currency' }, { field: 'deliveredDate', type: 'date' }] const body = [{ id: 1, amount: 4000, deliveredDate: "1610427600000" }, { id: 2, amount: 6000, deliveredDate: "1611118800000" }] const result = body.map(i => { const keys = Object.keys(i); return keys.map(k => { return {...i, [k]: headers.find(i => i.field === k)? convertData(headers.find(i => i.field === k).type, i[k]): null } }) }) console.log(result.flat()) /** Expected Result [{ id: 1, amount: $4000, deliveredDate: "Tue Jan 12 2021" }, { id: 2, amount: $6000, deliveredDate: "Wed Jan 20 2021" }] */

headers 中的字段值表示 body 数组中的键。 并且需要根据 header object 中的类型更改对应正文 object 中的值。 请指教。

一种可能的方法,它也允许灵活的适应和代码重用,利用经常被忽视的可选第二个参数,一个可以提供给大多数数组方法...... thisArg例如map

因此,这种方法确实从 OP 的headers列表中创建了一个查找表并将其提供给映射过程,因为人们不想总是一次又一次地搜索(在map的每个迭代步骤中)相同的列表以找到正确/匹配相关的转换type

实际的映射将很容易。 Implement a function which assumes its this context to be a lookup table and which does (re)create a type-converted version of the provided (here currently processed) object by looking up the correct conversion type before providing it to the OP's convertData function.

 const convertData = (type, value) => { switch (type) { case "date": { return new Date(Number(value)).toDateString(); } case "currency": { return value? `$${value}`: null; } default: return value; } }; const headers = [{ field: 'amount', type: 'currency' }, { field: 'deliveredDate', type: 'date' }]; const body = [{ id: 1, amount: 4000, deliveredDate: "1610427600000" }, { id: 2, amount: 6000, deliveredDate: "1611118800000" }]; function createConversionTypeLookupTable(conversionTypeList) { return conversionTypeList.reduce((table, conversionTypeItem) => { table[conversionTypeItem.field] = conversionTypeItem.type; return table; }, {}); } function createConvertedItemViaBoundLookupTable(item) { const lookupTable = (this || {}); // more fail safe. return Object.entries(item).reduce((obj, [key, value]) => { obj[key] = convertData((lookupTable[key] || key), value); return obj; }, {}); } console.log( 'Converted Item List...', body.map( createConvertedItemViaBoundLookupTable, createConversionTypeLookupTable(headers) ) ); console.log( 'Conversion Type Lookup Table...', createConversionTypeLookupTable(headers) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

您正在调用map两次,一次是在数组上,一次是在键上,因此您获得的不仅仅是数组中的项目,而且对象不正确,因为您每次都在不同的对象上调用convertData

考虑如下进行 -

 const convertData = (type, value) => { switch (type) { case "date": { return new Date(Number(value)).toDateString(); } case "currency": { return value? `$${value}`: null; } default: return value; } }; const headers = [{ field: 'amount', type: 'currency' }, { field: 'deliveredDate', type: 'date' }] const body = [{ id: 1, amount: 4000, deliveredDate: "1610427600000" }, { id: 2, amount: 6000, deliveredDate: "1611118800000" }] const result = body.map(i => { const newItem = {}; for (const prop in i) { const header = headers.find(f => f.field === prop); const value = i[prop]; newItem[prop] = header? convertData(header.type, value): value } return newItem; }); console.log(result)


编辑1:

 const convertData = (type, value) => { switch (type) { case "date": { return new Date(Number(value)).toDateString(); } case "currency": { return value? `$${value}`: null; } default: return value; } }; const headers = [{ field: 'amount', type: 'currency' }, { field: 'deliveredDate', type: 'date' }] const body = [{ id: 1, amount: 4000, deliveredDate: "1610427600000" }, { id: 2, amount: 6000, deliveredDate: "1611118800000" }] const result = body.map(i => { const newItem = {}; var keys = Object.keys(i); keys.forEach((prop) => { const header = headers.find(f => f.field === prop); const value = i[prop]; newItem[prop] = header? convertData(header.type, value): value }); return newItem; }); console.log(result);


JSON.stringify()与替换 function 做 OP 正在寻找的东西。

 // OP code and data // const convertData = (type, value) => { switch (type) { case "date": { return new Date(Number(value)).toDateString(); } case "currency": { return value? `$${value}`: null; } default: return value; } }; const headers = [{ field: 'amount', type: 'currency' }, { field: 'deliveredDate', type: 'date' }] const body = [{ id: 1, amount: 4000, deliveredDate: "1610427600000" }, { id: 2, amount: 6000, deliveredDate: "1611118800000" }] // use the OP code to convert a value to a string formatted by its key // const convertField = (key, value) => { let header = headers.find(e => e.field === key) return header? convertData(header.type, value): value } const convertedStr = JSON.stringify(body, convertField); const convertedObj = JSON.parse(convertedStr); console.log(convertedObj);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM