How to reset array to empty in nodejs? - node.js

In project I define an empty array var reply= []; and in a portion of code and conditions I had o reset it to its origin as an empty array. any one can help how to do this instead of using pop method.?

some times tiny questions are looking so big.
try:
reply = [];

You have to set length of an array to zero or use splice.
reply.length = 0
// or
reply.splice(0, reply.length)
setting it to [] will create a new array.

Related

Netsuite Saved Search - Return a number randomly from given values

I am just wondering if someone could help me to return a number randomly from given values for a Netsuite Saved Search
For example:
I want to return either one of the 3 values randomly here: 196429,190569,150567
Thank you so much
First thing that needs to be done here is to return the saved search values and store them in an array. Please try this and let me know how this goes!!
1.Define an array like below
var getArray = [];
2.Push the saved search values to the array by looping through the saved search results like below (I am pushing internal id here, you can push any value you want)
for (var i = 0; i < searchResult.length; i++)
{
var id = searchResult[i].getValue({ name: 'internalid'});
getArray.push(id);
}
3.The last step would be to generate the random item from the array
var randomGen = getArray[Math.floor(Math.random()*getArray.length)];
log.debug('randomGen',randomGen);

"new Set" is returning an empty set in nodejs

I was testing some websites using the below function with n, suddenly "new Set" started returning empty Array as the following:
function collectAllSameOriginAnchorsDeep(sameOrigin = true) {
const allElements = [];
// Some coding here
const filtered = allElements
// Some coding here
console.log(filtered) // The items are printed in the log probably
return Array.from(new Set(filtered)); // Nothing is getting returned!
}
If I replace the last line with return Array.from(filtered), then it works fine but I'm using "Set" to remove any repeated values.
Because Set receives iterable object as its input parameter, and will create set object respectively. Hence, we can construct a set from an array — but it will only include distinct elements from that array, aka no duplicate.
And of course, we can also convert a set back to array using Array.from() method.
let set = new Set([1,2,3]); // {1,2,3}
let arr = Array.from(set);//[1,2,3]
So you need to convert set back to array , please check here to understand the difference between set and array.
Hope this will help you!

NotesException: Unknown or unsupported object type in Vector

I'm trying to add new names to the address book programmatically but I'm getting the following error:
[TypeError] Exception occurred calling method NotesDocument.replaceItemValue(string, Array)
Unknown or unsupported object type in Vector
Code snippet below:
var addressBook = session.getDatabase("","names.nsf");
var gView:NotesView = addressBook.getView("($VIMGroups)");
var gDoc:NotesDocument = gView.getDocumentByKey("groupName", true);
var newg:java.util.Vector = [];
var mems:java.util.Vector = new Array(gDoc.getItemValue('Members'));
newg.push(mems);
var newNames:java.util.Vector = new Array(getComponent("NewMems").getValue());
newg.push(newNames);
gDoc.replaceItemValue("Members", newg);
gDoc.save();
Adding a single user works fine, but then it does not save users in the required canonical format below:
CN=John Doe/O=Org
Instead it is saved in the original format below:
John Doe/Org
I look forward to your suggestions. Thanks.
You can't store an Array in a field. Make newg a java.util.Vector instead and integrate with that.
For OpenNTF Domino API the team wrote a lot of code to auto-convert to Vectors, which may cover Arrays.
Don't use an Array (which is a JS thing). Initialize it as a Vector.
var newg:java.util.Vector = new java.util.Vectory();
Then look up the Vector methods to see how to add to that vector. Not sure if you will have to convert the names using the Name method but I would store them as "CN=Joe Smith/O=Test Org" to be sure you got the right format.
I was able to solve the issue using a forloop to loop through the list and push it into a newly created array. Using the forloop seems to make the difference.
var newg = [];
var group = new Array(getComponent("NewMems").getValue()), lenGA = group.length;
for(i = 0; i < lenGA; i++){
newg.push(group[i]);
}
gDoc.replaceItemValue("Members", newg);
gDoc.save();
An explanation about this behaviour will be appreciated.

Get Bitmap from Redis in binary

I am working on a user activity feature in which I track the activity of a user everyday. I am thinking of using bitmap, so users who will be active on that day will be set to 1.
SETBIT users:2015:9:30 <userid> 1
I don't want to do GETBIT for each user. I want to retrieve the entire value in binary so I can iterate through it and find the active or inactive users.
I would like to know if this approach is correct and which node-module to use.
Bit arrays in Redis are stored as strings. To get the entire array, just do a regular GET and parse the bytes from the result.
To get the bitarray you should set return_buffers as true and then do a get operation on it this will give you a buffer object. Convert this buffer object to hex string and then iterate through it.
My solution
neoredis = require 'redis'
key = "h"
map =
"0":[]
"1":[3]
"2":[2]
"3":[2,3]
"4":[1]
"5":[1,3]
"6":[1,2]
"7":[1,2,3]
"8":[0]
"9":[0,3]
"a":[0,2]
"b":[0,2,3]
"c":[0,1]
"d":[0,1,3]
"e":[0,1,2]
"f":[0,1,2,3]
usersbufferobject = null
redis = neoredis.createClient(6379,'redis',{return_buffers:true})
redis.get key,(err,res)->
redis.quit()
if not err?
if res?
usersbufferobject = res.toString('hex')
users = []
offset = 0
arr = usersbufferobject.match(/.{1,2}/g)
for i in arr
for j in map[i[0]]
users.push(offset+j)
for j in map[i[1]]
users.push(offset+4+j)
offset+=8
console.log "Active Users",users
else
console.log "No users active"
process.exit()

getDocumentByKey with a number vector doesn't find the document

I have a 2 column sorted view and try to get a document the following code:
var searchArr = new java.util.Vector();
searchArr.addElement(10000310);
searchArr.addElement(45);
var customerdoc:NotesDocument = viw.getDocumentByKey(searchArr,true);
but the result is null.
If I use only the first element for the key (10000310), then I get (the first) doc with that key. But with the 2-element-vector the lookup returns null.
the same in LotusScript works fine:
Dim searchkey(1) As Double
searchkey(0) = 10000307
searchkey(1) = 45
Set doc = luview.Getdocumentbykey(searchkey, true)
gives me the document I need.
Confusing, for me ....
Uwe
This is a known bug, hopefully to be fixed in 9.0.2. See this question getDocumentByKey with view category separated by "\\" in XPages
Your LS example uses an array, not a Vector. I am not even sure if it is intended to work with a Vector - never did that. So just use an array here, too, as the key.

Resources