Find parent where child in array - javascript

I've got two tables,
Places
Products
What I am trying to achieve is to find Place By multiple products.
So basically.
find PLACE where product ids in [1,4,6]
The thing is that it looks for every place where product id = 1 OR id = 4. I want to find a place that contains all the products. So 3 conditionals must be achieved.
This is how it looks in sequelize
const eq = await Place.findAndCountAll({
include: [
{
model: PlaceProduct,
as: 'products',
where: {
productId: [1,2,5]
}
},
],
}
And it returns places which contain only of the required products.
I want all to be required.
Thanks!

You can check the count of records are exact of product_ids , if yes then thats the record you want , and if is less than that it will be ignored
const product_ids = [1, 2, 5];
const eq = await Place.findAll({
include: [{
model: PlaceProduct,
attributes : [],
as: 'products',
where: {
productId: product_ids // <----- product_ids
}
}, ],
group: ['place.id'], // <--- Not sure but , you might need to add place_product.id also here
having: {
$and: [
db.sequelize.where(db.sequelize.fn('count', db.sequelize.col('place.id')), product_ids.length), // <----- product_ids
]
}
}

Related

find property inside nested array javascript

I have an object that contains data to display information pulled from the Notion API. I can see the data but not sure how I can extract nested array inside the current array. My goal is to use the category property to create a filter but first I need to get the string to create the condition.
Here is what the data looks like currently. How would I go about to filter out name: "commissions":
resultsArray:
0:
properties
category:
id: "sdasd"
multi_select:
0:
id:"324234"
name: "commissions"
I have tried use find but it doesn't do what I expect. My suspicion is that I will have to loop over the nested array again.
You can use find inside find condition
like this :
data.resultsArray.find(item=>item.category.multi_select.find(select=> select.name === "commissions"))
const data = {
resultsArray: [
{
category: {
id: 'sdasd',
multi_select: [
{
id: '324234',
name: 'commissions',
},
],
},
},
],
};
const result = data.resultsArray.find(item=>item.category.multi_select.find(select=> select.name === "commissions"))
console.log(result)

How can loop through array of JSON Object to find property value that includes user input characters

What is the correct method JavaScript function I can use to loop through an array of JSON data and return the JSON.KEY value that includes the user input query
Suppose that we have an array books with these data
const books = [
{ title: 'chemistry', pages: 123 },
{ title: 'chemical abcd', pages: 103 },
{ title: 'anatomy of something ', pages: 423 }
];
When a user query is
let query= 'chemi'
Then the output should be
filteredBooks = [
{ title: 'chemistry', pages: 123 },
{ title: 'chemical abcd', pages: 103 }
];
For this I would use the js Array.filter method:
const filteredBooks = books.filter(book => book.title.includes(query))
In addition of the other questions, using destructuring saves a bit of code:
const filteredBooks = books.filter(({title}) => title.includes(query));
this method searches all the values of the object if they contain the query. you should make your query lowercase first:
query=query.toLowerCase();
filteredBooks = books.filter(book=>Object.values(book).some(value=>value.toString().toLowerCase().includes(query)));
if the book object has sub-fields, this is a lazy way to query them:
query=query.toLowerCase();
filteredBooks = books.filter(book=>JSON.stringify(Object.values(book)).toLowerCase().includes(query))

Merge Data from different Queries without duplicates

I am getting data from three different queries via Api. I want data to be merged without the duplicate data.
This is my function where i am merging the data:
getStaffCount(data) {
if (data == null || data.results === null )
return [];
else
return data.results.StaffCount.map(m => ({ Name: m.Name, Accounts: m.Accounts })).
concat(data.results.RepProviderAccount.map(m => ({ Name: m.Name, Accnt: m.Accnt }))).
concat( data.results.ProviderAccount.map(m => ({ Name: m.Name, Account: m.Account })));
}
This is my table:
<PowerTable Data={{ rows: this.getStaffCount(this.props.GridData) }} rowsPerPage={5} orderBy="Name" order="asc" >
<PowerColumn id='Name' columnName='Name' numeric={false} disablePadding={false} label='Profile Name' width={100}>
</PowerColumn>
<PowerColumn id='Accounts' columnName='Accounts' numeric={false} disablePadding={false} label='Staff Accounts' width={100}>
</PowerColumn>
<PowerColumn id='Account' columnName='Account' numeric={false} disablePadding={false} label='Provider Account' width={100} >
</PowerColumn>
<PowerColumn id='Accnt' columnName='Accnt' numeric={false} disablePadding={false} label='Rep Provider Account' width={100} >
</PowerColumn>
</PowerTable>
So in the above image same Profile Name(CNX MSL Platform) is coming twice. So is there any way i can merged those rows?
I want the Output in this way:
Profile Name Staff Provider Rep Provider
Cnx MSl Platform 2 1
Cnx Specilaity sales Platform 7 22
Data:
As an object
if the data is an object the easy way to do that is the spread opperator
const combinedData = {
...dataSrc1,
...dataSrc2,
...dataSrc3,
}
All matching keys will be overwritten by the previous
As an array
It's a bit more complex. Assuming your object has a unique id (or any value to identify 2 as the same item) you can use a Set since they can only have unique values.
const array = [
...dataSrc1,
...dataSrc2,
...dataSrc3,
]
const unique = [...new Set(array.map(item => item.id))];
Your answer to my question about what the data looks like and how to group them didn't make any sense, neither did you answer Joe just showed the json data and tell him where the data comes from instead of what it is.
So I assume you group by Name and Account is ignored. You can group them in the following way:
const data = {
results: {
StaffCount: [
{
Name: 'a',
Accounts: 2,
},
{
Name: 'b',
Accounts: 20,
},
],
RepProviderAccount: [
{
Name: 'a',
Accnt: 3,
},
],
ProviderAccount: [
{
Name: 'a',
Account: 1,
},
],
},
};
const grouped = [
...data.results.StaffCount,
...data.results.RepProviderAccount,
...data.results.ProviderAccount,
].reduce((result, item) => {
const {
Name,
Account = 0,
Accounts = 0,
Accnt = 0,
} = item;
const existing = result.get(item.Name) || {
Name,
Account: 0,
Accounts: 0,
Accnt: 0,
};
existing.Account += Account;
existing.Accounts += Accounts;
existing.Accnt += Accnt;
return result.set(Name, existing);
}, new Map());
console.log([...grouped.values()]);
In case this doesn't work for you can you please update your question and provide code as in my answer with the expected input and output? You can respond to this answer and I'll have a look at your question again.
This may actually be an xy problem, you are fetching 3 data sources and then trying to group and sum them but maybe you can just get 1 data source and try salesforce to group and sum them in the query. I don't know enough about salesforce but maybe you can ask another question tagging it with soql if it's possible to just get the data grouped and summed.

Node.js code only working at top of file with sequelize

When at the top of my server-side code, this works fine and the results produced are correct:
var data_playlists = {};
models.Playlist.findAll({
attributes: ['id', 'name']
}).then(function (playlists){
data_playlists['playlists'] = playlists.map(function(playlist){
return playlist.get({plain: true})
});
addsongs(data_playlists, 1);
addsongs(data_playlists, 2);
addsongs(data_playlists, 3);
});
but when it's inside one of my Express methods, it isn't functioning properly; particularly, the addsongs method is not working as it should.
function addsongs(playlist_object, id_entered){
var arraysongs = [];
models.Playlist.findOne({
attributes: ['id'],
where: {
id: id_entered
}
})
.then(function(playlist) {
playlist.getSongs().then(function (thesongs){
for(var k = 0; k < thesongs.length ; k++){
arraysongs.push(thesongs[k].Songs_Playlists.SongId);
}
playlist_object.playlists[(id_entered - 1)]['songs'] = arraysongs;
});
});
}
I cannot for the life of me figure out why it works when the top segment of code is at the top, but doesn't work when inside my app.get() call.
From your code I have conducted that you want to return playlists (id and name) together with their songs (id). First of all your code will not work because the calls of addsongs(data_playlists, id) are run before data_playlists is filled with data by code above it. Moreover, the addsongs function performs asynchronous operations returning Promises, so calling them one by one will not give expected result. I suppose you can do it completely differently.
I suggest you use include attribute of options object that can be passed to findAll() method. include says which association model you also want to return from current query. In this case you want to return playlists together with their songs (M:M relation according to your code), so you need to include Song model in the query.
function getPlaylistsWithSongs() {
return models.Playlist.findAll({
attributes: ['id', 'name'],
include: [
{
model: models.Song,
as: 'Songs', // depends on how you have declare the association between songs and playlists
attributes: ['id'],
through: { attributes: [] } // prevents returning fields from join table
}
]
}).then((playlistsWithSongs) => {
return playlistsWithSongs;
});
}
Example result of getPlaylistsWithSongs result would be (after translating it to JSON e.g. like playlistsWithSongs.toJSON())
[
{
id: 1,
name: 'playlist #1',
Songs: [
{ id: 1 },
{ id: 2 }
]
}
]
Above code returns all playlists (their id and name) with their songs (only their id). Now in your route resolver you can simply call above function to return the result
app.get('/api/playlists', function (request, response) {
response.setHeader("Content-Type", "application/json; charset=UTF-8");
getPlaylistsWithSongs().then(function(playlistsWithSongs){
response.status(200).send(JSON.stringify(playlistsWithSongs));
});
});
EDIT
In order to simply return array of IDs instead array of objects with id (songs), you need to map the result. There is no simple sequelize way to return array of IDs in such a case.
}).then((playlistWithSongs) => {
let jsonPlaylists = playlistsWithSongs.map((singlePlaylist) => {
// return JSON representation of each playlist record
return singlePlaylist.toJSON();
});
jsonPlaylists.forEach((playlist) => {
// at every playlist record we map Songs to array of primitive numbers representing it's IDs
playlist.songs = playlist.Songs.map((song) => {
return song.id;
});
// when we finish we can delete the Songs property because now we have songs instead
delete playlist.Songs;
});
console.log(jsonPlaylists);
// example output: [{ id: 1, name: 'playlist #1', songs: [1, 2, 3] }]
return jsonPlaylists;
});

Select documents of a collection depending on user roles

I need to get all documents of a collection which has a specific value for a field (section) - which depends on a user role - or an userId for the field user.
So I'm trying to check if the user is in a role and put that into the query:
var sectionOne = Roles.userIsInRole(Meteor.userId(), ['something']) ? { section: 'cars' } : undefined;
var sectionSecond = Roles.userIsInRole(Meteor.userId(), ['anything']) ? { section: 'vegetables' } : undefined;
var sectionThird = Roles.userIsInRole(Meteor.userId(), ['else']) ? { section: 'countries' } : undefined;
var count = Collection.find(
{
$or: [
sectionOne,
sectionSecond,
sectionThird,
{ user: userId },
],
read: {
$nin: [ userId ]
}
}
).count();
console.log(count);
What would be the correct way to do that?
For better understanding here an example of what I'm trying to do:
Example
If the user has the roles something and else, then the query should look like this:
var count = Collection.find(
{
$or: [
{ section: 'cars' },
{ section: 'countries' },
{ user: userId },
],
read: {
$nin: [ userId ]
}
}
).count();
So all documents which has cars or countries or a userId and the userId is not in the read-array will be selected with that, right?
Update
I think my question is not precise enough. So let me explain it a bit better:
I want to get all docs of collection with user: userId - if there are any docs which matches this.
Additional (!) I need to get all docs which matches section: 'cars' if the user has the role something. So in this case there will be more results.
If the user has also the role anything I also want to get the results of section: 'vegetables' and add them to the result. So all of these things are not depending on each other.
Excluding the last thing read: { $nin: [ userId ] }: This should be a needed thing for every single match described above.

Categories