Javascript 如何使用flatmap lodash获取ArrayObject所有值
我有一个数组对象,在这个数组对象中我需要过滤数据,按特定键和数组对象中的数据分组Javascript 如何使用flatmap lodash获取ArrayObject所有值,javascript,arrays,multidimensional-array,lodash,Javascript,Arrays,Multidimensional Array,Lodash,我有一个数组对象,在这个数组对象中我需要过滤数据,按特定键和数组对象中的数据分组 var data = [{ 'value': [{ 'id': '1', 'list': [ { 'name': 'test', 'mapp_id': 1 }, { 'name': 'test1', '
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库参考
我可以尝试一些代码,但没有完全成功
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_:2},{'id:7},{test1}id:[{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',
结果=(数据)
.flatMap('值')
.flatMap('列表')
.groupBy('mapp_id'))
.value();
console.log(结果);
。作为控制台包装{最大高度:100%!重要;顶部:0;}
您可以为嵌套属性链接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_:2},{'id:7},{test1}id:[{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',
结果=(数据)
.flatMap('值')
.flatMap('列表')
.groupBy('mapp_id'))
.value();
console.log(结果);
。作为控制台包装{最大高度:100%!重要;顶部:0;}
您可以实现flatMap函数来返回每个元素的id,如
var result= _.flatMap(data, item => {
return _(item.value)
.flatMap(function(value){
return value.list.map(obj => ({...obj, id: value.id}));
}).value()
}
);
var数据=[{
“价值”:[{
“id”:“1”,
“列表”:[
{'name':'test','mapp_id':1},
{'name':'test1','mapp_id':1},
{'name':'test2','mapp_id':1},
{'name':'test3','mapp_id':2}
]
}, {
“id”:“2”,
“列表”:[
{'name':'test4','mapp_id':2},
{'name':'test5','mapp_id':2},
{'name':'test6','mapp_id':2},
{'name':'test7','mapp_id':1}
]
}
]
},{
“价值”:[{
“id”:“3”,
“列表”:[
{'name':'test8','mapp_id':3},
{'name':'test9','mapp_id':3},
{'name':'test10','mapp_id':1},
{'name':'test11','mapp_id':1}
]
}, {
“id”:“4”,
“列表”:[
{'name':'test12','mapp_id':1},
{'name':'test13','mapp_id':1},
{'name':'test14','mapp_id':2},
{'name':'test14','mapp_id':2}
]
}
]
}]
var结果=u2;.flatMap(数据,项=>{
返回值(项目值)
.flatMap(函数(值){
返回value.list.map(obj=>({…obj,id:value.id}));
}).value()
}
);
result=\ uu.groupBy(结果,函数(b){return b.mapp\ u id})
console.log(结果);
您可以实现flatMap函数来返回每个元素的id,如
var result= _.flatMap(data, item => {
return _(item.value)
.flatMap(function(value){
return value.list.map(obj => ({...obj, id: value.id}));
}).value()
}
);
var数据=[{
“价值”:[{
“id”:“1”,
“列表”:[
{'name':'test','mapp_id':1},
{'name':'test1','mapp_id':1},
{'name':'test2','mapp_id':1},
{'name':'test3','mapp_id':2}
]
}, {
“id”:“2”,
“列表”:[
{'name':'test4','mapp_id':2},
{'name':'test5','mapp_id':2},
{'name':'test6','mapp_id':2},
{'name':'test7','mapp_id':1}
]
}
]
},{
“价值”:[{
“id”:“3”,
“列表”:[
{'name':'test8','mapp_id':3},
{'name':'test9','mapp_id':3},
{'name':'test10','mapp_id':1},
{'name':'test11','mapp_id':1}
]
}, {
“id”:“4”,
“列表”:[
{'name':'test12','mapp_id':1},
{'name':'test13','mapp_id':1},
{'name':'test14','mapp_id':2},
{'name':'test14','mapp_id':2}
]
}
]
}]
var结果=u2;.flatMap(数据,项=>{
返回值(项目值)
.flatMap(函数(值){
重新