用lodash映射里面的地图

时间:2016-01-18 19:45:08

标签: javascript lodash

我需要将数字字符串转换为INT,我在_.map中使用lodash和_.map

请看一下

mydata=[
      {
        "RHID": "1",
        "CD_DOC_ID": "1",
        "SEQ": "1",
        "NR_DOCUMENTO": "3333",
        "EMISSOR": "Lisboas643223",
        "DT_EMISSAO": "2004-01-12",
        "DT_VALIDADE": "2014-12-17",
        "NOME_FICHEIRO": "1EV5.pdf"
  }
];
primary = ["RHID","CD_DOC_ID","SEQ"];
mydata = _.map(primary, function(key) {
    _.map(mydata, function(obj) {
        console.log(key +"----"+obj[key]);    
        return parseInt(obj[key], 10);    
    });
});
console.log(mydata);

https://jsfiddle.net/wubqc8yz/

1 个答案:

答案 0 :(得分:2)

mydata = [{
    "RHID": "1",
    "CD_DOC_ID": "1",
    "SEQ": "1",
    "NR_DOCUMENTO": "3333",
    "EMISSOR": "Lisboas643223",
    "DT_EMISSAO": "2004-01-12",
    "DT_VALIDADE": "2014-12-17",
    "NOME_FICHEIRO": "1EV5.pdf"
}];

primary = ["RHID", "CD_DOC_ID", "SEQ"];

// iterate over the objects in <mydata> and update them via _.map
_.map(mydata, function (data) {
    // iterate over property names in <primary> with _.each (read-only)
    _.each(primary, function (key) {
        // call parseInt() and store the return value right back in the same spot
        data[key] = parseInt(data[key], 10);
    });

    // return the modified object
    return data;
})

console.log(mydata);

fiddle