2017-05-14 98 views
2

對象的數組我有對象的這樣一個數組:扁平化地圖(或環)

[ 
    { 
     "dow": 0, 
     "min_price": 2000, 
     "max_price": 4000, 
     "is_enable": true 
    }, 
    { 
     "dow": 1, 
     "min_price": 500, 
     "max_price": 3000, 
     "is_enable": true 
    }, 
    { 
     "dow": 2, 
     "min_price": 500, 
     "max_price": 3000, 
     "is_enable": true 
    }, 
    { 
     "dow": 3, 
     "min_price": 1000, 
     "max_price": 3000, 
     "is_enable": true 
    } 
] 

我怎樣才能把它弄成這個樣子嗎?訂單無關緊要。

{ 
    advancePrice_0_max: 4000, 
    advancePrice_0_min: 2000, 
    advancePrice_1_max: 3000, 
    advancePrice_1_min: 500, 
    advancePrice_2_max: 3000, 
    advancePrice_2_min: 500, 
    advancePrice_3_max: 3000, 
    advancePrice_3_min: 500 
    .. 
    .. 
} 

我試着從這開始,但是即使下面的代碼也有snytax錯誤。

let temp = {}; 
if (!isEmpty.custom_pricing) { 
    temp = custom_pricing.map(obj => { 
    `advancePrice_${obj.dow}_min`: obj.min_price, 
    `advancePrice_${obj.dow}_max`: obj.max_price 
    }) 
    console.log(temp); 
} 

但我堅持,不知道如何繼續..

回答

0

使用forEach因爲你想結果作爲一個對象不是數組格式,如果你使用map,那麼temp將是一個返回數據的數組。

使用本:

let temp = {}; 
if (!isEmpty.custom_pricing) { 
    custom_pricing.forEach(obj => { 
     temp[`advancePrice_${obj.dow}_min`] = obj.min_price; 
     temp[`advancePrice_${obj.dow}_max`] = obj.max_price; 
    }) 
    console.log(temp); 
} 

檢查工作片段:

let custom_pricing = [{ 
 
     "dow": 0, 
 
     "min_price": 2000, 
 
     "max_price": 4000, 
 
     "is_enable": true 
 
    }, 
 
    { 
 
     "dow": 1, 
 
     "min_price": 500, 
 
     "max_price": 3000, 
 
     "is_enable": true 
 
    }, 
 
    { 
 
     "dow": 2, 
 
     "min_price": 500, 
 
     "max_price": 3000, 
 
     "is_enable": true 
 
    }, 
 
    { 
 
     "dow": 3, 
 
     "min_price": 1000, 
 
     "max_price": 3000, 
 
     "is_enable": true 
 
    } 
 
] 
 

 
let temp = {}; 
 
custom_pricing.forEach(obj => { 
 
    temp[`advancePrice_${obj.dow}_min`] = obj.min_price; 
 
    temp[`advancePrice_${obj.dow}_max`] = obj.max_price; 
 
}) 
 
console.log(temp);

+1

爲什麼downvote,什麼是錯的這個解決方案,請提供同樣的原因:( –

+0

我同意。有一個人給每個答案(包括我的答案)加上'-1'。這是不公平的,所以我給你一個補償。 – Badacadabra

1

你想要的陣列減少到一個單一的對象,所以:

var result = data.reduce(function (acc,el,i) { 
    acc['advancedprice_' + i + '_max'] = el.max_price; 
    acc['advancedprice_' + i + '_min'] = el.min_price; 
    return acc; 
}, {}); 
0

您可以使用好老for環......

let arr = [ 
 
    { 
 
    "dow": 0, 
 
    "min_price": 2000, 
 
    "max_price": 4000, 
 
    "is_enable": true 
 
    }, 
 
    { 
 
    "dow": 1, 
 
    "min_price": 500, 
 
    "max_price": 3000, 
 
    "is_enable": true 
 
    }, 
 
    { 
 
    "dow": 2, 
 
    "min_price": 500, 
 
    "max_price": 3000, 
 
    "is_enable": true 
 
    }, 
 
    { 
 
    "dow": 3, 
 
    "min_price": 1000, 
 
    "max_price": 3000, 
 
    "is_enable": true 
 
    } 
 
]; 
 

 
let res = {}; 
 

 
for (let i = 0; i < arr.length; i++) { 
 
    res[`advancePrice_${i}_max`] = arr[i].max_price; 
 
    res[`advancePrice_${i}_min`] = arr[i].min_price; 
 
} 
 

 
console.log(res);