我有一个具有以下结构的对象
{ 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
答案 0 :(得分:2)
const filter = (obj, arr) => Object.assign(...arr.map(el => ({[el]: obj[el]})));
所以可以做到
const result = filter({a:1, b:2, c:3}, ["a", "b"]);
答案 1 :(得分:1)
试试这个解决方案。我过滤了obj
(filter
部分)中的属性,然后遍历存在的属性(reduce
部分)并将属性值分配给新对象({{ 1}})。
acc

答案 2 :(得分:0)
。遍历数组并检查对象是否有此键,如果是,则使用此键创建一个新对象,而不是改变原始对象
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)

答案 3 :(得分: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);
&#13;
答案 4 :(得分: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)
&#13;
输出 {cardControlItem:&#39; Tidak&#39;, cardDirectUseItem:&#39; Tidak&#39;, kbUnspscUuid:&#39; 6a564b8e-2976-4fde-8759-7951970d7500&#39;, substoreUuid:&#39; 2f2b04bb-8b80-4b1f-b827-bf20311e31ee }