天天看點

前端項目實戰68-資料處理之一個數組和一個對象

export function myContact(target: any, source: any) {
    for (const [key, value] of Object.entries(source)) {
        const [name, index] = key.split('-') // 要求key隻能有一個'-'字元,前面是price之類的name,後面是index數字
        if (name === 'price') { // 可以自定義判斷,字首
            target[Number(index)].price = value
        }
    }
    return target
}
//結果值
[
     {
         "styleName": "我們",
         "styleId": 2,
         "styleValue": "",
         "price": "12312"
     },
     {
         "styleName": "我們",
         "styleId": 7,
         "styleValue": "",
         "price": "12312"
     }
 ] 初始資料值
[
     {
         "styleName": "我們",
         "styleId": 2,
         "styleValue": "",
     },
     {
         "styleName": "我們",
         "styleId": 7,
         "styleValue": "",
     }
 ] 初始資料值
{
     "price-0": "12312",
     "price-1": "12312",
     "price-2": "12312"
 }