【问题标题】:how to merge objects in javascript without override values如何在不覆盖值的情况下合并javascript中的对象
【发布时间】:2014-02-25 01:33:39
【问题描述】:

如何合并对象中的重复键和对象中的连接值 我有这样的对象

var object1 = {
    role: "os_and_type", 
    value: "windows"
};
var object2 = {
    role: "os_and_type", 
    value: "Android"
};
var object3 = {
    role: "features", 
    value: "GSM"
};

我怎样才能实现这个目标

new_object = [{
    role: "os_and_type",
    value: ["windows", "android"]         
}, {
    role: "features",
    value: ["GSM"]
}];

【问题讨论】:

  • 我不知道为什么人们对此投票。这个问题描述了一个有点耗时的问题。但是 OP 并没有对此进行任何尝试。
  • @rajaprabhuAravindasamy 你不认为这是一个真正的问题吗?我个人喜欢这个问题。
  • @RajaprabhuAravindasamy 在 SO 中获得 100 多票的所有这些问题有什么努力吗?

标签: javascript jquery arrays javascript-objects


【解决方案1】:

给你:

var object1 = {
    role: "os_and_type", 
    value: "windows"
};
var object2 = {
    role: "os_and_type", 
    value: "Android"
};
var object3 = {
    role: "features", 
    value: "GSM"
};

function convert_objects(){
    var output  = [];
    var temp    = [];
    for(var i = 0; i < arguments.length; i++){  // Loop through all passed arguments (Objects, in this case)
        var obj = arguments[i];                 // Save the current object to a temporary variable.
        if(obj.role && obj.value){              // If the object has a role and a value property
            if(temp.indexOf(obj.role) === -1){  // If the current object's role hasn't been seen before
                temp.push(obj.role);            // Save the index for the current role
                output.push({                   // push a new object to the output,
                    'role':obj.role,
                    'value':[obj.value]         //   but change the value from a string to a array.
                });
            }else{                              // If the current role has been seen before
                output[temp.indexOf(obj.role)].value.push(obj.value); // Save add the value to the array at the proper index
            }
        }
    }
    return output;
}

这样称呼它:

convert_objects(object1, object2, object3);

您可以向函数中添加任意数量的对象。

【讨论】:

    【解决方案2】:

    可惜我们没有看到任何尝试。

    function merge(array) {
        var temp = {},
            groups = [],
            l = array.length,
            i = 0,
            item;
        while (item = array[i++]) {
            if (!temp[item.role]) {
                temp[item.role] = {
                    role: item.role,
                    value: [item.value]
                };
            } else if (temp[item.role].value.indexOf(item.value) === -1) {
                temp[item.role].value.push(item.value);
            }
        }
        for (var k in temp) {
            groups.push(temp[k]);
        }
        return groups;
    }
    

    用法:

    var groups = merge([object1, object2, object3]);
    

    【讨论】:

    • 您的代码与 30 分钟前发布并接受的答案有什么区别?只是风格上的差异,我看不到任何附加值。
    • 避免在value中重复。
    • 最好就问题本身解释一下
    • 你的意思是“关于答案本身”吗?
    • 是的,我就是这个意思
    【解决方案3】:

    这是一个使用地图的版本,以避免一遍又一遍地扫描重复项。还使用了一些很酷的方法

    它最终也稍微小了一点。

    function merge(objects) {
        var roles = {};
        objects.forEach(function(obj){
            roles[obj.role] = roles[obj.role] || {};
            roles[obj.role][obj.value] = {};
        });
        return Object.keys(roles).map(function(role){
            return {
                role: role, 
                value: Object.keys(roles[role])
            };
        });
    }
    

    http://jsfiddle.net/mendesjuan/cD7uu/1/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 2019-09-22
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      相关资源
      最近更新 更多