簡體   English   中英

如何重命名對象數組中對象的所有鍵?

[英]How does one rename all of an object's keys within an array of objects?

有沒有辦法更改 object 數組中的鍵名?

代碼:更改鍵名的值列表。

var keys = {
    id:'identifier',
    facility:'site',
    status:'info',
    date:'days'
};

更改鍵名的 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'
}]

預期 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'
    }]

您可以使用reduceObject.entries進行操作。

  1. reduce是對 object 中的屬性進行重新分組
  2. 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)

這是一個map ping 任務。 與幾乎所有數組方法一樣, map允許第二個參數thisArg綁定到回調方法的上下文,因此在此類方法的應用/調用時可訪問。

提供的方法實現了一個回調方法remapObjectWithBoundKeys ,它利用其 this 上下文作為如何重新映射每個對象的鍵的配置。 它通過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; }

實施方法的優點是它獨立於map任務運行。 一個人可以很容易地利用或重復使用它來進行任何類型的 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM