merging tables - inserting every nth - node.js

I have the instance where I need to merge 2 tables to one where the result of the second is inserted every nth into the merged table.
to illustrate i have a products table and i need to insert an advert in every nth (say 4th) result of the final output. i have made the assumption the best way to handle these is to keep the two data source tables and merge the desired result into a new table (if theres a better way to use just one table and insert into that I'm interested)
the main data source: products
var productDataTable = [
{id: '1', content_type: 'product', name:'t-shirt'},
{id: '2', content_type: 'product', name:'t-shirt2'},
{id: '3', content_type: 'product', name:'t-shirt3'},
// .....
];
secondary datasource : adverts
var advertsTable = [
{id: 'advert1', content_type: 'advert', name: null},
{id: 'advert2', content_type: 'advert', name: null},
{id: 'advert3', content_type: 'advert', name: null},
// ....
];
desired output of final table required - an advert has been inserted every nth
var feedOutput = [
{id: '1', content_type: 'product', name:'t-shirt'},
{id: '2', content_type: 'product', name:'t-shirt2'},
{id: '3', content_type: 'product', name:'t-shirt3'},
{id: 'advert1', content_type: 'advert', name: null},
{id: '4', content_type: 'product', name:'t-shirt4'},
{id: '5', content_type: 'product', name:'t-shirt5'},
{id: '6', content_type: 'product', name:'t-shirt6'},
{id: 'advert2', content_type: 'advert', name: null},
];
query example
var params = {
TableName: 'feedOutput',
};
docClient.scan(params, function(err, data){
console.log(data)
});
Question:
what would be the most appropriate way to joint these two tables in this nth manner ? i am using node.js with the aws sdk.
thanks in advance.

Related

NetSuite SuiteScript 2.0 - nlobjSearchColumn contains an invalid column

I am trying to retrieve a specific system note below:
var noteSearch = search.create({
type: record.Type.NOTE,
columns: ['id', 'title', 'notetype', 'notedate', 'note'],
filters: [
search.createFilter({
name: 'id',
operator: search.Operator.EQUALTO,
values: 33
})
]
});
var noteSearchResultSet = noteSearch.run();
The error I'm getting:
{"type":"error.SuiteScriptError","name":"SSS_INVALID_SRCH_COL","message":"An nlobjSearchColumn contains an invalid column, or is not in proper syntax: id.","stack":["Error\n at Object.onRequest (...)"],"cause":{"type":"internal error","code":"SSS_INVALID_SRCH_COL","details":"An nlobjSearchColumn contains an invalid column, or is not in proper syntax: id.","userEvent":null,"stackTrace"
EDIT:
internalid has same error:
var noteSearch = search.create({
type: record.Type.NOTE,
columns: ['internalid', 'title', 'notetype', 'notedate', 'note'],
filters: [
search.createFilter({
name: 'internalid',
operator: search.Operator.EQUALTO,
values: 33
})
]
});
The note in question:
Try using another filter type: 'is' maybe. I guess the code below could work using either 'id' or 'internalid'.
var noteSearch = search.create({
type: record.Type.NOTE,
columns: ['internalid', 'title', 'notetype', 'notedate', 'note'],
filters: [
['internalid', 'is', 33]
]
});

Map Array of Array?

I have a crazy array look like this:
const data = [
[{ Name: 'Name 1', Block: [{Id: "1"}, {Id: "2"}] }],
[{ Name: 'Name 2', Block: [{Id: "3"}, {Id: "4"}] }],
]
I want to map Block to a single array to look like this:
[ { Id: '1' },
{ Id: '2' },
{ Id: '3' },
{ Id: '4' }
]
I have tried doing like this:
const data = [
[{ Name: 'Name 1', Block: [{Id: "1"}, {Id: "2"}] }],
[{ Name: 'Name 2', Block: [{Id: "3"}, {Id: "4"}] }],
]
const idList = data.map(blockData => {
return blockData[0].Block;
});
console.log(idList)
What did I do wrong?
.map will create a new item for every index of the old array. If your input array has 2 items, the output array will also only have 2 items - but you want 4 items, so .map won't work. Use flatMap instead, to flatten:
const data = [
[{ Name: 'Name 1', Block: [{Id: "1"}, {Id: "2"}] }],
[{ Name: 'Name 2', Block: [{Id: "3"}, {Id: "4"}] }],
];
const idList = data.flatMap(([{ Block }]) => Block);
console.log(idList)
flatMap is only implemented on newer implementations, though - otherwise, use a polyfill or a different method, like reduceing into an array:
const data = [
[{ Name: 'Name 1', Block: [{Id: "1"}, {Id: "2"}] }],
[{ Name: 'Name 2', Block: [{Id: "3"}, {Id: "4"}] }],
];
const idList = data.reduce((a, [{ Block }]) => a.concat(Block), []);
console.log(idList)
your data is an array inside array so you should use map twice, Buth that will give you an array with 2 elements now you need to reduce or flatten the resulting array to get the desired output.
data.map(a=>{return a[0].Block.map(b=>{ return b})}).reduce((o, m) => [...m, ...o], [])
The most succinct way to do it would be with a reduce statement:
const reducer = (a, b) => a.concat(b[0].Block);
const idList = data.reduce(reducer, []);
This way it will be much clearer what you are trying to do.

How to add condition in mongoose?

I need a mongoose query, to select only one record in or condition. There is a collection of blogs. Some of them are in English or in French. Certain blogs are a duplicate like they have the same content but in a different language.But they have same 'group_id'.
If I filter blogs based on some criteria, like 'category' only blogs with the 'userLanguage' should show. But if there is no, show the other language. How do I generate a mongoose query for this?
db.find($and:[{category:'Health'},{$or: [{language: 'en'},{language:'fr'}]}])
But it gives 2 records if both 'en' and 'fr' is present, I need only either 'en' or 'fr'. How can I implement this in one query ?
These are some sample documents,
"{_id: '1', category_id: 'xyz', language:'en', group_id: 'aaa'} ",
"{_id: '2', category_id: 'xyz', language:'fr', group_id: 'aaa'}",
"{_id: '3', category_id: 'xyz' language: 'en', group_id: 'bbb'}",
"{_id: '4', category_id: 'xyz', language: 'fr', group_id: 'ccc'}"
I request for category_id : 'xyz' and language: 'en'. So the result should be,
"{_id: '1', category_id: 'xyz', language:'en', group_id: 'aaa'} ",
"{_id: '3', category_id: 'xyz' language: 'en', group_id: 'bbb'}",
"{_id: '4', category_id: 'xyz', language: 'fr', group_id: 'ccc'}"
I have an array of distinct group_id = ['aaa','bbb','ccc']. So the condition is like,
group_id: $in:group_id $$ language should be 'en' if not 'fr'
Use findOne query, which gives you only one result, like this
db.findOne($and:[{category:'Health'},{$or: [{language: 'en'},{language:'fr'}]}]);

Querying a result returned by mongoose in nodejs

I am relatively new to Node.js, Mongoose and MongoDB.
I want to perform filter functionality and want to filter products by criteria selected by the user.
Is it possible in Node.js to query a response returned by Mongoose?
Sample response from Mongoose as below:
[ { _id: 589860c21f9997fce3502f10,
title: 'Watch',
brand: 'PUMA2',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '58a420cd7c77aca4b3ce34cd' },
{ _id: 5899bd33c28dbdf2b938f698,
title: 'Watch 2',
brand: 'PUMA',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '58a420cd7c77aca4b3ce34cd' },
{ _id: 5899bd59c28dbdf2b938f69a,
title: 'Watch 4',
brand: 'PUMA',
store: 'ZARA',
for: 'MALE',
size: '32',
colour: 'RED',
userId: '5899bde3c28dbdf2b938f69e' }]
Now how can I query this response to select data based on brand.
For finding PUMA brand, then query will be
db.collection.find({'brand': 'PUMA'})
Thanks for help everyone, finally got nice plugin called array-query which helped me to achieve what I want.
https://www.npmjs.com/package/array-query

Clustering algorithm using NodeJS

I have a set of data:
{ name: 'item1', timeOfDay: '36000', dayOfWeek: '1', dayOfMonth: '15', room: '1'}
{ name: 'item2', timeOfDay: '3600', dayOfWeek: '2', dayOfMonth: '10', room: '2'}
{ name: 'item1', timeOfDay: '18000', dayOfWeek: '3', dayOfMonth: '20', room: '3'}
{ name: 'item3', timeOfDay: '72000', dayOfWeek: '4', dayOfMonth: '5', room: '4'}
Given a new item i'm looking for an algorithm to find the closest items order by distance
{ name: 'item2', timeOfDay: '36000', dayOfWeek: '5', dayOfMonth: '3', room: '2'}
First I looked at kMeans to organise items around centers but I have the feeling I need something sorting the item at the good location.
Multicriteria sort ? but that could learn which criteria is the stronger ?
I don't want to do Array.sort() because:
I need to add a new item without sorting all the array
I need to merge same (closest) values

Resources