2016-08-19 19 views
0

ネストされたプロパティテンプレートオブジェクトに変換したいオブジェクトの配列を作成しました。ネストされたオブジェクトは、チェックされた項目に基づいて作成されます。オブジェクトのネストされた配列をネストされたプロパティテンプレートオブジェクトに変換する

最初のレベルのオブジェクトは、プロパティテンプレートオブジェクトの先頭に常に追加されるため、「チェック済み」キーがnullに設定されています。

最初のレベルのオブジェクトはプロパティを持っていない、またはプロパティのどれがチェックされていない、それは次のようになります場合:

{staff: true} 

しかし、オブジェクトのプロパティを持っているとプロパティのいくつかがチェックされている場合、それは次のようになります。

{staffedLocation: ['schedulingGroup', 
        {property: 'staff', subProperties: ['description']} 
        ] 
} 

これは非常に単純な例であり、いくつかのケースでは、ネストされたオブジェクトの配列は、さらに下に行くことができますので、私は、私は再帰関数を作成する必要があります知っています。しかし、オブジェクトの最初のレベルが他のレベルと完全に異なる場合、再帰的にそれを作成する方法については、私はうまくいきませんでした。手持ちの

オブジェクトの配列の例は次のようになります。

[ 
    {checked: null, name: "staffedLocation", properties: [ 
     {checked: null, name: "oid", properties: null, toggle: null, type: "integer"}, 
     {checked: null, name: "_class", properties: null, toggle: null, type: "string"}, 
     {checked: true, name: "schedulingGroups", properties: null, toggle: null, type: "list"}, 
     {checked: null, name: "staff", properties: [ 
      {checked: null, name: "oid", properties: null, toggle: null, type: "integer"}, 
      {checked: null, name: "_class", properties: null, toggle: null, type: "string"}, 
      {checked: true, name: "description", properties: null, toggle: null, type: "string"}, 
      {checked: null, name: "limits", properties: null, toggle: null, type: "list"}, 
      {checked: null, name: "weeklyMaxHours", properties: null, toggle: null, type: "integer"} 

     ], toggle: true, type: "list"}, 
    ], toggle: true, type: "staffedLocation"}, 
    {checked: null, 
    name: "staff", properties: [ 
     {checked: null, name: "oid", properties: null, toggle: null, type: "integer"}, 
     {checked: null, name: "_class", properties: null, toggle: null, type: "string"}, 
     {checked: null, name: "description", properties: null, toggle: null, type: "string"}, 
     {checked: null, name: "limits", properties: null, toggle: null, type: "list"}, 
     {checked: null, name: "weeklyMaxHours", properties: null, toggle: null, type: "integer"} 
    ], toggle: true, type: "staff"}, 
    {checked: null, name: "assignedShifts", properties: null, toggle: null, type: "shiftForEmail"}, 
    {checked: null, name: "editedShifts", properties: null, toggle: null, type: "shiftForEmail"}, 
    {checked: null, name: "deletedShifts", properties: null, toggle: null, type: "shiftForEmail"}, 
    {checked: null, name: "unassignedShifts", properties: null, toggle: null, type: "shiftForEmail"}, 
    {checked: null, name: "rangeStart", properties: null, toggle: null, type: "timestamp"}, 
    {checked: null, name: "rangeEnd", properties: null, toggle: null, type: "timestamp"} 
] 

そして、私はそれがそのようにのようなネストされたオブジェクトに変換されたい:

{ 
    staffedLocation: ['schedulingGroup',{property: 'staff',subProperties: ['description']}], 
    staff: true, 
    assignedShifts: true, 
    editedShifts: true, 
    deletedShifts: true, 
    unassignedShifts: true, 
    rangeStart: true, 
    rangeEnd: true 
} 
+0

第1レベルが他のレベルと異なる場合は、第1レベルを反復するときに再帰関数を実行する必要があります。 [配列 'map'メソッド](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)をループに使用することができます。 – estus

答えて

0

何実際にあなた再帰関数を最上位オブジェクトのプロパティにのみ適用し、最上位オブジェクトには適用しないでください。

// No ES6 sugar 
function convert(content) { 

function convertValues(props) { 
    if (!(props && props.length && props.length > 0)) { 
     return true; 
    } 
    var convertedProps = []; 

    props.forEach(function(prop) { 
     // Recursive 
     var values = convertValues(prop.properties); 
     if (prop.checked || values.length) { 

      convertedProps.push(prop.name, values); 
     } 
    }); 

    return convertedProps; 
} 

var convertedContent = {}; 

content.forEach(function(obj) { 
    convertedContent[obj.name] = convertValues(obj.properties); 
}); 

return convertedContent; 

}

私はそれがあなたが望む正確なコードではないと思っていますが、あなたに方向性を与えることを願っています。

+0

アレクセイ助けてくれてありがとう!私は微調整をして、必要なものを返すためにそれを得ることができるかどうかを見ます –

0

アレクシーの助けを借りて、私はすべてを適切に変換することができました。以下の関数は、すべてを正しく変換します。

$scope.convertToPropertyTemplate = function(content) { 
    function convertValues(props) { 

     if (!(props && props.length && props.length > 0)) { 
      return true; 
     } 
     var convertedProps = []; 

     props.forEach(function(prop) { 
      // Recursive 
      var convertedObj = {}; 
      var values = convertValues(prop.properties); 
      if (prop.checked) { 
       convertedProps.push(prop.name); 
      } 

      if (values.length){ 
       convertedObj["property"] = prop.name; 
       convertedObj["subProperties"] = values; 
       convertedProps.push(convertedObj); 
      } 
     }); 

     return convertedProps; 
    } 

    $scope.convertedContent = {}; 

    content.forEach(function(obj) { 
     $scope.convertedContent[obj.name] = convertValues(obj.properties); 
    }); 
} 
関連する問題