Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据angular4项目中的公共属性分组或合并我的json对象_Json_Angular - Fatal编程技术网

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

如何根据angular4项目中的公共属性分组或合并我的json对象,json,angular,Json,Angular,我的angular4项目中有这个json对象。我想根据公共属性分组或合并此对象 如何基于fixture_desc合并这些对象?我想显示为fixture_desc=“Pepsi Cooler”及其下的所有项目 "items": [ { "barcode": "052000324815", "price": 2, "taxable": false,

我的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,

              }
        ]
我想让Organize显示我的数据。我的预期结果如下:

"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
        }
    ]
}
]

你可以向我们展示你为实现它所做的一切,而不是询问代码,代码生成器也不是。但无论如何,这里的代码可以做同样的你想要的

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 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);
    }

你能提供预期的结果吗?@Myonara我更新了我的问题,请检查一下。谢谢。
    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);
    }