[英]How does one rename all of an object's keys within an array of objects?
Is there a way to change the key names in an array of object?有没有办法更改 object 数组中的键名?
Code: List of value to change the key name.代码:更改键名的值列表。
var keys = {
id:'identifier',
facility:'site',
status:'info',
date:'days'
};
Array of object to change the key names更改键名的 object 数组
var arrayObj = [{
id:'jr210',
facility:'earth',
status:'sample info',
date:'Feb 29, 2020'
},
{
id:'tl980',
facility:'mars',
status:'sample info',
date:'Jan 15, 2020'
}]
Expected output:预期 output:
var newArrayObj = [{
identifier:'jr210',
site:'earth',
info:'sample info',
days:'Feb 29, 2020'
},
{
identifier:'tl980',
site:'mars',
info:'sample info',
days:'Jan 15, 2020'
}]
You can use reduce
and Object.entries
for the operation.您可以使用
reduce
和Object.entries
进行操作。
reduce
is to re-group the property in the object reduce
是对 object 中的属性进行重新分组Object.entries
is to retrieve the key and value pairs in the object. Object.entries
是检索 object 中的键值对。type KeyForFinalResult = 'identifier'|'site'|'info'| 'days'
type KeyForReferenceKeys = 'id' | 'facility' | 'status' | 'date'
type TempObjectType = {
[T in KeyForReferenceKeys | string ]: string
}
type FinalObjectType = {
[T in KeyForFinalResult | string]: string
}
var keys: TempObjectType = {
id:'identifier',
facility:'site',
status:'info',
date:'days'
};
var arrayObj:TempObjectType[] = [{
id:'jr210',
facility:'earth',
status:'sample info',
date:'Feb 29, 2020'
},
{
id:'tl980',
facility:'mars',
status:'sample info',
date:'Jan 15, 2020'
}]
const result = arrayObj.reduce((finalResult: FinalObjectType[], elem)=>{
let temp: FinalObjectType = {} as FinalObjectType
Object.entries(keys).forEach((entry: string[] )=> {
//each entry: [[key, value],[key1, value1]...]
temp[entry[1]] = elem[entry[0]]
})
finalResult.push(temp)
return finalResult
},[])
console.log(result)
This is a map
ping task.这是一个
map
ping 任务。 Like almost every array method map
allows a 2nd argument, the thisArg
, a context which gets bound to the callback method and thus is accessibly at such a method's apply/call time.与几乎所有数组方法一样,
map
允许第二个参数thisArg
绑定到回调方法的上下文,因此在此类方法的应用/调用时可访问。
The provided approach implements a callback method remapObjectWithBoundKeys
which utilizes its this context as configuration of how to remap each of an object's key.提供的方法实现了一个回调方法
remapObjectWithBoundKeys
,它利用其 this 上下文作为如何重新映射每个对象的键的配置。 It does so by reduce
ing the entries
of its key-config and creating a new object by iteratively assign
ing new key-value pairs to it.它通过
reduce
其键配置的entries
并通过迭代地assign
新的键值对来创建新的 object 来实现这一点。
const sampleList = [{ id:'jr210', facility:'earth', status:'sample info', date:'Feb 29, 2020', }, { id:'tl980', facility:'mars', status:'sample info', date:'Jan 15, 2020', }]; function remapObjectWithBoundKeys(item) { const keyConfig = this; return Object.entries(keyConfig).reduce((obj, [recentKey, currentKey]) => Object.assign(obj, { [currentKey]: item[recentKey] }), {}); } console.log( sampleList.map(remapObjectWithBoundKeys, { id: 'identifier', facility: 'site', status: 'info', date: 'days', }) );
.as-console-wrapper { min-height: 100%;important: top; 0; }
The advantage of the implemented method is that it operates independently from a map
task.实施方法的优点是它独立于
map
任务运行。 One easily can utilize or re-use it for any kind of object (re)creation...一个人可以很容易地利用或重复使用它来进行任何类型的 object(重新)创建......
function remapObjectWithBoundKeys(item) { const keyConfig = this; return Object.entries(keyConfig).reduce((obj, [recentKey, currentKey]) => Object.assign(obj, { [currentKey]: item[recentKey] }), {}); } const planetType = { id:'tl980', facility:'mars', status:'sample info', date:'Jan 15, 2020', }; const recreatePlanetItem = remapObjectWithBoundKeys.bind({ id: 'identifier', facility: 'site', status: 'info', date: 'days', }); console.log( planetType, recreatePlanetItem(planetType) );
.as-console-wrapper { min-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.