$.unique(array) is not working for array elements having multiple words - jquery-autocomplete

var availableNames=['Hello India', 'Hello India', 'Test', 'Test']
$.unique(availableNames);
$("#corpName").autocomplete({
source : availableNames,
minLength : 4
});
availableNames is my array source for auto-complete. I want to show unique values in the list so called jquery unique function.
The unique function is working fine for single words like 'Hello' but not for two word strings like 'Hello India'. Its showing two 'Hello India' in dropdown and one 'Hello'.
Please Suggest me how to display only unique values in the drop-down list.
Thank You.

you have to write your own function. There are two ways to do that:
use filter:
// returns a new array with unique entries
Array.prototype.unique = function() {
return this.filter(function(el, index, oThis) {
return index === oThis.indexOf(el);
});
}
EDIT here is a real prototype version, that changes the original array
Array.prototype.unique2 = function() {
var c=this.length;
while(c--)
c===this.indexOf(this[c])||this.splice(c,1);
};
you can test both versions in this fiddle: http://jsfiddle.net/6mpqaxhk/

Related

Create array and randomize node.js array

I'm trying to write a cisco webex bot which get all people in the space(room) and randomly write only one name.
I have this code
framework.hears("daily host", function (bot) {
console.log("Choosing a daily host");
responded = true;
// Use the webex SDK to get the list of users in this space
bot.webex.memberships.list({roomId: bot.room.id})
.then((memberships) => {
for (const member of memberships.items) {
if (member.personId === bot.person.id) {
// Skip myself!
continue;
}
let names = (member.personDisplayName) ? member.personDisplayName : member.personEmail;
let arrays = names.split('\n');
var array = arrays[Math.floor(Math.random()*items.length)];
console.log(array)
bot.say(`Hello ${array}`);
}
})
.catch((e) => {
console.error(`Call to sdk.memberships.get() failed: ${e.messages}`);
bot.say('Hello everybody!');
});
});
But this doesn't work.
Also name after i use let arrays = names.split('\n'); separated by space and don't have comma.
I think because of what code doesn't work
Output of console log:
[ 'George Washington' ]
[ 'John' ]
[ 'William Howard Taft' ]
Main question now how to turn output to array?
That's because arrays[Math.floor(Math.random()*items.length)] only assigns an array with length 3. You need to randomise the index and push to array or use a sort function on the original array
var array = arrays.sort((a,b)=>{
return Math.floor(Math.random()*arrays.length);
});
if you are looking to get the output as per you question you can use reduce instead of sort.
var arrays = [ 'George Washington', 'John', 'William Howard Taft'];
var array = arrays.reduce((a,i)=>{
if(!a) a = [];
a.splice(Math.floor(Math.random()*arrays.length), 0, [i]);
return a;
},[]);
Here is how to get a single name from your data, and ensuring it is a string. There are only four names in the array, so run the snippet several times if you keep getting the same name.
// A list of names. Notice that Arraymond is an array; the other names are strings.
const names = [ 'George Washington', 'John', 'William Howard Taft', ['Arraymond'] ];
// Randomize the names
const randomNames = names.sort(() => Math.random() - 0.5);
// Get the first name. Make sure the name is a string (not an array)
const name = randomNames[0].toString();
console.log(name)
A tip: don't name your array "array" or "arrays" - it is not meaningful. Use good naming conventions and meaningful variable names that help others understand what the code is doing.

Edit Global array in Express

I want to change one item from array from app locals variable and i am not sure how to do this
Here is what i have set as global
app.locals.products=[{name: 'a',url: '/a' },
{name: 'b',url: '/b' },
{name: 'c',url: '/c' },...
I want to edit them and set active product from routes and do something like this
products:[{name: 'a',url: '/a' active:true}],
But when i do this it will remove all the other items and set only the product the one i wrote. Is there a way to edit just the one i need and leave all the rest unchanged?
You can use the array find function to find a specific item in the products array and edit it.
function setActive(name) {
var element = products.find(function(product) {
return product.name === name;
});
if (element) {
element.active = true;
}
}
This function takes the name variable, checks if it exists in the product array and sets it to active. You might want to throw an exception if it doesn't.
Usage:
setActive('a');

Get Saved Search values for multiple "formula" columns

Morning Gurus,
I have a saved search within Netsuite with multiple "formula" columns.
For example, there are several formulapercent' named columns, although thelabel' for each is unique.
However when using nlobjSearchResult.getValue('formulapercent') naturally I only get the first formulapercent column value.
How do I specify in getValue which of the formula columns I want to return the value for?
I really don't want to use a column number, in case I need to insert a new column to the saved search within Netsuite later.
Hoping for something along the lines of nlobjSearchResult.getValue('formulapercent','<label>')
I have tried the multi parameter option, but it does not work.
Simple fix?
Cheers
Steve
What I generally do is add a label to the saved search formula columns. Then:
var f1Val, f2Val, etc;
results.forEach(function(res){
var cols = res.getAllColumns();
cols.forEach(function(col){
switch(col.getLabel()){
case 'formula1' : f1Val = res.getValue(col); break;
case 'formula2' : f2Val = res.getValue(col); break;
...
}
});
});
Thought I'd add an answer I have since learned.
Instead of generically numbering the columns. For example:
var column = []
column[0] = new nlobjSearchColumn('formulanumeric').setFormula('myformula1');
column[1] = new nlobjSearchColumn('formulanumeric').setFormula('myformula2');
searchresults = nlapiSearchRecord(.......);
Instead of this, I found the easiest way to retrieve the formula column values was to uniquely define the columns:
var colformula1 = new nlobjSearchColumn('formulanumeric').setFormula('myformula1');
var colformula2 = new nlobjSearchColumn('formulanumeric').setFormula('myformula2');
var searchresults = nlapiSearchRecord('item',null,filters,[colformula1,colformula2]);
To then grab the formula results:
var formulares1 = searchresults[i].getValue(colformula1');
var formulares2 = searchresults[i].getValue(colformula2');
Removes the issue if column orders change.
Thought this might help somebody.
There is a method in the nlobjSearchResult object called getAllColumns(). Then I use the index of the formula columns to get the value.
I dont't know of any other way to get the values of the formula columns. Do note that if you use this method, if you change the order of the columns in the saved search it will break your script.
This works for me using SuiteScript 2.0. Place this into a function and pass in the needed variables
if(join){
if(summary){
if(String(name).startsWith("formula")){
return result.getValue(result.columns[column])
}else{
var searchResult = result.getValue({
name: name,
join: join,
summary:summary
});
return searchResult
}
}else{
if(String(name).startsWith("formula")){
return result.getValue(result.columns[column])
}else{
var searchResult = result.getValue({
name: name,
join: join
});
return searchResult
}
}
}else{
if(summary){
if(String(name).startsWith("formula")){
return result.getValue(result.columns[column])
}else{
var searchResult = result.getValue({
name: name,
summary: summary,
});
if((column==7 || column ==8 || column==10 || column==12) && type=='cases'){
return dropDown_Obj[column].getKeyByValue(searchResult)
}
return searchResult
}
}else{
if(String(name).startsWith("formula")){
return result.getValue(result.columns[column])
}else{
var searchResult = result.getValue({
name: name
});
return searchResult
}
}
}

Deleting a row from JQuery Jtable Client Side only

I am trying to delete a row from JTable in client side only.But somehow i am not able to get the value of the key of the selected record.See the code below
deleteAction : function (postData) {
var row_id = $('.jtable-data-row').attr('data-record-key');
$('#AuthorTableContainer').jtable('deleteRecord', {
key: row_id,
clientOnly:true
});
}},
The problem is when the table contains the multiple record.then in that case "tr" attributes become
"jtable-data-row jtable-row-even" in that case i am not able to get the value of the data-record-key.
Is there any other way to delete a row from JTable from client side only?
Here it is
deleteAction : function (data) {
$('#AuthorTableContainer').jtable('deleteRecord', {
key: data.keyValue,
clientOnly:true
});
}},
In order to remove a row from a JTable, you will need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:
((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
If you want to delete any record from jtable and you only know key that's what with my case just pass it like below.
$('#detailVoucherTable').jtable('deleteRecord', { key: 0, clientOnly:true });
Regards
mOOn
digitaal.logix#gmail.com

node.js: How to make a variable out of a text input?

I want a user to put in a name of a file, then copy the content into an array and save this array with the name of the input filename. Here is the code I am talking about:
The array "items" represents the user input. Right now those items are fed into my function "einlesen" which returns the array with the data. This data is now written into the same array results but I want it to be written into the arrays "GVZ", "TAL", and "XPG". Can someone help me out?
Grateful for every idea :)
Nils
var items = [['GZV', ';', 1, 2], ['TAL', '<|>', 1, 'n'], ['XPG', '<|>', 0, 2]];
var results = [];
items.forEach(function(item) {
einlesen.einlesen(item[0], item[1], item[2], item[3], function(err, result){
results.push(result[1][0]);
console.log(item[0]);
if(results.length == items.length) {
final();
}
})
});
You would be better off using an object instead of an array of arrays:
var items = {
GZV: [...],
TAL: [...],
XPG: [...]
}
for(item in items) {
einlesen.einlesen(...)
}
This way, you can create object properties based on user input. It is not possible to create a variable (presuming you mean variable name?) out of user input.
var newProp = "resultOfInput";
items[newProp] = [...];
console.log(items.resultOfInput) // [...]
If you're doing this, you need to ensure that there's strong validation on the input. For example; if the input contains any spaces this will prevent the property being accessed via dot notation. Worse still, duplicate user input could easily over-ride any existing properties in the array.

Resources