如何解决使用lodash等效的Mongodb $ unwind
我有以下对象:
data =[
{
"id": "AQ103_2020-09-03T06:00:00.000Z","date": "2020-09-03T06:00:00.000Z","station": {
"name": "AQ103","loc": {
"type": "Point","coordinates": [
45,12.45
]
}
},"samples": [
{
"t": "2020-09-03T06:10:57.617Z","data": {
"PM1": 100,"PM10": 800,"O3": 300
}
},{
"t": "2020-09-03T06:12:00.000Z","O3": 300.25,"NO2": 100.45
}
}
]
},{
"id": "AQ103_2020-09-03T10:00:00.000Z","date": "2020-09-03T07:00:00.000Z","samples": [
{
"t": "2020-09-03T07:10:57.617Z","PM10": 800
}
}
]
},{
"id": "AQ103_2020-09-03T09:00:00.000Z","date": "2020-09-03T09:00:00.000Z","samples": [
{
"t": "2020-09-03T09:46:55.256Z","data": {
"PM1": 689.325,"CO": 47.849
}
},{
"t": "2020-09-03T09:52:22.233Z","data": {
"PM1": 200,"PM10": 200
}
}
]
},"station": {
"name": "AQ101","samples": [
{
"t": "2020-09-03T09:55:32.124Z","PM10": 800
}
}
]
}
]
我想实现在MongoDB中使用$ unwind进行实现,而在javascript中使用 lodash 进行实现。 到目前为止,我能够使用flatMap整理数据为:
const output = _(data).flatMap('samples').value()
console.log(output)
[
{
t: '2020-09-03T06:10:57.617Z',data: { PM1: 100,PM10: 800,O3: 300 }
},{
t: '2020-09-03T06:12:00.000Z',O3: 300.25,NO2: 100.45 }
},{ t: '2020-09-03T07:10:57.617Z',PM10: 800 } },{ t: '2020-09-03T09:46:55.256Z',data: { PM1: 689.325,CO: 47.849 } },{ t: '2020-09-03T09:52:22.233Z',data: { PM1: 200,PM10: 200 } },{ t: '2020-09-03T09:55:32.124Z',PM10: 800 } }
]
我想向结果数组中的每个对象添加一个名为station_name的成员,该成员类似于data.station.name中的原始成员。
有什么建议吗?
谢谢。
解决方法
data =[
{
"id": "AQ103_2020-09-03T06:00:00.000Z","date": "2020-09-03T06:00:00.000Z","station": {
"name": "AQ103","loc": {
"type": "Point","coordinates": [
45,12.45
]
}
},"samples": [
{
"t": "2020-09-03T06:10:57.617Z","data": {
"PM1": 100,"PM10": 800,"O3": 300
}
},{
"t": "2020-09-03T06:12:00.000Z","O3": 300.25,"NO2": 100.45
}
}
]
},{
"id": "AQ103_2020-09-03T10:00:00.000Z","date": "2020-09-03T07:00:00.000Z","samples": [
{
"t": "2020-09-03T07:10:57.617Z","PM10": 800
}
}
]
},{
"id": "AQ103_2020-09-03T09:00:00.000Z","date": "2020-09-03T09:00:00.000Z","samples": [
{
"t": "2020-09-03T09:46:55.256Z","data": {
"PM1": 689.325,"CO": 47.849
}
},{
"t": "2020-09-03T09:52:22.233Z","data": {
"PM1": 200,"PM10": 200
}
}
]
},"station": {
"name": "AQ101","samples": [
{
"t": "2020-09-03T09:55:32.124Z","PM10": 800
}
}
]
}
];
let unwindedData = data.map(doc => doc.samples.map(sample => ({
...doc,samples: sample
}))).flat();
console.log(unwindedData);
这很好地模拟了$ unwind方法,但不需要lodash。
将每个文档映射到单个示例对象的数组,然后展平。
data.map(doc => doc.samples.map(sample => ({
...doc,samples: sample
}))).flat()
注意:在SO上运行的代码段在日志输出中显示为站的参考注释,但实际输出正确。
,感谢GitGitBoom, 通过进一步阐述您的建议,我得到了以下代码:
let values = obj.map((item)=>{
// if items key is not found do not pass it to the new array
if(item.Object.key != X){
return false
}
return item
})
这恰好提供了我想要的东西:
const obj = {
APRIL: 35,COMPANY: 'Walmart',FEBUARY: 34,FEBUARY_1: 9,HEADQUARTERS: 'Bentonville,AR',JANUARY: 22.6,MARCH: 23.4,MAY: 21.1,TOTAL: 145.1,};
const notInclude = ['COMPANY','HEADQUARTERS','TOTAL'];
let labelValues = () => {
return Object.keys(obj).filter((val) => !notInclude.includes(val));
};
const data = labelValues();
const result = data.map((val) => obj[val]);
console.log(data);
console.log(result);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。