简体   繁体   English

如何重命名对象数组中对象的所有键?

[英]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.您可以使用reduceObject.entries进行操作。

  1. reduce is to re-group the property in the object reduce是对 object 中的属性进行重新分组
  2. 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.

相关问题 如何在对象数组中找到一个对象的所有匹配键和值? - How to find all matching keys and values of one object in array of objects? 有效地重命名/重新映射对象数组中的 javascript/json 对象键 - Efficiently rename/re-map javascript/json object keys within array of objects Javascript - 如何创建对象数组,其中对象的键都具有相同的值 - Javascript - how to create an array of objects where the object's keys all have the same value 如何重命名数组内的 object 键 - How to rename object keys inside array 如何将具有相同键的对象数组合到一个对象中? - How can I combine an array of objects with the same keys into one object? 如何获取具有多个键的对象数组中所有键的值,然后将它们全部相加[暂停] - How to get values of all keys in array of objects with more than one keys and then sum of them all [on hold] 通过键之一将对象数组转换为单个对象 - Converting an array of objects into a single object by one of the keys 如何使用这些键及其值将具有一个 object 和多个键的数组转换为多个对象的数组? - How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values? 如何通过每个 object 项目的属性名称过滤一组对象? - How does one filter an array of objects by each object item's property name? 如何强制一个联合的所有成员都将出现在一个对象数组中? 如何将数组简化为 object 并以所有联合成员作为键? - How to enforce all members of a union will be present in an array of objects? How to reduce the array to an object with all union members as keys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM