挑选和合并 JavaScript 数组

Picking and merging JavaScript arrays

本文关键字:数组 JavaScript 合并 挑选      更新时间:2023-09-26

我有一个看起来像这样的数据集:

[
    {
        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',
    },
]

我想做的是使用该引用数组基本上删除"不需要的"数据(即引用数组中未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() 和 transform() 来做这件事:

_.map(data, function(item) {
    return _.transform(ref, function(result, r) {
        result[r.map_to] = item[r.name];
    }, {});
});

您正在将data映射到新结构。每个新项都是转换ref项的结果。新对象中的 map_to 键获取原始集合中的name值。