选择并合并 JavaScript 个数组
Picking and merging JavaScript arrays
我有一个如下所示的数据集:
[
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
},
{
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
]
我还有一个参考数组,如下所示:
[
{
name: 'ProductId',
map_to: 'item_id',
},
{
name: 'ProductName',
map_to: 'item_name',
},
{
name: 'Description',
map_to: 'description',
},
]
我想要做的是使用该引用数组基本上删除 "unwanted" 数据(即引用数组中不属于 name
的属性)并将键替换为无论它应该映射到什么(即 ProductId->item_id
)。
生成的数组应如下所示:
[
{
item_id: 1,
item_name: 'MyProduct',
description: '.. some text here ..'
},
{
item_id: 2,
item_name: 'MyOtherProduct',
description: '.. some text here ..'
}
]
到目前为止我做了什么
鉴于ref
是参考数组,data
是产品列表。
var only = _.map( ref, 'name' );
var products = [];
async.eachLimit( data, 20, function(item, cb) {
products.push( _.pick(item, only) );
cb();
}, function(err) {
// .. then I iterate over the products array and manually replace each property
// I haven't done this yet
});
这段代码应该可以工作,但感觉有点低效,我想知道是否有更好的方法来实现所需的结果数组,因为我将把它们存储在 MongoDB.
任何人都可以在这里阐明一些问题吗?
您可以尝试 for
循环:
var result = [];
for(var i=0; i<data.length; ++i) {
result[i] = {};
for(var j=0; j<ref.length; ++j)
result[i][ref[j].map_to] = data[i][ref[j].name];
}
var data = [
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
}, {
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
];
var ref = [
{
name: 'ProductID',
map_to: 'item_id',
}, {
name: 'ProductName',
map_to: 'item_name',
}, {
name: 'Description',
map_to: 'description',
},
];
var result = [];
for(var i=0; i<data.length; ++i) {
result[i] = {};
for(var j=0; j<ref.length; ++j)
result[i][ref[j].map_to] = data[i][ref[j].name];
}
console.log(result);
或者,使用 ES5 数组方法,
data.map(function(old) {
return ref.reduce(function(obj, assoc) {
obj[assoc.map_to] = old[assoc.name];
return obj;
}, {});
});
var data = [
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
}, {
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
];
var ref = [
{
name: 'ProductID',
map_to: 'item_id',
}, {
name: 'ProductName',
map_to: 'item_name',
}, {
name: 'Description',
map_to: 'description',
},
];
console.log(data.map(function(old) {
return ref.reduce(function(obj, assoc) {
obj[assoc.map_to] = old[assoc.name];
return obj;
}, {});
}));
我通常发现用 _.pairs
将对象分成对,对这些对进行操作,然后用 _.object
将它们变回一个对象更容易:
function goodPair(product_pair) {
var k = product_pair[0];
return refmap[k] != null;
}
function fixPair(product_pair) {
var k = product_pair[0];
var v = product_pair[1];
return [refmap[k], v];
}
function trimProduct(full_product) {
return _.object(_.pairs(full_product)
.filter(goodPair)
.map(fixPair));
}
console.log(data.map(trimProduct));
通过这种方式,您可以将整个转换转换为一个同步 map
乘积数组。
请注意,这里使用的是 ref
的稍微简化版本 refmap
var refmap = _.object(ref.map(function(r) {
return [r.name, r.map_to];
}));
// OR
var refmap = {
'ProductId': 'item_id',
'ProductName': 'item_name',
'Description': 'description',
};
我会为此使用 map() and transform():
_.map(data, function(item) {
return _.transform(ref, function(result, r) {
result[r.map_to] = item[r.name];
}, {});
});
您正在将 data
映射到新结构。每个新项目都是转换 ref
项目的结果。新对象中的 map_to
键获取原始集合中的 name
值。
我有一个如下所示的数据集:
[
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
},
{
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
]
我还有一个参考数组,如下所示:
[
{
name: 'ProductId',
map_to: 'item_id',
},
{
name: 'ProductName',
map_to: 'item_name',
},
{
name: 'Description',
map_to: 'description',
},
]
我想要做的是使用该引用数组基本上删除 "unwanted" 数据(即引用数组中不属于 name
的属性)并将键替换为无论它应该映射到什么(即 ProductId->item_id
)。
生成的数组应如下所示:
[
{
item_id: 1,
item_name: 'MyProduct',
description: '.. some text here ..'
},
{
item_id: 2,
item_name: 'MyOtherProduct',
description: '.. some text here ..'
}
]
到目前为止我做了什么
鉴于ref
是参考数组,data
是产品列表。
var only = _.map( ref, 'name' );
var products = [];
async.eachLimit( data, 20, function(item, cb) {
products.push( _.pick(item, only) );
cb();
}, function(err) {
// .. then I iterate over the products array and manually replace each property
// I haven't done this yet
});
这段代码应该可以工作,但感觉有点低效,我想知道是否有更好的方法来实现所需的结果数组,因为我将把它们存储在 MongoDB.
任何人都可以在这里阐明一些问题吗?
您可以尝试 for
循环:
var result = [];
for(var i=0; i<data.length; ++i) {
result[i] = {};
for(var j=0; j<ref.length; ++j)
result[i][ref[j].map_to] = data[i][ref[j].name];
}
var data = [
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
}, {
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
];
var ref = [
{
name: 'ProductID',
map_to: 'item_id',
}, {
name: 'ProductName',
map_to: 'item_name',
}, {
name: 'Description',
map_to: 'description',
},
];
var result = [];
for(var i=0; i<data.length; ++i) {
result[i] = {};
for(var j=0; j<ref.length; ++j)
result[i][ref[j].map_to] = data[i][ref[j].name];
}
console.log(result);
或者,使用 ES5 数组方法,
data.map(function(old) {
return ref.reduce(function(obj, assoc) {
obj[assoc.map_to] = old[assoc.name];
return obj;
}, {});
});
var data = [
{
ProductID: 1,
ProductName: 'MyProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage here'
}, {
ProductID: 2,
ProductName: 'MyOtherProduct',
Description: '.. some text here ..',
UnwantedData: 'garbage herAe',
GarbageField: '.. lorem ipsum ..'
}
];
var ref = [
{
name: 'ProductID',
map_to: 'item_id',
}, {
name: 'ProductName',
map_to: 'item_name',
}, {
name: 'Description',
map_to: 'description',
},
];
console.log(data.map(function(old) {
return ref.reduce(function(obj, assoc) {
obj[assoc.map_to] = old[assoc.name];
return obj;
}, {});
}));
我通常发现用 _.pairs
将对象分成对,对这些对进行操作,然后用 _.object
将它们变回一个对象更容易:
function goodPair(product_pair) {
var k = product_pair[0];
return refmap[k] != null;
}
function fixPair(product_pair) {
var k = product_pair[0];
var v = product_pair[1];
return [refmap[k], v];
}
function trimProduct(full_product) {
return _.object(_.pairs(full_product)
.filter(goodPair)
.map(fixPair));
}
console.log(data.map(trimProduct));
通过这种方式,您可以将整个转换转换为一个同步 map
乘积数组。
请注意,这里使用的是 ref
的稍微简化版本 refmap
var refmap = _.object(ref.map(function(r) {
return [r.name, r.map_to];
}));
// OR
var refmap = {
'ProductId': 'item_id',
'ProductName': 'item_name',
'Description': 'description',
};
我会为此使用 map() and transform():
_.map(data, function(item) {
return _.transform(ref, function(result, r) {
result[r.map_to] = item[r.name];
}, {});
});
您正在将 data
映射到新结构。每个新项目都是转换 ref
项目的结果。新对象中的 map_to
键获取原始集合中的 name
值。