2017-12-27 143 views
-1

我有具有以下結構的Filter對象道具

{ cardControlItem: 'Tidak', 
    cardDirectUseItem: 'Tidak', 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee', 
    cardDetailMin: 654, 
    cardDetailMax: 65, 
    cardDetailIncrement: 754, 
    cardDetailPriceOverall: 4534, 
    cardDetailPriceUnit: 0, 
    ltMeasurementMinUuid: 'fddca37a-d0a3-40a4-8537-e84375b01601', 
    ltMeasurementMaxUuid: '2bc6d7d2-5167-4459-9910-a65839008afd' } 

一個目的,我有以陣列鍵的列表

['cardControlItem', 'cardDirectUseItem', 'kbUnspscUuid', 'substoreUuid', 'stockCardGroupUuid', 'stockCardBatchUuid'] 

如何根據減少它陣列指定

預期輸出:

{ cardControlItem: 'Tidak', 
    cardDirectUseItem: 'Tidak', 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee' } 

P.S:我花了3個小時在StackOverflow上找到類似的問題。什麼是這種問題的正確標題:D

回答

1

試試這個解決方案。我篩選objfilter部分)中的屬性,然後遍歷存在的屬性(reduce部分)並將屬性值分配到新對象(acc)中。

const obj = { 
 
    cardControlItem: 'Tidak', 
 
    cardDirectUseItem: 'Tidak', 
 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee', 
 
    cardDetailMin: 654, 
 
    cardDetailMax: 65, 
 
    cardDetailIncrement: 754, 
 
    cardDetailPriceOverall: 4534, 
 
    cardDetailPriceUnit: 0, 
 
    ltMeasurementMinUuid: 'fddca37a-d0a3-40a4-8537-e84375b01601', 
 
    ltMeasurementMaxUuid: '2bc6d7d2-5167-4459-9910-a65839008afd' 
 
}; 
 

 
const keys = ['cardControlItem', 'cardDirectUseItem', 'kbUnspscUuid', 'substoreUuid', 'stockCardGroupUuid', 'stockCardBatchUuid']; 
 

 
const mapped = keys.filter(key => obj.hasOwnProperty(key)) 
 
        .reduce((acc, key) => { acc[key] = obj[key]; return acc }, {}); 
 

 
console.log(mapped);

+2

解釋您的解決方案可能有助於未來的遊客。 –

0

.Loop throught陣列,並檢查是否該對象具有此鍵,如果是這樣的話創建與該密鑰的新的對象,而不是突變原來

var orgObject = { 
 
    cardControlItem: 'Tidak', 
 
    cardDirectUseItem: 'Tidak', 
 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee', 
 
    cardDetailMin: 654, 
 
    cardDetailMax: 65, 
 
    cardDetailIncrement: 754, 
 
    cardDetailPriceOverall: 4534, 
 
    cardDetailPriceUnit: 0, 
 
    ltMeasurementMinUuid: 'fddca37a-d0a3-40a4-8537-e84375b01601', 
 
    ltMeasurementMaxUuid: '2bc6d7d2-5167-4459-9910-a65839008afd' 
 
} 
 

 

 
var keys = ['cardControlItem', 'cardDirectUseItem', 'kbUnspscUuid', 'substoreUuid', 'stockCardGroupUuid', 'stockCardBatchUuid'] 
 

 
var newObj = {}; 
 
keys.forEach(function(item) { 
 
    if (orgObject.hasOwnProperty(item)) { 
 
    newObj[item] = orgObject[item] 
 

 
    } 
 

 
}) 
 

 
console.log(newObj)

2
const filter = (obj, arr) => Object.assign(...arr.map(el => ({[el]: obj[el]}))); 

這樣一個可以做

const result = filter({a:1, b:2, c:3}, ["a", "b"]); 
+0

[試試](http://jsbin.com/suwutuwake/edit?console) –

0

var data = { cardControlItem: 'Tidak', 
 
    cardDirectUseItem: 'Tidak', 
 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee', 
 
    cardDetailMin: 654, 
 
    cardDetailMax: 65, 
 
    cardDetailIncrement: 754, 
 
    cardDetailPriceOverall: 4534, 
 
    cardDetailPriceUnit: 0, 
 
    ltMeasurementMinUuid: 'fddca37a-d0a3-40a4-8537-e84375b01601', 
 
    ltMeasurementMaxUuid: '2bc6d7d2-5167-4459-9910-a65839008afd' }; 
 
    
 
var keys = ['cardControlItem', 'cardDirectUseItem', 'kbUnspscUuid', 'substoreUuid', 'stockCardGroupUuid', 'stockCardBatchUuid']; 
 
    
 
var new_data = {}; 
 
for(var i = 0; i < keys.length; i++){ 
 
    if(data.hasOwnProperty(keys[i])){ 
 
    new_data[keys[i]] = data[keys[i]]; 
 
    } 
 
} 
 
    
 
console.log(new_data);

0

試試這個片斷

let obj = { cardControlItem: 'Tidak', 
 
    cardDirectUseItem: 'Tidak', 
 
    kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', 
 
    substoreUuid: '2f2b04bb-8b80-4b1f-b827-bf20311e31ee', 
 
    cardDetailMin: 654, 
 
    cardDetailMax: 65, 
 
    cardDetailIncrement: 754, 
 
    cardDetailPriceOverall: 4534, 
 
    cardDetailPriceUnit: 0, 
 
    ltMeasurementMinUuid: 'fddca37a-d0a3-40a4-8537-e84375b01601', 
 
    ltMeasurementMaxUuid: '2bc6d7d2-5167-4459-9910-a65839008afd' } 
 
    
 
let comparator = ['cardControlItem', 'cardDirectUseItem', 'kbUnspscUuid', 'substoreUuid', 'stockCardGroupUuid', 'stockCardBatchUuid']; 
 
    
 
    
 
Object.keys(obj).map((key)=>{ 
 
    if(!comparator.includes(key)){ 
 
    delete obj[key] 
 
    } 
 
}) 
 
    
 
console.log(obj)

輸出 {cardControlItem: 'Tidak', cardDirectUseItem: 'Tidak', kbUnspscUuid: '6a564b8e-2976-4fde-8759-7951970d7500', substoreUuid:「2f2b04bb-8b80-4b1f-b827-bf20311e31ee }