get records doing a match between arrays rethinkdb - javascript

I want get the id from table records that have the array match with other record from the same table's array example:
it is record of user '1'
✔ r.db('fotogena').table('users').filter({user:'1'}).pluck('pleasures')
{
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af5-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1afb-a7b1-11e7-86dc-d709d79803e4",
"432f1afc-a7b1-11e7-86dc-d709d79803e4",
"432f1afd-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b03-a7b1-11e7-86dc-d709d79803e4",
"432f1b04-a7b1-11e7-86dc-d709d79803e4",
"432f1b07-a7b1-11e7-86dc-d709d79803e4"
]
}
]
}
and i want compare each one that items(without the user '1') inside that pleasure.subCategory with others records inside the same table with the same estructure.
Table with 3 records
[
{
"date": "2017-10-03T03:58:02.651Z",
"id": "d82279a7-fbc6-40a2-99ca-39796ea57efa",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af5-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1afb-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b03-a7b1-11e7-86dc-d709d79803e4",
]
}
],
"user": "1"
},
{
"date": "2017-10-07T02:59:45.942Z",
"id": "174c0e35-da79-4ca8-b237-8ec569cc27b1",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e4",
"432f1af4-a7b1-11e7-86dc-d709d79803e4"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1aff-a7b1-11e7-86dc-d709d79803e4",
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1b04-a7b1-11e7-86dc-d709d79803e4",
]
}
],
"user": "10"
},
{
"date": "2017-10-07T02:07:13.715Z",
"id": "dd11edac-e0f5-43ac-811a-eaa78a6509c7",
"pleasures": [
{
"category": "432f1ae0-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1ae1-a7b1-11e7-86dc-d709d79803e5"
]
},
{
"category": "432f1aef-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e5"
]
},
{
"category": "432f1afa-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e6"
]
},
{
"category": "432f1b02-a7b1-11e7-86dc-d709d79803e4",
"subCategory": [
"432f1af3-a7b1-11e7-86dc-d709d79803e7"
]
}
],
"user": "25"
}
]
i think that maybe can be it:
r.db('fotogena').table('users').filter({user:'1'}).pluck('pleasures').map(pleasures=>{
return //do something
})

i could solve it, later of search and search, i could created a script that show that i want get
I had that do in unique query(no joins) compare arrays with arrays using .contains() and worked very good
r.db('fotogena').table('users').filter(users=>{
return users('pleasures').contains(category=>{
return category('subCategory').contains(subCategory=>{
return r.db('fotogena').table('users').filter({user: '1'}).pluck('pleasures').contains(pleasures2=>{
return pleasures2('pleasures').contains(subCate2=>{
return subCate2('subCategory')
.contains(subCategory2=>{
return subCategory2.eq(subCategory)
})
})
})
})
}).and(users('user').ne('1'))
})

Related

javascript print nested array to screen

I have a data array, there are multiple objects in this array, and the data object is an array. There may be more than one object in this array. How can I copy these objects under a single array?
const test = []
const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
]
[{
"price":10,
"title": "sda"
},
{
"price":20,
"title": "asdas"
},
]
If this is the output I expect, it should be like this. how can I do that
const data = [
{
"_id": "124141",
"name": "test",
"data": [
{
"price":10,
"title": "sda"
},
{
"price":99,
"title":"aaaaa"
}
]
},
{
"_id": "2525",
"name": "test2",
"data": [
{
"price":20,
"title": "asdas"
},
]
}
];
console.log(data.map(e => e.data).flat());
// OR
console.log(data.flatMap(e => e.data));

How can i group and restructure the JSON object in Angular8

I am getting below response, I want to ReGroup all the data and its children based on buSubRegion which is present inside children now. And also needs to add buSubRegion outside children.
For EX - In below Response, 1UL Africa belongs to Africa object and also present inside Europe object, so i need to merge children of all objects which belongs to 1UL Africa into one array of object.
I want if buSubRegion have a same value and it is present inside many objects then find all those objects and merge into one.
Also, Agbara - Savoury dont have a BU sub-region So i don't want to include this object.
Can anyone please help me to achieve this.
const data = [
{
"name": "Africa",
"id":1,
"children": [
{
"name": "Agbara - Laundry",
"buSubRegion": "1UL Africa",
"children": [
{
'lineId':"R_D005_TPKDST02"
}
]
},
{
"name": "Agbara - Savoury",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
},
{
"name": "Europe",
"id":2,
"children": [
{
"name": "Europe1",
"buSubRegion": "1UL Africa",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
},
{
"name": "Europe2",
"buSubRegion": "Test Europe",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
},
{
"name": "Latem",
"id":3,
"children": [
{
"name": "test1",
"buSubRegion": "latem1",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
}
];
Below is my Expected Output
[
{
"buSubRegion": "1UL Africa",
"name": "Africa",
"id":1,
"children": [
{
"name": "Agbara - Laundry",
"buSubRegion": "1UL Africa",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
},
{
"name": "Europe1",
"buSubRegion": "1UL Africa",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
},
{
"buSubRegion": "Test Europe",
"name": "Europe",
"id":2,
"children": [
{
"name": "Europe2",
"buSubRegion": "Test Europe",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
},
{
"name": "Latem",
"buSubRegion": "latem1",
"id":3,
"children": [
{
"name": "test1",
"buSubRegion": "latem1",
"children": [
{
"lineId":"R_D005_TPKDST02"
}
]
}
]
}
];
You can make use of reduce and inside that loop through children property to group the data by buSubRegion.
const data = [{"name":"Africa","id":1,"children":[{"name":"Agbara - Laundry","buSubRegion":"1UL Africa","children":[{"lineId":"R_D005_TPKDST02"}]},{"name":"Agbara - Savoury","children":[{"lineId":"R_D005_TPKDST02"}]}]},{"name":"Europe","id":2,"children":[{"name":"Europe1","buSubRegion":"1UL Africa","children":[{"lineId":"R_D005_TPKDST02"}]},{"name":"Europe2","buSubRegion":"Test Europe","children":[{"lineId":"R_D005_TPKDST02"}]}]},{"name":"Latem","id":3,"children":[{"name":"test1","buSubRegion":"latem1","children":[{"lineId":"R_D005_TPKDST02"}]}]}];
const result = data.reduce((a,{children, ...rest})=>{
children.forEach(({buSubRegion,...others})=>{
if(buSubRegion){
a[buSubRegion] ??= {buSubRegion, ...rest, children:[]};
a[buSubRegion].children.push({buSubRegion, ...others})
}
});
return a;
},{});
console.log(Object.values(result));

how to sort array of object in js by property i.ecategoryname' in my dataset in alphabetical order using mergesort

Here is the list of data that should be sorted in alphabetical based of categoryName
value using merger sort
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows":
{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
i can use filter and includes method to sort.. but i have to implement some types of algorithm in my project like{mergersort ,bubblesort,depthsearch etc}but for now i need to use mergesort***
The final result should be like this i have not included all the property inside the object here i think you got it ..
filteritem=[
{categoryName:"admission"...},
{cateogryName:"Binodkhatricv"..},
{categoryName:"officialdocument"...},
{categoryName:"syallabus"...}
]```
You can use sort I guess it's do want to try to do :
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows":
[{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
}];
let sorted = filteritem.sort((a, b) => (a < b ? -1 : 1))
console.log(sorted)
You need to use localeCompare to sort the string, but after toLowerCase to ignore case sensitivity.
const filteritem = [
{
"categoryName": "admission",
"rows": [
{
"uploaddate": "8/8/2022",
"title": "binoddocs",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"fullname": "bruno",
"id": "B8QsXYVFH8fHt3PrBYUp"
}
]
},
{
"categoryName": "officialdocument",
"rows": [
{
"file": null,
"uploadedby": "bruno",
"uploaddate": "6/27/2022",
"title": "sudhikchaadmission",
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"id": "Z27GLizWnYTJvLQyYRQt"
},
{
"view": "https://firebasestorage.googleapis.com/v0/b/auth",
"uploadedby":"bruno",
"uploaddate":"6/27/2022",
"title":"ankitadmission",
"file":null,
"id":"rmcbUrg9TpFhQh5RLqva"
}
]
},
{
"categoryName":"syallabus",
"rows": [
{
"fullname":"bruno",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"title":"sudhir",
"uploaddate":"8/15/2022",
"id":"hi7QEOlBzzVLZ1QHYqlk"
}
]
},
{
"categoryName":"Binodkhatricv",
"rows": [
{
"title":"binodtry",
"fullname":"bruno",
"uploaddate":"8/15/2022",
"view":"https://firebasestorage.googleapis.com/v0/b/auth",
"id":"o4EtP1xkbWMk1icp4uNH"
}
]
}
]
const result = filteritem.sort((a, b) => a.categoryName.toLowerCase().localeCompare(b.categoryName.toLowerCase()))
console.log(result)

How to add name in JSONPATH

I have an array like this
{
"activities": [
{
"activityId": "60f672b20f38640008a6c29b",
"_id": "610cb887b149cb00095e1eb6",
"trackingProgress": [
{
"_id": "610cb8ee541fea0009189c36",
"dateRecorded": "2021-07-16T00:00:00.000Z",
"items": [
{
"_id": "610cb8ee541fea0009189c37",
"feelingType": "Satisfied",
"times": 2
}
]
},
{
"_id": "611358300fea490008d56ea7",
"dateRecorded": "2021-08-10T00:00:00.000Z",
"items": [
{
"_id": "6113583b0fea490008d56eae",
"feelingType": "Neutral",
"times": 1
}
]
},
{
"_id": "611b6068fed9100009a4f689",
"dateRecorded": "2021-08-17T00:00:00.000Z",
"items": [
{
"_id": "611b6068fed9100009a4f68a",
"feelingType": "Satisfied",
"times": 4
}
]
}
],
"title": "Losing & Managing Weight",
"description": "Losing weight",
"order": null,
"daysOfWeek": [
"tuesday"
],
"reminderTimeAt": "09:00 am",
"type": "Custom",
"progressOverview": {
"notSatisfied": 0,
"neutral": 1,
"satisfied": 6
}
}
]
}
I use JSONPATH to get _id in items
$.activities.[*].trackingProgress.[*].items[*].[_id]
it return an array like this
[
"610cb8ee541fea0009189c37",
"6113583b0fea490008d56eae",
"611b6068fed9100009a4f68a"
]
But, i want to keep the "_id", something like :
[
"_id":"610cb8ee541fea0009189c37",
"_id":"6113583b0fea490008d56eae",
"_id":"611b6068fed9100009a4f68a"
]
How can i do that?? Thank you a lots, i search internet but i found nothing
If you use map, you can get an array of items that each have an _id and value.
let result = [
"610cb8ee541fea0009189c37",
"6113583b0fea490008d56eae",
"611b6068fed9100009a4f68a"
].map(i => ({"_id": i}))
Results in:
[{ _id: "610cb8ee541fea0009189c37" },
{ _id: "6113583b0fea490008d56eae" },
{ _id: "611b6068fed9100009a4f68a" }]

How to groupBy in multi arrays using lodash

I want to group By sub-work in array
Here is my array I want to group By sub-work
result = [
{
"date": "10-07-2019",
"data": [
{
"data_id": "20",
"work": "work_1",
"sub-work": "sub_work1",
"sub-data": [
{
"id": 7,
"title": 'subdata-1',
}
]
}
]
},
{
"date": "12-07-2019",
"data": [
{
"data_id": "20",
"work": "work_1",
"sub-work": "sub_work1",
"sub-data": [
{
"id": 7,
"title": 'subdata-1',
}
]
}
]
},
]
Here is what I try
result = _(result)
.map(function(items, data) {
_.groupBy(items.data, function({ sub_work }) {
return sub_work;
});
})
.value();
first I map result into data then I try to groupby but It's return null
Update
I want my output look like this
[
{
"date": "10-07-2019",
sub-work: [{
sub-work : "sub_work1",
sub-data[
{
"id": 7,
"title": 'subdata-1',
}
]
}]
}
]
...........................
It would be better if you could provide your expected result.
Here's what I could infer:
_(result)
.map(function(obj) { // Convert into a 2D array.
return obj.data.map(e => Object.assign(e, {date: obj.date}));
})
.flatten() // We have an array of objects now.
.groupBy('sub-work') // We can call groupBy().
.value();
Here's what you get:
{
"sub_work1": [{
"data_id": "20",
"work": "work_1",
"sub-work": "sub_work1",
"sub-data": [{
"id": 7,
"title": "subdata-1"
}],
"date": "10-07-2019"
}, {
"data_id": "20",
"work": "work_1",
"sub-work": "sub_work1",
"sub-data": [{
"id": 7,
"title": "subdata-1"
}],
"date": "12-07-2019"
}]
}

Categories

Resources