如何使用AngularJS将arrray转换为字符串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"
}
}
有什么办法吗?
假设资产可以包含多个部分,这里有一个解决方案,它将返回每个颜色数组中的第一个项目:
var result = _.mapObject(assets, function(asset){
return _.mapObject(asset, _.first);
});
类似于我昨天给出的关于如何使用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>
下面是一个可以从数组中创建对象的函数:
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'}
相关文章:
- 如何有效地将游戏数据存储在URL查询字符串中
- Datatables查询url字符串:需要regex来修复url解析
- 在字符串中查找所有图像 src URL 并存储在数组中
- 如何从django中的url解析javascript字符串
- 未捕获的InvalidValueError:setIcon:不是字符串;并且没有url属性;并且没有路径属性
- javascript在动态Url中匹配并添加字符串
- 将字符串添加到页面url
- 如何在AngularJS URL中的hashtag(#)前面加上字符串
- 将URL字符串${userid}替换为javascript对象中的实际用户id
- 使用Regex查找URL中的特定字符串
- 从 url 获取字符串值
- Javascript:从拆分字符串 url 加载多个图像
- 如何使用AngularJS将arrray转换为字符串url
- Blob字符串url到PDF
- 查询字符串URL的Javascript URL验证失败
- 将Blob字符串URL转换为Blob,然后转换为base64
- 使用javascript从字符串(URL)中删除子字符串(URL)
- 如何在JavaScript中使用正则表达式从字符串(URL)中删除unicode特殊字符
- 如何替换字符串url
- 在Java脚本中解码UTF16编码的字符串(URL)