将嵌套的对象数组转换为嵌套的属性模板对象

时间:2016-08-19 18:23:57

标签: javascript angularjs arrays

我创建了一个对象数组,我希望将其转换为嵌套属性模板对象。将根据检查的项目创建嵌套对象。

第一级对象已经过检查' key设置为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
}

2 个答案:

答案 0 :(得分: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;

}

我不认为这是您想要的确切代码,但希望它能为您指明方向。

答案 1 :(得分:0)

在Alexey的帮助下,我能够正确地转换所有内容。下面的函数现在可以正确转换所有内容。

$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);
    });
}