我正在以下列方式使用 _.pick 方法
假设我有一个字符串数组,它们只是我想从对象数组中的每个对象获取的属性名称
var wantedPropArray=["prop1","prop2","prop3.name"];
下面是我的对象数组
var objectArray = [
{"prop1":"prop1Data1","prop2":"prop2Data1","prop3":{"name":"Tom","age":"24","class":"graduate"},"prop4":"prop4Data1","prop5":"prop5Data1"},
{"prop1":"prop1Data2","prop2":"prop2Data2","prop3":{"name":"Cat","age":"24","class":"graduate"},"prop4":"prop4Data2","prop5":"prop5Data2"}
{"prop1":"prop1Data3","prop2":"prop2Data3","prop3":{"name":"Tom","age":"24","class":"graduate"},"prop4":"prop4Data3","prop5":"prop5Data3"}
{"prop1":"prop1Data4","prop2":"prop2Data4","prop3":{"name":"Tom","age":"24","class":"graduate"},"prop4":"prop4Data4","prop5":"prop5Data4"}
]
for( var item in objectArray ){
var objectArrayOnlySelectedProperties = _.pick(objectArray[item] , wantedPropArray);
}
假设第一次迭代让我们看看 objectArrayOnlySelectedProperties 数据,我得到
objectArrayOnlySelectedProperties = {"prop1":"prop1Data1","prop2":"prop2Data1"};
我期待它能给我这样的结果
objectArrayOnlySelectedProperties = {"prop1":"prop1Data1","prop2":"prop2Data1","prop3.name":"Tom"};
我的意思是 _.pick 方法无法查看 prop3 并得到我的 prop3.name。谁能建议如何使用下划线的pick方法来匹配数组中每个对象的子属性。
提前致谢