Filter对象道具
我有具有以下结构的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
回答:
试试这个解决方案。我筛选obj
(filter
部分)中的属性,然后遍历存在的属性(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);
回答:
.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)
回答:
const filter = (obj, arr) => Object.assign(...arr.map(el => ({[el]: obj[el]})));
这样一个可以做
const result = filter({a:1, b:2, c:3}, ["a", "b"]);
回答:
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);
回答:
试试这个片断
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 }
以上是 Filter对象道具 的全部内容, 来源链接: utcz.com/qa/265237.html