如何使用 AngularJS 将数组转换为字符串 url?

How to convert arrray to be string url using AngularJS?

我是 AngularJS 的初学者,现在我正在构建简单的应用程序。

我有一些 HTML 这样的代码

<table class="table" ng-repeat="(attr, assets) in customize.product.attributes">
    <tr>
        <th colspan="2">{{attr}}</th>
    </tr>
    <tr ng-repeat="asset in assets">
        <td>
            {{asset}}
        </td>
        <td>
                <file-uploader class="form-control"
                    image-width="{{galleryConfigs.width}}"
                    image-height="{{galleryConfigs.height}}"
                    image-crop-width="{{galleryConfigs.width}}"
                    image-crop-height="{{galleryConfigs.height}}"
                    max-size="5000"
                    allowed-extensions="png,jpeg,jpg"
                    result-model="customize.assets[attr][asset]">
                </file-uploader>
        </td>
    </tr>
</table>

这是我的代码 JS

$scope.create = function(published){
        var published = published || false,
            customize = angular.copy($scope.customize);

        if(published)
            customize.published = true;

        dataProvider.Customize.save(customize).$promise
        .then(function(_customize){
            if(!_customize) throw 'Error System';

            toast.create({content:'Succes', className:'success'});
            $state.go('pos.product.customize.show',{customizeId:_customize.id});
        }).catch(function (error){
            $log.error(error);
        });
    };

当我推送数据时,结果存储的数据如下所示

"assets" : {
    "part 1" : {
        "black" : [ 
            "/file/c36a3297-11bb-4028-8cb7-d750b98436ec.png", 
            "/file/4a6ec1ed-c3b1-48f9-84c0-61dd0f6da08a.png", 
            "/file/c66ac97a-18be-4e79-9ec1-ca67ab37d3f3.png"
        ],
        "red" : [ 
            "/file/c36a3297-11bb-4028-8cb7-d750b98145ec.png", 
            "/file/4a6ec1ed-c3b1-48f9-8cb7-61dd0f6da07a.png", 
            "/file/c66ac97a-18be-4e79-8cb7-ca67ab37d3c3.png"
        ]
    }
}

尽管我想用如下结构保存数据

"assets" : {
    "part 1" : {
        "black" :"/file/a11c553f-de74-48e2-93a0-6fc72a54fa9b.png",
        "red"   :"/file/a11c553f-de74-48e2-93a0-6fc72a54ga8c.png" 
    }
}

有什么办法吗?

这是一个可以从数组创建对象的函数:

function arrayToObj(a) {

   //Create an empty object
   var obj = {};

   //Go through the array and add elements as properties to the object
   a.forEach(function(element, idx){
      obj[String(idx)] = element;       
   }) 

   return obj;

}

此函数将 ['apple', 'orange', 'grapes'] 数组转换为 {1: 'apple', 2: 'orange', 3: 'grapes'}

如果您需要一个包含单个 属性 的对象,其中包含数组的所有元素作为字符串,请按以下步骤操作:

function arrayToObjWithOneProperty(a) {

       //Create an empty object
       var obj = {value: ''};

       //Go through the array and add elements as properties to the object
       a.forEach(function(element){
          obj.value = obj.value + element + ',';       
       }) 

       return obj;

}

此函数会将 ['apple', 'orange', 'grapes'] 转换为 {value: 'apple,orange,grapes'}

假设资产可以包含很多部分,下面是一个解决方案,它将 return 每个颜色数组中的第一项:

var result = _.mapObject(assets, function(asset){
    return _.mapObject(asset, _.first);
});

类似于我昨天对How to change data array to be object using javascript?

问题的回答

var assets = {
     "part 1" : {
         "black" : [ 
             "/file/c36a3297-11bb-4028-8cb7-d750b98436ec.png", 
             "/file/4a6ec1ed-c3b1-48f9-84c0-61dd0f6da08a.png", 
             "/file/c66ac97a-18be-4e79-9ec1-ca67ab37d3f3.png"
         ],
         "red" : [ 
             "/file/c36a3297-11bb-4028-8cb7-d750b98145ec.png", 
             "/file/4a6ec1ed-c3b1-48f9-8cb7-61dd0f6da07a.png", 
             "/file/c66ac97a-18be-4e79-8cb7-ca67ab37d3c3.png"
         ]
     }
 }

var result = _.mapObject(assets, function(asset){
 return _.mapObject(asset, _.first);
});

document.getElementById('results').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore.js"></script>

<pre id="results"></pre>