Autocomplete values using array - javascript

Is it possible to use two arrays to insert data into values rather then hard coding the names of the users in tabulator.
One array would hold the username while the other would hold the actual name of the user.
{title:"Approver", field:"Approver", editor:"autocomplete", editorParams:
{
values:{
"jd1":"John Doe",
"mm12":"Marty McFly",
}
}
}

You should use Object.keys(values) and Object.values(values) to obtain username and fullname as two arrays, respectively.

Related

How can I get sliced documents

I have many documents like this:
How can I get, say, 11 documents without everything expect content.headline, content.description, content.tags and id fields via _id.
Simply put I have array of _id and I want to get news fields via this array.
Array example:
[
6002baa15e0b210f9ccd3060,
6002baa25e0b210f9ccd3062,
6002ba9e5e0b210f9ccd305c,
6002baa05e0b210f9ccd305f,
6002ba9f5e0b210f9ccd305d,
6002ba9e5e0b210f9ccd305b,
6002baa15e0b210f9ccd3061,
6002ba9c5e0b210f9ccd3059,
6002ba8d5e0b210f9ccd3058,
6002ba9d5e0b210f9ccd305a,
6002baa05e0b210f9ccd305e
]
You use the same dot notation as it is object.
You do normal find with project and limit.
db.coll.find({},
{
"content.headline":1,
"another_field":1
}
).limit(11)

How to merge multiple dynamic queries

I'm using node to communicate with my postgresql database.
I have a list of cars:
[{id:1, areaId: 1},
{id:2, areaId: 2},
{id:3, areaId: 2}]
areaId is a column in another table (say areas), and I want to fetch the area name for each car:
select name from areas
where id = areaId
How can I build a list containing all the cars with the area name attached?
The naive approach is to query the database for each object:
const query = `select name from areas
where id = $1`;
return pg.query(query, car.areaId);
But it seems like a lot of queries to be executed.
You should be able to select multiples Id's at the same time as this.
SELECT name
FROM areas
where ID in (5263, 5625, 5628, 5621)
Yes, you can specify multiple car.ids and fetch car.name and area.name and return them together.
Further you can sort the response by car.id using ORDER BY. I am not aware of any way to enforce the ordering based on the order of the passed in IDS, but you could easily sort the results in memory very quickly with JavaScript.
There are multiple ways to do this in SQL: inner join, full join, subselect...maybe more. I'll demonstrate the "inner join" as it's usually the most efficient, and some SQL query planners will convert subselects to inner joins anyway.
You haven't specified, but assuming you're using node-postgres...here's an answer when carIds is already defined, as an array:
Inner join
const query = `SELECT car.id, car.name, areas.name FROM car INNER JOIN area ON car.areaId WHERE car.id = ANY (${carIds}) ORDER BY car.id`
Finally:
return pq.query(query, carIds)

extracting a list of 2 property values

I have an object array:
user :[
{
name: String,
username: String
}
]
I want to view every change either to name or username.
I found underscore _.pluck only does the trick for one property (_.pluck(user, 'name')
Is there another way to have the list of both values?
With pluck you can only use one property, it simply isn't made to retrieve multiple. The method you would want to use is map, as suggested in this relevant question + answer: How to pluck multiple attributes from a Backbone collection?
Assuming you want the following output [['nameA','usernameA'],['nameB','usernameB'],...]], you could use map in the following manner:
var myResult = users.map(function(user) {
return [user.name, user.username];
});
NOTE: I changed the variable user to users to make more sense with your data.

cross-referencing two firebase arrays to obtain data

I was wondering if there's an easy way to pull out a value from one array based on its corresponding name.
Here, I've obtained the user email from the "coupons" collection. Now I would like to search through the "users" collection, find similar email, and output its corresponding "Name" ("Wes Haque Enterprises") to a $scope variable.
I already have references to both collections and $scope objects which have those references stored.
I just wanted to know if there's an easy way to traverse through the $scope.users object looking for the string "wes#wes.com" and then extracting "Wes Haque Enterprises" from it. Thanks.
Assuming that you don't really want to iterate (traverse?) over a bunch of arrays but instead query for the data you need...
You can query the users node for the data you need. In MacOS:
FQuery *allUsers = [usersRef queryOrderedByChild:#"emailAddress"];
FQuery *thisUser = [allUsers queryEqualToValue:#"wes#wes.com"];
[thisUser observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
for ( FDataSnapshot *child in snapshot.children) {
NSLog(#"%#", child);
}
}]
The result of the query will contain "Wes Haque Enterprises"
Or
ref.orderByChild("emailAddress").equalTo("wes#wes.com").on("child_added", function(snapshot) {
console.log(snapshot.key());
});

Taffydb dynamic like 'and' query

I recently started working with taffydb. Assuming I have this as my data
db= TAFFY([
{OrderNo:'prod1',range: 3,description:'one two'},
{OrderNo:'prod2',range: 2,description:'one two three'},
{OrderNo:'prod3',range: 2,description:'one three two'},
{OrderNo:'prod4',range: 6,description:'one two four three'},
{OrderNo:'prod5',range: 5,description:'three'},...
if I wanted to write a query to find all records with "one two" and "three" I'd do something like
db({description:{likenocase:"one two"}},{description:{likenocase:"three"}}).get()
this would return products 2 and 4. Unfortunately I can't figure out how to do this with a dynamic query that will have an unknown number of variables to search for. I'm doing this to let the user search for their own supplied words.
Anyone got any ideas?
As a precursor, this will not be the best answer to your problem. But it will work. :)
So the user will have the option of searching a database with an "unknown number of variables". Let's add a maximum amount of variables--perhaps 10?
Now we catch all the user's search variables in an array:
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
userSearchVars.push( $(this).val());
}
// Note: by default an empty input will return the empty string: ""
Using your code snippet, just query the database with the array:
db(
{description:{likenocase:userSearchVars[0]}},
{description:{likenocase:userSearchVars[1]}},
{description:{likenocase:userSearchVars[2]}},
{description:{likenocase:userSearchVars[3]}},
{description:{likenocase:userSearchVars[4]}},
{description:{likenocase:userSearchVars[5]}},
{description:{likenocase:userSearchVars[6]}},
{description:{likenocase:userSearchVars[7]}},
{description:{likenocase:userSearchVars[8]}},
{description:{likenocase:userSearchVars[9]}}
).get()
Adapting #Jacob-IT's answer so that its dynamic. Used Taffy for the first time tonight and just discovered you can pass an array of objects as the query argument.
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
// This is my edit - push the whole query on to the array.
userSearchVars.push({description:{likenocase: $(this).val() }});
}
// Then pass the whole query array in...
db( userSearchVars ).get()
Tested the above - and it worked for me.
You can do it like this
let items = [];
items.push({description:{likenocase:"one two"}});
items.push({description:{likenocase:"three"}});
db(...items).get()

Categories

Resources