lodash:基于对象的子数组创建数组

时间:2015-10-23 11:09:35

标签: javascript lodash

我正在学习Lodash这是一个javascript库。

在这里,我有一个像这样的对象:

场景#1:

{
    name: 'abc'
    email: 'abc@example.com'
    z: [
        {
            time: '<any-time>',
            t: 't1'
        },
        {
            time: '<any-time>',
            t: 't2'
        },
        {
            time: '<any-time>',
            t: 't3'
        }
    ]
}

我希望使用lodash获得此结果:

[
    {
        name: 'abc'
        email: 'abc@example.com'
        z: [
            {
                time: '<any-time>',
                t: 't1'
            }           
        ]
    },
    {
        name: 'abc'
        email: 'abc@example.com'
        z: [
            {
                time: '<any-time>',
                t: 't2'
            }           
        ]
    },
    {
        name: 'abc'
        email: 'abc@example.com'
        z: [
            {
                time: '<any-time>',
                t: 't3'
            }           
        ]
    }
]

场景#2:

{
    name: 'abc'
    email: 'abc@example.com'
    z: [
        {
            time: '<any-time>',
            t: 't1'
        },
        {
            time: '<any-time>',
            t: 't1'    // see here t1 repeated
        },
        {
            time: '<any-time>',
            t: 't3'
        }
    ]
}

然后结果应该是:

[
    {
        name: 'abc'
        email: 'abc@example.com'
        z: [
            {
                time: '<any-time>',
                t: 't1'
            },
            {
                time: '<any-time>',
                t: 't1'
            }           
        ]
    },
    {
        name: 'abc'
        email: 'abc@example.com'
        z: [
            {
                time: '<any-time>',
                t: 't3'
            }           
        ]
    }
]

如果我的详细信息没有通知你,请告诉我。

谢谢:)

修改 我尝试了以下代码,

var finalArr = [];
var baseInfo = _.omit(obj, 'z');
var childArr = obj.z;
for(var j = 0; j < childArr.length; j++){
    var flattenObj = angular.copy(baseInfo);
    flattenObj.z = [];
    flattenObj.z.push(childArr[j]);
    finalArr.push(flattenObj);
}

它只是处理场景#1。而且我认为它比lodash使用更多的javascript。

2 个答案:

答案 0 :(得分:0)

此代码适用于两种情况:

// I assume obj is the input object
var result = _.map(obj.z, function(value) {
    return {
        z : value,
        name : obj.name,
        email : obj.email
    };
});

请注意,对于此示例,您根本不需要lodash。你可以写下这个:

// I assume obj is the input object
var result = obj.z.map(function(value) {
    return {
        z : value,
        name : obj.name,
        email : obj.email
    };
});

答案 1 :(得分:0)

在lodash链中,按t对内部数组进行分组,然后将每个组映射到一个新对象:

&#13;
&#13;
function collectAndCombine(obj) {
  return _(obj.z)
  .groupBy('t')
  .map(function(arr) {
    return _.assign(_.pick(obj, ['name', 'email']), { z: arr });
  })
  .value(); 
}

var obj1 = {
    name: 'abc',
    email: 'abc@example.com',
    z: [
        {
            time: '<any-time>',
            t: 't1'
        },
        {
            time: '<any-time>',
            t: 't2'
        },
        {
            time: '<any-time>',
            t: 't3'
        }
    ]
};

var obj2 = {
    name: 'abc',
    email: 'abc@example.com',
    z: [
        {
            time: '<any-time>',
            t: 't1'
        },
        {
            time: '<any-time>',
            t: 't1'    // see here t1 repeated
        },
        {
            time: '<any-time>',
            t: 't3'
        }
    ]
};

console.log(collectAndCombine(obj1));
  
console.log(collectAndCombine(obj2));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script>
&#13;
&#13;
&#13;