I will explain what do I need.. I have a JSON in which I do some operations including creating some input dynamically. The example is here:
jsfiddle
Actually when I create an input (just tapping over an item of the list, for example "asd1"), it fill a new JSON with the new values. What I need is to group the same item in an unique array. So, this is the JSON it creates right now:
{
"objects":[
{
"name":"firstObj",
"attributes":[
{
"attrname":"asd1",
"attrValue":"aaaDDD",
"attrType":"text",
"clicks":1
},
{
"attrname":"asd1",
"attrValue":"qwe",
"attrType":"text",
"clicks":2
}
]
}
]
}
This is what it should be:
{
"objects":[
{
"name":"firstObj",
"attributes":[
{
"attrname":"asd1",
"attrValue":[
"aaaDDD",
"qwe"
],
"attrType":"text",
"clicks":2
}
]
}
]
}
I have grouped the attrValue
in an array because I created the same input two times from "asd1" attrname
. How could I do it? Here is some code by the way:
Javascript:
var myApp = angular.module('myApp',[]);
myApp.controller("mycontroller", ["$scope", "$http",
function($scope, $http){
$scope.getItems = {
"data": [
{
"label": "first",
"objects": [
{
"name": "firstObj",
"attributes": [
{
"attrname": "asd1",
"attrValue": "",
"attrType":"text"
},
{
"attrname": "asd2",
"attrValue": "",
"attrType":"text"
}
]
}
],
"key": "bolla"
},
{
"label": "second",
"objects": [
{
"name": "secondObj",
"attributes": [
{
"attrname": "asd",
"attrValue": "",
"attrType":"text"
},
{
"attrname": "asd3",
"attrValue": "",
"attrType":"text"
}
]
}
],
"key": "2"
}
]
};
$scope.filterSelected = $scope.getItems.data[0].objects;
$scope.myNewArray = {
objects: [
]
}
$scope.createjson = function(attribute, items) {
var obj = {};
obj.name = angular.copy(attribute);
obj.attributes = [];
obj.attributes.push(angular.copy(items));
return obj;
}
$scope.checkIfAttributeExists = function(attribute) {
for(var i=0; i<$scope.myNewArray.objects.length; i++) {
if($scope.myNewArray.objects[i]["name"] == attribute) {
return i;
}
}
return -1;
}
$scope.pushItems = function pushItems(attribute, items) {
if (items.clicks) {
items.clicks++
} else {
items.clicks = 1
}
var index = $scope.checkIfAttributeExists(attribute);
if(index == -1) {
var obj = $scope.createjson(attribute, items);
$scope.myNewArray.objects.push(angular.copy(obj));
}
else {
$scope.myNewArray.objects[index].attributes.push(angular.copy(items));
}
//console.log($scope.myNewArray);
}
// remove function
$scope.removeItem=function(attribute){
attribute.clicks--;
var idx = $scope.myNewArray.objects.indexOf(attribute);
$scope.myNewArray.objects.splice(idx, 1);
}
$scope.showNewJson = function() {
return $scope.myNewArray;
}
}]);
HTML:
<div ng-app='myApp' ng-controller='mycontroller'>
<div data-ng-repeat="item in myNewArray.objects track by $index">
<div data-ng-repeat="attr in item.attributes track by $index">
<div ng-if="attr.attrType == 'text'" >
<input id="form-f{{$index}}" type="text" placeholder="{{attr.attrname}}" data-ng-model="attr.attrValue"/> <button ng-click="removeItem(attr)">Remove</button>
</div>
</div>
</div>
<div data-ng-repeat="object in getItems.data">
<div data-ng-repeat="att in object.objects">
<ul ng-repeat="data in att.attributes">
<li>
<a ng-click="pushItems(att.name, data)">{{data.attrname}}</a>
<span>({{data.clicks}})</span>
</li>
</ul>
</div>
</div>
<p>{{showNewJson()}}</p>
</div>
PS: I use Angularjs
See Question&Answers more detail:
os