javascript - How to get ArrayObject all value using flatmap lodash - Stack Overflow

admin2025-04-09  0

I have Array Object and in this Array Object i want filter data, group-by data in particular key and Array Object blow.

    var data = [{
                'value': [{
                    'id': '1',
                    'list': [
                        { 'name': 'test', 'mapp_id': 1 },
                        { 'name': 'test1', 'mapp_id': 1 },
                        { 'name': 'test2', 'mapp_id': 1 },
                        { 'name': 'test3', 'mapp_id': 2 }
                    ]
                }, {
                    'id': '2',
                    'list': [
                        { 'name': 'test4', 'mapp_id': 2 },
                        { 'name': 'test5', 'mapp_id': 2 },
                        { 'name': 'test6', 'mapp_id': 2 },
                        { 'name': 'test7', 'mapp_id': 1 }
                    ]
                }

                ]
            },{
                'value': [{
                    'id': '3',
                    'list': [
                        { 'name': 'test8', 'mapp_id': 3 },
                        { 'name': 'test9', 'mapp_id': 3 },
                        { 'name': 'test10', 'mapp_id': 1 },
                        { 'name': 'test11', 'mapp_id': 1 }
                    ]
                }, {
                    'id': '4',
                    'list': [
                        { 'name': 'test12', 'mapp_id': 1 },
                        { 'name': 'test13', 'mapp_id': 1 },
                        { 'name': 'test14', 'mapp_id': 2 },
                        { 'name': 'test14', 'mapp_id': 2 }
                    ]
                }

                ]
            }]

i want to data like way and some code try but no fully success some success.

    var output = {
                '1': [
                          { 'name': 'test', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test1', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test2', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test7', 'mapp_id': 1 ,'id': '2'},
                          { 'name': 'test10', 'mapp_id': 1, 'id': '3' },
                          { 'name': 'test11', 'mapp_id': 1, 'id': '3' },
                          { 'name': 'test12', 'mapp_id': 1,'id': '4' },
                          { 'name': 'test13', 'mapp_id': 1 ,'id': '4'}
                     ],
                '2': [
                          { 'name': 'test3', 'mapp_id': 2 ,'id': '1'}, 
                          { 'name': 'test4', 'mapp_id': 2,'id': '2' },
                          { 'name': 'test5', 'mapp_id': 2 ,'id': '2'},
                          { 'name': 'test6', 'mapp_id': 2,'id': '2' }, 
                          { 'name': 'test14', 'mapp_id': 2 ,'id': '4'},
                          { 'name': 'test14', 'mapp_id': 2 ,'id': '4'}
                ],
                '3':[ 
                          { 'name': 'test8', 'mapp_id': 3 ,'id': '4'},
                          { 'name': 'test9', 'mapp_id': 3 ,'id': '4'}
                    ]

            };

lodash library used in her documentation refer .17.10

i can try some code and that code post blow but no fully success.

var result= _.flatMap(data, item => 
        _(item.value)
        .flatMap('list')
        .value()
        );
result=_.groupBy(result, function(b) { return b.mapp_id})

and my code out blow posted

output = {
            '1': [
                      { 'name': 'test', 'mapp_id': 1 },
                      { 'name': 'test1', 'mapp_id': 1 },
                      { 'name': 'test2', 'mapp_id': 1 },
                      { 'name': 'test7', 'mapp_id': 1 },
                      { 'name': 'test10', 'mapp_id': 1 },
                      { 'name': 'test11', 'mapp_id': 1 },
                      { 'name': 'test12', 'mapp_id': 1 },
                      { 'name': 'test13', 'mapp_id': 1 }
                 ],
            '2': [
                      { 'name': 'test3', 'mapp_id': 2 }, 
                      { 'name': 'test4', 'mapp_id': 2 },
                      { 'name': 'test5', 'mapp_id': 2 },
                      { 'name': 'test6', 'mapp_id': 2 }, 
                      { 'name': 'test14', 'mapp_id': 2 },
                      { 'name': 'test14', 'mapp_id': 2 }
            ],
            '3':[ 
                      { 'name': 'test8', 'mapp_id': 3 },
                      { 'name': 'test9', 'mapp_id': 3 }
                ]

        };

help me guys how i can do this?

I have Array Object and in this Array Object i want filter data, group-by data in particular key and Array Object blow.

    var data = [{
                'value': [{
                    'id': '1',
                    'list': [
                        { 'name': 'test', 'mapp_id': 1 },
                        { 'name': 'test1', 'mapp_id': 1 },
                        { 'name': 'test2', 'mapp_id': 1 },
                        { 'name': 'test3', 'mapp_id': 2 }
                    ]
                }, {
                    'id': '2',
                    'list': [
                        { 'name': 'test4', 'mapp_id': 2 },
                        { 'name': 'test5', 'mapp_id': 2 },
                        { 'name': 'test6', 'mapp_id': 2 },
                        { 'name': 'test7', 'mapp_id': 1 }
                    ]
                }

                ]
            },{
                'value': [{
                    'id': '3',
                    'list': [
                        { 'name': 'test8', 'mapp_id': 3 },
                        { 'name': 'test9', 'mapp_id': 3 },
                        { 'name': 'test10', 'mapp_id': 1 },
                        { 'name': 'test11', 'mapp_id': 1 }
                    ]
                }, {
                    'id': '4',
                    'list': [
                        { 'name': 'test12', 'mapp_id': 1 },
                        { 'name': 'test13', 'mapp_id': 1 },
                        { 'name': 'test14', 'mapp_id': 2 },
                        { 'name': 'test14', 'mapp_id': 2 }
                    ]
                }

                ]
            }]

i want to data like way and some code try but no fully success some success.

    var output = {
                '1': [
                          { 'name': 'test', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test1', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test2', 'mapp_id': 1 ,'id': '1'},
                          { 'name': 'test7', 'mapp_id': 1 ,'id': '2'},
                          { 'name': 'test10', 'mapp_id': 1, 'id': '3' },
                          { 'name': 'test11', 'mapp_id': 1, 'id': '3' },
                          { 'name': 'test12', 'mapp_id': 1,'id': '4' },
                          { 'name': 'test13', 'mapp_id': 1 ,'id': '4'}
                     ],
                '2': [
                          { 'name': 'test3', 'mapp_id': 2 ,'id': '1'}, 
                          { 'name': 'test4', 'mapp_id': 2,'id': '2' },
                          { 'name': 'test5', 'mapp_id': 2 ,'id': '2'},
                          { 'name': 'test6', 'mapp_id': 2,'id': '2' }, 
                          { 'name': 'test14', 'mapp_id': 2 ,'id': '4'},
                          { 'name': 'test14', 'mapp_id': 2 ,'id': '4'}
                ],
                '3':[ 
                          { 'name': 'test8', 'mapp_id': 3 ,'id': '4'},
                          { 'name': 'test9', 'mapp_id': 3 ,'id': '4'}
                    ]

            };

lodash library used in her documentation refer https://lodash./docs/4.17.10

i can try some code and that code post blow but no fully success.

var result= _.flatMap(data, item => 
        _(item.value)
        .flatMap('list')
        .value()
        );
result=_.groupBy(result, function(b) { return b.mapp_id})

and my code out blow posted

output = {
            '1': [
                      { 'name': 'test', 'mapp_id': 1 },
                      { 'name': 'test1', 'mapp_id': 1 },
                      { 'name': 'test2', 'mapp_id': 1 },
                      { 'name': 'test7', 'mapp_id': 1 },
                      { 'name': 'test10', 'mapp_id': 1 },
                      { 'name': 'test11', 'mapp_id': 1 },
                      { 'name': 'test12', 'mapp_id': 1 },
                      { 'name': 'test13', 'mapp_id': 1 }
                 ],
            '2': [
                      { 'name': 'test3', 'mapp_id': 2 }, 
                      { 'name': 'test4', 'mapp_id': 2 },
                      { 'name': 'test5', 'mapp_id': 2 },
                      { 'name': 'test6', 'mapp_id': 2 }, 
                      { 'name': 'test14', 'mapp_id': 2 },
                      { 'name': 'test14', 'mapp_id': 2 }
            ],
            '3':[ 
                      { 'name': 'test8', 'mapp_id': 3 },
                      { 'name': 'test9', 'mapp_id': 3 }
                ]

        };

help me guys how i can do this?

Share Improve this question edited Jun 8, 2018 at 13:58 Shubham Khatri 282k58 gold badges431 silver badges411 bronze badges asked Jun 8, 2018 at 13:34 Jimit H.Jimit H. 1951 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You could chain flatMap for nested properties and then group the flat data.

var data = [{ value: [{ id: '1', list: [{ name: 'test', mapp_id: 1 }, { name: 'test1', mapp_id: 1 }, { name: 'test2', mapp_id: 1 }, { name: 'test3', mapp_id: 2 }] }, { id: '2', list: [{ 'name': 'test4', mapp_id: 2 }, { 'name': 'test5', mapp_id: 2 }, { 'name': 'test6', mapp_id: 2 }, { 'name': 'test7', mapp_id: 1 }] }] }, { value: [{ id: '3', list: [{ name: 'test8', mapp_id: 3 }, { name: 'test9', mapp_id: 3 }, { name: 'test10', mapp_id: 1 }, { name: 'test11', mapp_id: 1 }] }, { id: '4', list: [{ name: 'test12', mapp_id: 1 }, { name: 'test13', mapp_id: 1 }, { name: 'test14', mapp_id: 2 }, { name: 'test14', mapp_id: 2 }] }] }],
    result = _(data)
        .flatMap('value')
        .flatMap('list')
        .groupBy('mapp_id')
        .value();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

You can implement the flatMap function to return the id with each element like

var result= _.flatMap(data, item => {
       return  _(item.value)
        .flatMap(function(value){
            return value.list.map(obj => ({...obj, id: value.id}));
         }).value()
        }
);

var data = [{
                'value': [{
                    'id': '1',
                    'list': [
                        { 'name': 'test', 'mapp_id': 1 },
                        { 'name': 'test1', 'mapp_id': 1 },
                        { 'name': 'test2', 'mapp_id': 1 },
                        { 'name': 'test3', 'mapp_id': 2 }
                    ]
                }, {
                    'id': '2',
                    'list': [
                        { 'name': 'test4', 'mapp_id': 2 },
                        { 'name': 'test5', 'mapp_id': 2 },
                        { 'name': 'test6', 'mapp_id': 2 },
                        { 'name': 'test7', 'mapp_id': 1 }
                    ]
                }

                ]
            },{
                'value': [{
                    'id': '3',
                    'list': [
                        { 'name': 'test8', 'mapp_id': 3 },
                        { 'name': 'test9', 'mapp_id': 3 },
                        { 'name': 'test10', 'mapp_id': 1 },
                        { 'name': 'test11', 'mapp_id': 1 }
                    ]
                }, {
                    'id': '4',
                    'list': [
                        { 'name': 'test12', 'mapp_id': 1 },
                        { 'name': 'test13', 'mapp_id': 1 },
                        { 'name': 'test14', 'mapp_id': 2 },
                        { 'name': 'test14', 'mapp_id': 2 }
                    ]
                }

                ]
            }]
var result= _.flatMap(data, item => {
       return  _(item.value)
        .flatMap(function(value){
            return value.list.map(obj => ({...obj, id: value.id}));
         }).value()
        }
);
result=_.groupBy(result, function(b) { return b.mapp_id})
console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744202445a235863.html

最新回复(0)