javascript - 使用 Underscorejs 按位置合并两组 js 对象

标签 javascript underscore.js

对象 1:

{a50: "PTP-1",b51: "09+75",c52: "112D",d53: "60.745",e54: "72.698",f55: "72.695",g56: "0.003",h57: null,i58: null, j59: "68.918", k60: null}

对象2:

[{ipid:'50', type:'Constant'},{ipid:'51', type:'Constant'},{ipid:'52', type:'Constant'},{ipid:'53', type:'Constant'},{ipid:'54', type:'Constant'},{ipid:'55', type:'Constant'},{ipid:'56', type:'Constant'},{ipid:'57', type:'Variable'},{ipid:'58', type:'Variable'},{ipid:'59', type:'Variable'},{ipid:'60', type:'Variable'}]

(Screenshot)

我有两个具有相同长度的 javascript 对象,我想组合并生成一个新的 javascript 对象

[
    {ipid: "50", type: "Constant", a50: "PTP-1"},
    {ipid: "51", type: "Constant", b51: "09+75"},
    {ipid: "52", type: "Constant", c52: "112D"},
    {ipid: "53", type: "Constant", d53: "60.745"},
    {ipid: "54", type: "Constant", e54: "72.698"},
    {ipid: "55", type: "Constant", f55: "72.695"},
    {ipid: "56", type: "Constant", g56: "0.003"},
    {ipid: "57", type: "Variable", h57: null},
    {ipid: "58", type: "Variable", i58: null},
    {ipid: "59", type: "Constant", j59: "68.918"},
    {ipid: "60", type: "Variable", k60: null}
]

正在寻找更快、更高效的代码。 (也许只是在原生js中)

谢谢

最佳答案

看看这个:

var obj1 = {a50: "PTP-1",b51: "09+75",c52: "112D",d53: "60.745",e54: "72.698",f55: "72.695",g56: "0.003",h57: null,i58: null, j59: "68.918", k60: null}
var obj2 = [{ipid:'50', type:'Constant'},{ipid:'51', type:'Constant'},{ipid:'52', type:'Constant'},{ipid:'53', type:'Constant'},{ipid:'54', type:'Constant'},{ipid:'55', type:'Constant'},{ipid:'56', type:'Constant'},{ipid:'57', type:'Variable'},{ipid:'58', type:'Variable'},{ipid:'59', type:'Variable'},{ipid:'60', type:'Variable'}]

var output = [],
    key = '';

for(key in obj1){
    if(obj1.hasOwnProperty(key)){
        var current = obj2.filter(function(e){
            return e.ipid == key.substr(1)
        });
        for(var i = 0; i < current.length; i++){
            var temp = {
                ipid: current[i].ipid,
                type: current[i].type
            }
            temp[key] = obj1[key];
            output.push(temp);
        }
    }
}

这将返回以下结果:

[
    {"ipid": "50", "type": "Constant", "a50": "PTP-1"},
    {"ipid": "51", "type": "Constant", "b51": "09+75"},
    {"ipid": "52", "type": "Constant", "c52": "112D"},
    {"ipid": "53", "type": "Constant", "d53": "60.745"},
    {"ipid": "54", "type": "Constant", "e54": "72.698"},
    {"ipid": "55", "type": "Constant", "f55": "72.695"},
    {"ipid": "56", "type": "Constant", "g56": "0.003"},
    {"ipid": "57", "type": "Variable", "h57": null},
    {"ipid": "58", "type": "Variable", "i58": null},
    {"ipid": "59", "type": "Variable", "j59": "68.918"},
    {"ipid": "60", "type": "Variable", "k60": null}
]

关于javascript - 使用 Underscorejs 按位置合并两组 js 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756055/

相关文章:

javascript - 下划线模板添加额外的 <h2> 标签

javascript - 对对象数组进行分组的最有效方法

javascript - 克隆 LI div,以便将其放置在原始 LI 之后而不是末尾

javascript - 带有 args 下划线的去抖功能

javascript - 是否可以更改带有项目的对象中父/子的顺序?

javascript - 渲染中的箭头函数与。携带数据的属性

javascript - 有条件地遍历数组

javascript - 删除文本之间的多个空格

javascript - jquery if 语句检测到高度

javascript - 窗口失焦时恢复选项