我有数组对象,在这个数组对象中我想要过滤数据、分组数据(特别是键)和数组对象。
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 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库引用https://lodash.com/docs/4.17.10
我可以尝试一些代码,但该代码发布失败,但没有完全成功。
var result= _.flatMap(data, item =>
_(item.value)
.flatMap('list')
.value()
);
result=_.groupBy(result, function(b) { return b.mapp_id})
我的代码被发布了
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 }
]
};
请帮帮我,我该怎么做?
最佳答案
您可以链接flatMap
以获取嵌套属性,然后对平面数据进行分组。
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.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
关于javascript - 如何使用 flatmap lodash 获取 ArrayObject 所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50761839/