【问题标题】:How to convert arrray to be string url using AngularJS?如何使用AngularJS将数组转换为字符串url?
【发布时间】:2016-01-09 08:26:16
【问题描述】:

我是 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" 
    }
}

有什么办法吗?

【问题讨论】:

  • 你的阵列在哪里?
  • @CharlieH 再次看到我的问题,请帮忙
  • 您只需要数组中的一个元素吗?
  • 这意味着黑色数组中的三个元素需要三个单独的条目
  • @CharlieH 是的,我只需要一个元素,黑色数据不是数组而是对象

标签: javascript angularjs underscore.js lodash


【解决方案1】:

假设资产可以包含许多部分,这里有一个解决方案,它将返回每个颜色数组中的第一项:

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>

【讨论】:

  • 我用过这种方式,但是存储的数据和我的实现不匹配,有什么办法吗?
  • 我无法从您的 sn-p 中解决您要执行的操作 - 它无法运行。尝试将您的资产对象粘贴到我的答案中的代码 sn-p 中,看看结果如何。
【解决方案2】:

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

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'}

【讨论】:

  • 那么如何将只有苹果和数组中的数据变成字符串?
  • 请重新表述您的问题。语法看不懂。
  • 好的,我正在编辑我的问题,在此之前我会感谢您的帮助
  • 我已经提供了可以做你想做的代码。您应该能够更改这个简单的代码以满足您作为程序员的要求。
猜你喜欢
  • 2019-03-18
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多