count items in a field

84 views
Skip to first unread message

grahamsc...@gmail.com

unread,
Jan 16, 2023, 10:39:15 PM1/16/23
to mementodatabase
I have a contact field and it picks peoples name from my phone list and I want a total field where it counts those names is there an expression that does it 

Eg Contacts filed

Bill
Mary
Jo
Adam

Total Field
3

how can I do this

Craig Hunter

unread,
Jan 17, 2023, 8:49:00 AM1/17/23
to grahamsc...@gmail.com, mementodatabase
I see no simple way, but there is probably a way with a javascript field.

Bill Crews

unread,
Jan 17, 2023, 11:22:56 AM1/17/23
to Craig Hunter, grahamsc...@gmail.com, mementodatabase
I was never told by the developer what is returned by the field() method (or function, in a JavaScript field), so I never documented it in the Wiki, but like any other field that shows a list of things (like link to entry), I bet it returns an array of objects, each of which is a value of the field type -- in this case, a Contact object.

If I'm right, then in a JavaScript field, the code would be something like...

var contactsList = field("Contacts");
for (var contactx in contactsList) {
    var contact = contactsList[contactx];
    var name = contact.Name;
    // Now you can do something with the contact name (a text string)
    // Maybe you decide only to deal with the 1st contact in the list
}
name; // return the contact name as the value

If you decide to deal only with the 1st contact in the list, you can avoid the for loop and just grab that 1st contact name...

var name = contactsList[0];
name; // Return the name as the value of the field

(The wiki shows the property for the name in a Contact object to be Name, but if that doesn't work, you could try just name instead.)

Good luck.



--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/570B9FBF-A5D7-420B-B86B-003C909CC6E3%40gmail.com.

Craig Hunter

unread,
Jan 17, 2023, 1:50:56 PM1/17/23
to Bill Crews, grahamsc...@gmail.com, mementodatabase
Testing with this code in javascript field:

var contactsList = field("con-tact");
var contactCount = 0;
var contactx;
for (contactx in contactsList) {
contactCount++;
}
contactCount;

When the field "con-tact" has 3 contacts, the script returns 0 with execution time 75 ms.

Writing something based on the wiki documentation:
https://wiki.mementodatabase.com/index.php?title=Memento_JavaScript_Library#Built-in_objects_for_certain_Memento_field_types

First draft sketch:

var contactsList = field("con-tact");
var contactCount = 1;
//Loop start ...
if(contactsList.hasNext)
{ contactCount++;
contactsList.next;} //continue loop ...
// ~ else
// ~ {...} ;  //exit loop ...
contactCount;

I would have to refresh my memory on what kind of loop to use.

Er Mo

unread,
Jan 17, 2023, 1:58:10 PM1/17/23
to mementodatabase
Hallo
Schon mal mit der Länge versucht

Hi
Tried the length

var contactsList = field("con-tact");
var l = contactsList.length
l

Ernst

Craig Hunter

unread,
Jan 17, 2023, 2:39:09 PM1/17/23
to Er Mo, mementodatabase
Result is undefined.


var contactsList = field("con-tact");
var L = contactsList.length;
L;

Object JSContacts is not an array. It is something like a linked list. Use next and hasNext.

Bill Crews

unread,
Jan 17, 2023, 4:27:40 PM1/17/23
to Craig Hunter, Er Mo, mementodatabase
Well, if the length property is undefined, I was wrong, and the field doesn't return an array. I apologize. I guess it returns a Contact object. See if what is returned is a Contact object that has a Name or name property, among others. It probably does.

grahamsc...@gmail.com

unread,
Jan 17, 2023, 4:30:19 PM1/17/23
to mementodatabase
Thanks guys, tried all of the above and nothing is working to show how many contacts in the list

Craig Hunter

unread,
Jan 17, 2023, 6:15:49 PM1/17/23
to Bill Crews, Er Mo, mementodatabase
According to the wiki it returns a JSContacts object. The typeof operator returns object.

var contactsList = field("con-tact");
var theType, theName;
theType = typeof contactsList;
theType;
theName = contactsList.Name;
theName;

It does not have a name or Name propery.
contactsList.Name; ➡️ undefined.
contactsList.name; ➡️ undefined.

Now trying a loop with hasNext and next. This shows that contactsList.hasNext is always true. Looking for workaround possibility.


var contactsList = field("con-tact");
var contactCount = 0;
//Loop start
if(contactsList.hasNext)
{
do
{
contactCount++;
contactsList.next;}
while (contactsList.hasNext & contactCount < 6)
}
contactCount;

Craig Hunter

unread,
Jan 17, 2023, 7:00:49 PM1/17/23
to Bill Crews, Er Mo, mementodatabase
After a lot of experimentation I found something that works.
Edit the first line with your field name.
Edit the while statement & change 6 to a number greater than your count of contacts will ever be. (I added it to stop infinite loops.)


var contactsList = field("con-tact");
var contactCount = 1;
var currentContact;
currentContact = contactsList.next;
var nullContact;
nullContact = currentContact === null;
while (!nullContact & contactCount < 6)
{
contactCount++;
currentContact = currentContact.next;
nullContact = currentContact === null;
}
contactCount;

grahamsc...@gmail.com

unread,
Jan 17, 2023, 9:18:40 PM1/17/23
to mementodatabase

Craig thankyou very much I really appreciate you looking at this and a working solution
Thankyou once again

Bill Crews

unread,
Jan 17, 2023, 10:45:39 PM1/17/23
to grahamsc...@gmail.com, mementodatabase
The type of object is JSContact, not JSContacts, because it's a object representing a contact, not a list of contacts. I'm sorry I misled you early on.

So, again, consider it to be a Contact object (in JavaScript, a JSContact object, and reference those properties...
email
The primary email address of this contact
fullName
The full name of this contact
hasNext
Returns TRUE if there is a next JSContact object, otherwise FALSE
next
Returns the next JSContact object, if there is one.
phone
The primary phone number of this contact

Bill Crews

unread,
Jan 18, 2023, 12:20:30 AM1/18/23
to grahamsc...@gmail.com, mementodatabase
In case it wasn't obvious, multiple contacts are supported via the hasNext property, although I have no idea how to use hasNext. I assume it's a Boolean. Properties are text strings, not objects or Booleans, after all, and there's no listed getNext() method for a JSContact.

This method is in contrast the the normal array-based way to support multiple objects. Why the difference, I don't know.

Craig Hunter

unread,
Jan 18, 2023, 12:46:04 AM1/18/23
to grahamsc...@gmail.com, mementodatabase
This is a better version. It returns 0 if the record has no contact in the "con-tact" field. The previous version would return an error in that case.

var contactsList = field("con-tact");
var contactCount = 0;
if(!(contactsList === null))
{
contactCount++;
while (contactsList.hasNext & contactCount < 100)
{
contactCount++;
contactsList = contactsList.next;
}
}
contactCount;

grahamsc...@gmail.com

unread,
Jan 18, 2023, 3:32:41 AM1/18/23
to mementodatabase

Excellent thanks Craig 
Reply all
Reply to author
Forward
0 new messages