_.pick method to match the sub property in an objects array
Solution 1
It looks like this is not supported with underscorejs out of the box. But there is this gist that offers the support as a mix-in:
https://gist.github.com/furf/3208381
Here is some code using the above gist to do what you need: http://jsfiddle.net/wHXCv/1/
_.mixin({
deep: function (obj, key) {
var keys = key.split('.'),
i = 0,
value = null,
n = keys.length;
while ((obj = obj[keys[i++]]) != null && i < n) {};
value = i < n ? void 0 : obj;
var result = {};
result[key]=value;
return result;
}
});
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"}];
var plucked = function(o, wantedPropArray) {
return _.reduce(wantedPropArray, function(acc, val){
acc.push(_.deep(o,val));
return acc;
},[]);
}
var answer = _.map(objectArray, function(o){
return plucked(o, ["prop1","prop2","prop3.name"]);
});
console.log(JSON.stringify(answer));
Solution 2
I don't know about Underscore.js, but you may try this code:
function pick(obj,list){
var newObj={};
for(var i=0;i<list.length;i++){
var str=list[i].split('.');
var o=obj[str[0]];
for(var j=1;j<str.length;j++){
o=o[str[j]];
}
newObj[list[i]]=o;
}
return newObj;
}
Solution 3
Underscore Deep Pick Plugin
I've created this plugin for underscore.js to help with problems like this.
Available at NPM and Bower
npm install deep_pick
bower install deep_pick
Example:
var input = {
one: 1,
two: true,
three: 'Three',
four: [1,2,3,4],
five: {
alpha: 1,
beta: 2,
gamma: 3,
teta: {
alef: 1,
beh: 2,
peh: 3
}
},
answer: '42.00',
description: 'This is an object.'
};
var schema = {
one: true,
three: true,
five: {
alpha: true,
teta: {
beh: true
}
}
};
deepPick(input, schema); // =>
{
one: 1,
three: "Three",
five: {
alpha: 1,
teta: {
beh: 2
}
}
}
Related videos on Youtube
Comments
-
bhargav over 1 year
I am using _.pick method in the following manner
suppose I have an array of strings which are nothing but property names I want to get from each object in array of objects
var wantedPropArray=["prop1","prop2","prop3.name"];
Below is my objects array
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); }
suppose for first iteration lets see objectArrayOnlySelectedProperties data, I am getting
objectArrayOnlySelectedProperties = {"prop1":"prop1Data1","prop2":"prop2Data1"};
I am expecting it to give me the result something like this
objectArrayOnlySelectedProperties = {"prop1":"prop1Data1","prop2":"prop2Data1","prop3.name":"Tom"};
what i mean to say is _.pick method is not able to look into prop3 and get me prop3.name. Can anyone suggest how to use underscores' pick method to match the sub properties of each object in an array.
Thanks in advance