0

So, I have an object as follows:

custom_fields:{
21:{
    edit:true
    required:true
    show:true
   }
}

Which in my angular controller is stored here: $scope.page.custom_fields Within this object I have another one, like this:

custom_fields:{
    21:{
        edit:true
        required:true
        show:true
       }
    22:{
        edit:true
        required:true
        show:true
       }
    data:[
         0:{
            display_name:"Text"
            id:21
            name:"Text"
            value:[
                   0:{"TextHere"}
                  ]
        }
         1:{
            display_name:"Text"
            id:22
            name:"Text"
            value:[
                   0:{"TextHere"}
                  ]
        }
    ]
}

This one is stored like this: $scope.page.custom_fields.data = response.data.custom_fields; As you can see the first one is an object of objects while the second one is an array of objects. I don't know why they ended up like this, but I would need to assign the first key in data to the first key in custom fields, so they would look like this in the end:

custom_fields:{
    21:{
        edit:true
        required:true
        show:true
        display_name:"Text2"
        id:21
        name:"Text"
        value:[
               0:{"TextHere"}
              ]
       }
}

I should do this in the angular controller. As you can see every id from data corresponds to the key in custom_fields (in this case 21:{} and data[0:{id:21}]) But they are being put in order by a foreach in php so there is no need to make a foreach in js too, I only have to assign in order every key from custom_fields.data to every key from custom_fields But how can I do this?

Alphonse
  • 661
  • 1
  • 12
  • 29
  • 1
    What is this `data[0:{id:21}]` data structure? Doesn't it given any error? – Jyothi Babu Araja Nov 16 '16 at 06:53
  • data:[0:{display_name:'Text'....}] is not a valid json. It should be data:[{display_name:'Text'.....}]. Then only we can parse correctly to a format you required. – Paulson Peter Nov 16 '16 at 07:06
  • data[0:{id:21}] is the structure for custom_fields.data, it doesn't give any error – Alphonse Nov 16 '16 at 07:07
  • [0:{display_name:'Text'....}] is an array 0 is the key. But I don't know how to properly convert it to an object – Alphonse Nov 16 '16 at 07:09
  • It's not a valid json, see your data !! – Manish Singh Nov 16 '16 at 07:09
  • I don't understand this sentence : "But they are being put in order by a foreach in php so there is no need to make a foreach in js too". If you want to assign item from data to each custom_field, you be able to use a loop to get each custom_field and asssign data values. The only thing you can if you are sure the server send to you an ordered values is to not filter datas by id but just take it depends on the current iterator – Silvinus Nov 16 '16 at 07:14

4 Answers4

0
data[0:{id:21}]

No, this is not possible. Please refer link

numeric-type-as-an-object-key

Community
  • 1
  • 1
J-Mean
  • 1,192
  • 1
  • 8
  • 14
  • This is how it show in my consoler, as a numeric value – Alphonse Nov 16 '16 at 07:11
  • Suggestion!! Is it not possible for you to merge these data changes in your php code. Because in javascript/angularJs you won't be able to use your object like $scope.page.custom_fields.21. Please try to use string object key instead of integer key. – J-Mean Nov 16 '16 at 07:21
  • @J-Mean You are wrong you can still access it like this `$scope.page.custom_fiels['21']` – Weedoze Nov 16 '16 at 07:37
0

It will be very difficult to use the $scope.page.custom_fields.21 later in your code. So better use it as array format instead of object. Following code will help you parse it in array of objects of custom fields.

angular.forEach($scope.custom_fields.data, function(value, idx){ // may be $scope.custom_fields.custom_fields.data as per your code. Please correct accordingly.
   if($scope.custom_fields[value.id]){
       angular.merge($scope.custom_fields.data[idx], $scope.custom_fields[value.id]);
       delete($scope.custom_fields[value.id]);
   } 
});
$scope.custom_fields = $scope.custom_fields.data;
console.log($scope.custom_fields);
Paulson Peter
  • 574
  • 4
  • 12
0

let custom_fields = {
  "21": {
    "edit": true,
    "required": true,
    "show": true
  },
  "22": {
    "edit": true,
    "required": true,
    "show": true
  },
  "data": [{
    "display_name": "Text",
    "id": 21,
    "name": "Text",
    "value": ["TextHere"]
  }, {
    "display_name": "Text",
    "id": 22,
    "name": "Text",
    "value": ["TextHere"]
  }]
};

custom_fields.data.forEach(function(item) {
  let obj = custom_fields[item.id];
  for (let attr in item)
    obj[attr] = item[attr];
});
//Remove the data property
delete custom_fields.data;
console.log(custom_fields);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
-1

As I said in comment, I think you need a loop to asign each custom fields

var i = 0;
for (var key in custom_fields) {
  if (custom_fields.hasOwnProperty(key)) {
     // only if you are sure that data is ordered, else must be filter data by custom_fields[key].id
     custom_fields[key].display_name = data[i].display_name;
     custom_fields[key].name = data[i].name;
     custom_fields[key].value = data[i].value;
     i++;
  }
 }

I hope this will help you

Silvinus
  • 1,445
  • 8
  • 16