我有2个数据来源。其中一个来源是"模板"到数据可接受的范围。但是,第二个来源可能包含大量我不关心的数据(JSON中有100多个属性)。这是模式:
>unique(unlist(projectdf$ProjectSubject))
[1] "Applied Learning" "Applied Learning, Literacy
& Language"
[3] "Literacy & Language" "Special Needs"
[5] "Literacy & Language, History & Civics" "Math & Science"
[7] "History & Civics, Math & Science" "Literacy & Language,
Special Needs"
[9] "Applied Learning, Special Needs" "Health & Sports, Special
Needs"
[11] "Math & Science, Literacy & Language" "Literacy & Language, Math
& Science"
[13] "Literacy & Language, Music & The Arts" "Math & Science, Special
Needs"
[15] "Health & Sports" "Music & The Arts"
[17] "Math & Science, Applied Learning" "Literacy & Language,
Applied Learning"
[19] "Applied Learning, Music & The Arts" "History & Civics,
Literacy & Language"
[21] "Applied Learning, Math & Science" "Health & Sports, Math &
Science"
[23] "Applied Learning, Health & Sports" "History & Civics"
[25] "History & Civics, Music & The Arts" "Math & Science, History &
Civics"
[27] "Math & Science, Music & The Arts" "Special Needs, Music &
The Arts"
[29] "History & Civics, Applied Learning" "History & Civics, Special
Needs"
第二个来源将在上面的数据模式中具有4个属性(加上我不在乎的许多属性)。目前,我正在这样分配它们:
// Only store the data we care about. Only a small subset of
// data that I need for this particular dataset.
state = {
isDirty: false,
data: {
name: '',
address: '',
city: '',
state: ''
}
}
使用ES6,也许是破坏,是否有更好的方法来批量分配这样的变量?
再次感谢!
修改
添加以便澄清循环后的分配。
答案 0 :(得分:1)
Lodash pick
可用于挑选特定键,或者辅助函数可用于相同目的:
const pick = (obj, keys) => Object.keys(obj)
.filter((key) => keys.indexOf(key) >= 0)
.reduce(
(newObj, key) => Object.assign(newObj, { [key]: obj[key] }),
{}
);
许多相关问题已经提出了这一点。这个问题特有的是:
this.state.data = pick(someDataSource, Object.keys(this.state.data));
答案 1 :(得分:0)
你可以做的一个技巧(技巧,因为它需要吞下错误)是使用不可扩展的对象,使用Object.preventExtensions
然后使用Object.assign
来填充它包含数据(try
/ catch
块中的)。
// Only store the data we care about. Only a small subset of
// data that I need for this particular dataset.
state = {
isDirty: false,
data: {
name: '',
address: '',
city: '',
state: ''
}
}
const newData = {
name:'name',
address:'address',
city:'city',
state:'state',
phone:'phone',
zip:'zip'
}
const updatedData = Object.preventExtensions({...state.data});
try{
Object.assign(updatedData, newData);
} catch(throwaway){};
console.log(updatedData);

作为重用的功能
function schemaMerge(schema, data) {
const mergedData = Object.preventExtensions({...schema});
try {
Object.assign(mergedData, data);
} catch (throwaway) {};
return ({...mergedData}); // create a new object from the merged one so that it no longer is extensionless
}
// Only store the data we care about. Only a small subset of
// data that I need for this particular dataset.
state = {
isDirty: false,
data: {
name: '',
address: '',
city: '',
state: ''
}
}
const newData = {
name: 'name',
address: 'address',
city: 'city',
state: 'state',
phone: 'phone',
zip: 'zip'
}
const updatedData = schemaMerge(state.data, newData);
state.data = updatedData;
console.log(state.data);

答案 2 :(得分:0)
var o = JSON.parse('{"a":1, "b":2}', (k, v) => k === 'a' ? void 0 : k === 'b' ? 3 : v)
console.log( o )