如何根据angular4项目中的公共属性分组或合并我的json对象

时间:2017-08-07 12:00:57

标签: json angular

我的angular4项目中有这个json对象。我想基于公共属性分组或合并此对象。

如何根据fixture_desc合并这些对象?我希望显示为fixture_desc =" Pepsi Cooler"并在其下所有项目。

 "items": [
              {
                "barcode": "052000324815",
                "price": 2,
                "taxable": false,
                "description": "Gatorade Cool Blue",
                "tax_rate": 0,
                "inventory": 8,
                "fixture_desc": "Pepsi Cooler"

              },
              {
                "barcode": "052000328660",
                "price": 2,
                "taxable": false,
                "description": "Gatorade Fruit Punch ",
                "tax_rate": 0,
                "inventory": 8,
                "fixture_desc": "Pepsi Cooler",
                "min": 2,
                "max": 8,
                "_id": "58feb29a3a5c560011b1b96c"
              },
              {
                "barcode": "052000328684",
                "price": 2,
                "taxable": false,
                "description": "Gatorade Lemon Lime ",
                "tax_rate": 0,
                "inventory": 4,
                "fixture_desc": "Pepsi Cooler",
                "min": 1,
                "max": 4,

              }
        ]

我想组织显示我的数据。我的预期结果是这样的:

"items": [
{
    "name": "Pepsi Cooler”,
    "items": [
    {
          "barcode": "052000324815",
          "price": 2,
          "taxable": false,
          "description": "Gatorade Cool Blue",
          "tax_rate": 0,
          "inventory": 8,
          "fixture_desc": "Pepsi Cooler"
        },
        {
          "barcode": "052000328660",
          "price": 2,
          "taxable": false,
          "description": "Gatorade Fruit Punch ",
          "tax_rate": 0,
          "inventory": 8,
          "fixture_desc": "Pepsi Cooler"

        }
    ]
}
{
    "name": “Cook Cooler”,
    "items": [
    {
          "barcode": "052000324815",
          "price": 2,
          "taxable": false,
          "description": "Gatorade Cool Blue",
          "tax_rate": 0,
          "inventory": 8,
          "fixture_desc": "Cook Cooler
        },
        {
          "barcode": "052000328660",
          "price": 2,
          "taxable": false,
          "description": "Gatorade Fruit Punch ",
          "tax_rate": 0,
          "inventory": 8,
          "fixture_desc": "Cook Cooler
        }
    ]
}
]

2 个答案:

答案 0 :(得分:1)

我解决了我的问题。我发布它,如果它的帮助任何人。 这是我的数组:

    var myobj={"items": [
                      {
                        "barcode": "052000324815",
                        "description": "Gatorade Cool Blue",
                        "fixture_desc": "Pepsi Cooler"
                      },
                      {
                        "barcode": "052000328660",
                        "description": "Gatorade Fruit Punch ",
                        "fixture_desc": "Pepsi Cooler",
                      },
                      {
                        "barcode": "052000328684",
                        "description": "Gatorade Lemon Lime ",
                        "fixture_desc": "Pepsi Cooler",
                      }
                ]
    }
    and this is my solution:
     result:any[] = [];
     finalResult(myObj:any){
                var Obj = myObj;
                var groups:any[]=[];
    for (let a of Obj) {
        groups[a.fixture_desc] = groups[a.fixture_desc] || {
                        name:a.fixture_desc,
                        items:[],
                        isfixtureExpanded:false
                    };
                    groups[a.fixture_desc].items.push({

                            barcode: a.barcode,
                            description: a.description,
                            fixture_desc:a.fixture_desc,

                    }); 
                }

         for (var key in groups){

            this.result.push(groups[key]);
        }
                }

console.log(this.result);
    }

答案 1 :(得分:0)

而不是询问代码,你可以告诉我们你为实现它所做的一切,SO不是代码生成器。但无论如何这里的代码可以做你想做的同样的事情。

function group(array, prop) {
var result = [];
    array.forEach(function(val) {
        var found = false;
        for(var i=0;i<result.length;i++) {
            if(result[i].name === val[prop]) {
                result[i].items.push(val);
                found = true;
            }
        }
    });
}

将其命名为

var finalResult = group(items,'fixture_desc');