查找键并创建新对象
find key and create new object
我有这个对象数组:
[{
"f_name":"nand",
"lastName":"bhatt",
"gender":"",
"email":"chuchubhatt@webuters.com",
"mobileNo":"0043 664 321 54 76",
"company":"IBM Bhimtal"
}, {
"f_name":"Pooran",
"lastName":"Pradhan",
"gender":"male",
"email":"pp@wt.com",
"mobileNo":"13123123123",
"company":"Google"
}, {
"f_name":"Krishna",
"lastName":"Bhatt",
"gender":"Male",
"email":"krishna@wt.com", "mobileNo":"983232324",
"company":"IBM"
}];
我想要一个与此类似的新对象:
[
{
field_name:f_name, sample: ['nand','Pooran','Krishna'],
field_name:lastName, sample: ['bhatt','Pradhan','Bhatt'],
field_name:gender, sample: ['','male','Male'],
field_name:email, sample: ['chuchubhatt@webuters.com','pp@wt.com','krishna@wt.com'],
field_name:mobileNo, sample: ['0043 664 321 54 76','13123123123','983232324'],
}]
知道我们应该如何在 lodash 中执行此操作吗?
我想你最终想要的是一组对象而不是一个对象。如果我理解正确 - 使用 lodash
函数的组合(link 到 jsbin):
_.reduce(arr, function(result, item, index) {
_.forEach(item, function(value, key) {
var resulted = _.find(result, { field_name: key });
if (!resulted) {
resulted = { field_name: key, sample: [] };
result.push(resulted);
}
resulted.sample.push(value);
});
return result;
}, []);
这是我的方法:
_(collection)
.map(_.keys)
.flatten()
.uniq()
.map(function(item) {
return {
field_name: item,
sample: _.pluck(collection, item)
};
}).value();
前三个调用 map(), flatten(), and uniq(), gets you an array of keys to work with. The next call to map()
generates the output collection. Since you're mapping the keys, the field_name
property is just the item
argument. The sample
property uses pluck() 以对原始集合中的所有字段值进行采样。
我有这个对象数组:
[{
"f_name":"nand",
"lastName":"bhatt",
"gender":"",
"email":"chuchubhatt@webuters.com",
"mobileNo":"0043 664 321 54 76",
"company":"IBM Bhimtal"
}, {
"f_name":"Pooran",
"lastName":"Pradhan",
"gender":"male",
"email":"pp@wt.com",
"mobileNo":"13123123123",
"company":"Google"
}, {
"f_name":"Krishna",
"lastName":"Bhatt",
"gender":"Male",
"email":"krishna@wt.com", "mobileNo":"983232324",
"company":"IBM"
}];
我想要一个与此类似的新对象:
[
{
field_name:f_name, sample: ['nand','Pooran','Krishna'],
field_name:lastName, sample: ['bhatt','Pradhan','Bhatt'],
field_name:gender, sample: ['','male','Male'],
field_name:email, sample: ['chuchubhatt@webuters.com','pp@wt.com','krishna@wt.com'],
field_name:mobileNo, sample: ['0043 664 321 54 76','13123123123','983232324'],
}]
知道我们应该如何在 lodash 中执行此操作吗?
我想你最终想要的是一组对象而不是一个对象。如果我理解正确 - 使用 lodash
函数的组合(link 到 jsbin):
_.reduce(arr, function(result, item, index) {
_.forEach(item, function(value, key) {
var resulted = _.find(result, { field_name: key });
if (!resulted) {
resulted = { field_name: key, sample: [] };
result.push(resulted);
}
resulted.sample.push(value);
});
return result;
}, []);
这是我的方法:
_(collection)
.map(_.keys)
.flatten()
.uniq()
.map(function(item) {
return {
field_name: item,
sample: _.pluck(collection, item)
};
}).value();
前三个调用 map(), flatten(), and uniq(), gets you an array of keys to work with. The next call to map()
generates the output collection. Since you're mapping the keys, the field_name
property is just the item
argument. The sample
property uses pluck() 以对原始集合中的所有字段值进行采样。