Here is my plan:
We have an array of objects retrieved from the database. Let's focus on one item: this.usersObj.
@Input() inlinetext: string; <<-- This represents the input field from the UI
public usersObj: string[]; <<-- Type definition for usersObj
[
{
"fname": "joe",
"lname": "jones",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7ddd8d2f7ddd8d299d4d8da">[email protected]</a>"
},{
"fname": "pete",
"lname": "daniels",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a6b3a2b396a6b3a2b3f8b5b9bb">[email protected]</a>"
},{
"fname": "peter",
"lname": "stephens",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9c9dccddccbf9c9dccddccb97dad6d4">[email protected]</a>"
},{
"fname": "mary",
"lname": "franklin",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cda0acbfb48da0acbfb4e3aea2a0">[email protected]</a>"
},{
"fname": "jane",
"lname": "jefferson",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4329222d260329222d266d202c2e">[email protected]</a>"
}
]
I'm currently using _.forOwn() to extract a specific group of people based on name search criteria such as first name, last name, or both.
In this instance, I am only searching by first name like so:
public getAllUsers(): Observable<Object> {
let currUserObj: any;
let allUsersObj: any;
const endpoint = `${environment.SOCIAL_SHARE.users}/`;
currUserObj = this._sessionUsersService.getSessionUsers(); // retrieve current users only
allUsersObj = endpoint;
console.log('currently logged session users ', currUserObj);
console.log('all users endpoint: ', allUsersObj);
this.http.get<Object>(`${endpoint}`, {headers: this.httpheaders}).subscribe(
(response: any) => {
this.usersObj = response;
console.log('Get List of Reply Comments Response: ', this.usersObj);
// Compare input value with DB entries
this.getUsersOnly();
}, (error: any) => {
console.log('Get List of Reply Comments Error: ', error);
}
);
return undefined;
}
public getUsersOnly(): Object {
const self = this; // necessary evil for _.forOwn
let ctr = 0;
_.forOwn(this.usersObj, function (value: any, key: any) {
if (value.firstName !== undefined && value.lastName !== undefined) {
if (value.firstName === self.inlinetext) {
console.log('Found someone: ', key + ' : ' + value.firstName);
self.retUsers.push(value); // PUSH method required here
}
}
});
ctr = 0;
console.log('Found name object: ', this.retUsers);
return this.retUsers;
}
If I enter "Pete", I expect to receive a list containing Pete and Peter within this.retUsers:
[
{
"fname": "pete",
"lname": "daniels",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="403025342500302534256e232f2d">[email protected]</a>"
},{
"fname": "peter",
"lname": "stephens",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c2d7c6d7c0f2c2d7c6d7c09cd1dddf">[email protected]</a>"
}
]
If I type "Peter", only this result will be shown:
[
{
"fname": "peter",
"lname": "stephens",
"email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89f9ecfdecfbc9f9ecfdecfba7eae6e4">[email protected]</a>"
}
]
The numeric values under "Pete" represent the keys of the corresponding values.
Any suggestions on how to improve this process?
UPDATE:
Solution involves using the PUSH method. self.retUsers.push(value); <-- THIS IS WHAT NEEDS TO BE DONE (PUSH)
BETTER UPDATE:
This method simplifies the process and eliminates the need for previous steps! Success!
_.forOwn(this.usersObj, function (value: any, key: any) {
if (value.firstName !== undefined && value.lastName !== undefined) {
if (value.firstName === self.inlinetext) {
console.log('Found someone: ', key + ' : ' + value.firstName);
self.retUsers.push(value); <-- THIS IS WHAT NEEDS TO BE DONE (PUSH)
}
}
});