我AngularJS的初学者,现在我建立简单的应用程序。
我有一些HTML code这样的
<表类=表NG重复=(ATTR,资产)在customize.product.attributes> &所述; TR> 百分位列跨度=2> {{ATTR}}&下; /第i < / TR> < TR NG重复=资产的资产> &所述; TD> {{资产}} < / TD> &所述; TD> <文件上传类=表格控 图像宽度={{galleryConfigs.width}} 图像高度={{galleryConfigs.height}} 图像裁切宽度={{galleryConfigs.width}} 图像作物高度={{galleryConfigs.height}} 最大规模=5000 允许的扩展=PNG,JPEG,JPG 结果模型=customize.assets [ATTR] [财富]> < /文件上传> < / TD> < / TR>< /表>
这是我的code JS
$ scope.create =功能(出版){ VAR公布= ||出版假, 定制= angular.copy($ scope.customize); 如果(出版) customize.published = TRUE; dataProvider.Customize.save(自定义)。$承诺 。然后(功能(_customize){ 如果(!_自定义)扔系统错误; toast.create({内容:更迭,产品类别:'成功'}); $ state.go('pos.product.customize.show',{customizeId:_customize.id}); })赶上(功能(错误){ $ log.error(错误); }); };
当我把数据原来存储的数据如下图所示那样
资产:{ 第1部分 : { 黑色 : [ /file/c36a3297-11bb-4028-8cb7-d750b98436ec.png /file/4a6ec1ed-c3b1-48f9-84c0-61dd0f6da08a.png /file/c66ac97a-18be-4e79-9ec1-ca67ab37d3f3.png ] 红: /file/c36a3297-11bb-4028-8cb7-d750b98145ec.png /file/4a6ec1ed-c3b1-48f9-8cb7-61dd0f6da07a.png /file/c66ac97a-18be-4e79-8cb7-ca67ab37d3c3.png ] }}
尽管我想保存与结构的数据如下图所示。
资产:{ 第1部分 : { 黑:/文件/ a11c553f-de74-48e2-93a0-6fc72a54fa9b.png 红:/文件/ a11c553f-de74-48e2-93a0-6fc72a54ga8c.png }}
有没有办法做到这一点?
解决方案假设资产可以包含很多地方,这里是将每个颜色数组中返回的第一个项目的解决方案:
VAR的结果= _.mapObject(资产,函数(资产){ 返回_.mapObject(资产,_.first);});
要我昨天给了这个问题的答案How改变数据阵列使用JavaScript是对象?
\r\r
VAR资产= {\r\t 第1部分 : {\r\t 黑色 : [\r/file/c36a3297-11bb-4028-8cb7-d750b98436ec.png\r/file/4a6ec1ed-c3b1-48f9-84c0-61dd0f6da08a.png\r/file/c66ac97a-18be-4e79-9ec1-ca67ab37d3f3.png\r]\r红:\r/file/c36a3297-11bb-4028-8cb7-d750b98145ec.png\r/file/4a6ec1ed-c3b1-48f9-8cb7-61dd0f6da07a.png\r/file/c66ac97a-18be-4e79-8cb7-ca67ab37d3c3.png\r]\r}\r}\r\rVAR的结果= _.mapObject(资产,函数(资产){\r返回_.mapObject(资产,_.first);\r});\r\r的document.getElementById('结果')的textContent = JSON.stringify(结果);
\r
&LT;脚本SRC =https://cdnjs.cloudflare.com/ajax /libs/underscore.js/1.8.2/underscore.js\"></script>\r\r&LT; pre ID =结果&GT;&LT; / pre&GT;
\r\r\r
I'm beginner of AngularJS, and now I'm building simple app.
I have some HTML code like this
<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>
And this is my code 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);
});
};
When I push the data turns out the data stored as shown below like that
"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"
]
}
}
Even though I want to save the data with the structure as shown below
"assets" : {
"part 1" : {
"black" :"/file/a11c553f-de74-48e2-93a0-6fc72a54fa9b.png",
"red" :"/file/a11c553f-de74-48e2-93a0-6fc72a54ga8c.png"
}
}
Is there any way to do it?
解决方案Assuming that assets can contain many parts, here's a solution that will return the first item in each colour array:
var result = _.mapObject(assets, function(asset){
return _.mapObject(asset, _.first);
});
Similar to the answer I gave yesterday to the question 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>