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?
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>