基于子的价值在合并数组对象 [英] Merging objects in array based on sub-value

查看:102
本文介绍了基于子的价值在合并数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有填充对象的数组。我如何去了解这个数组中合并对象时,他们有一个匹配的特定子价值?

I have an array populated with objects. How do I go about merging objects inside this array when they have a matching specific sub value?

我的数组是这样的:

var data = [
    {
        prod_name:"test1", type:"1", color:"white", product_id:"5"
    },
    {
        prod_name:"test2", type:"1", color:"green", product_id:"7"
    },
    {
        prod_name:"test2", type:"2", color:"green", product_id:"7"
    },
    {
        prod_name:"test3", type:"4", color:"red", product_id:"8"
    },
    {
        prod_name:"test4", type:"2", color:"white", product_id:"21"
    }    
];

我想根据匹配的合并对象的product_id

在情况下,值是不一样的,我想保持这两个值,用逗号分隔。因此,previous数组的结果将变成:

In case the values are not the same, I want to keep BOTH values, seperated with a comma. So the outcome of the previous array would become:

[
    {
        prod_name:"test1", type:"1", color:"white", product_id:"5"
    },
    {
        prod_name:"test2", type:"1,2", color:"green", product_id:"7"
    },
    {
        prod_name:"test3", type:"4", color:"red", product_id:"8"
    },
    {
        prod_name:"test4", type:"2", color:"white", product_id:"21"
    }    
];

该阵列缩水1,因为它有一个重复的,而这两个值,凡不一样的由逗号类型合并,分开:1,2

我以为可以如下:

jQuery.each( data, function( i, val ) {
    var productID = val.product_id;
    var index_key = i;
    jQuery.each( data, function( i, val ) {
        if(val.product_id == productID && data[index_key] != data[i]){
            jQuery.extend(data[index_key], data[i]);
        }
    });
});

但是,这仅覆盖键入第一个撞价值,并保持这两个条目。

But this only overwrites the type value of the first hit and keeps both entries.

有关mergable项的值 PROD_NAME 的product_id 都是一样的。

For "mergable" items the values prod_name and product_id are always the same.

有谁知道的一种方式,以达到预期的效果?

Does anyone know of a way to achieve the desired result?

更新:
不同的值(产品属性)可能在稍后的阶段加入。为此我preFER不专门检查键入属性的方法,而是检查eveything这不是的product_id PROD_NAME ,如果它有一个命中,合并了这个用逗号。

UPDATE: Different values(product attributes) might be added in a later stage. Therefor I'd prefer a method that does not specifically check for the type attribute, but rather checks for eveything that is NOT product_id or prod_name and if it has a hit, merges this with a comma.

FIDDLE

FIDDLE

推荐答案

我在例如波纹​​管做的,首先我创建对象来实现唯一值(由 PROJECT_ID ) ,然后转换对象阵列。在第一个循环我检查,如果项目不水库存在 - 放 RES ,否则我只能更改属性类型

What I do in example bellow., first I create object to achieve unique values (by project_id), and then converted object to array. In first loop I check if item does not exists in res - put to res, else I change only property type

var res = {};

$.each(data, function (key, value) {
    if (!res[value.product_id]) {
        res[value.product_id] = value;    
    } else {
        res[value.product_id].type = [
          res[value.product_id].type, 
          value.type
        ].join(',');
    }
});

data = $.map(res, function (value) {
  return value;
}); 

console.log(data);

示例

这篇关于基于子的价值在合并数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆