如何使用Ionic 4在数组中寻找价值?

时间:2019-05-20 05:42:41

标签: angular ionic-framework ionic4

我正在使用呼叫者姓名播音员应用程序,为此应用程序,我将联系人存储在本地存储中,并在数组中获取联系人值。我之所以这样做,是因为当有人打电话给我时,需要一段时间才能找到联系人。因此,我的上级建议我将其存储在本地存储中,并在收到呼叫时从本地存储中找到该存储,这样便可以更快地开始工作。为此,我做了这段代码,请尽可能帮助我。

我正在使用此插件:contacts

declare var window: any;

setContacts() {
const options = new ContactFindOptions();
options.filter = "";
options.multiple = true;
options.hasPhoneNumber = true;

this.contact.find(['*'], options).then((contacts) => {
    this.ArrayData = contacts;
    this.storage.set("mycontactsdata", this.ArrayData);
    console.log("stringify contact all", JSON.stringify(this.ArrayData));
});
var testobj = JSON.stringify(this.ArrayData);
localStorage.setItem("testobjdata", testobj);
}

phonecalls() {
if (window.PhoneCallTrap) {
    window.PhoneCallTrap.onCall((obj) => {
        var callObj = JSON.parse(obj),
            state = callObj.state,
            callingNumber = callObj.incomingNumber;
        if (callingNumber > 0) {
            var resultdata = localStorage.getItem("testobjdata");
            if (resultdata) {
                this.ArrayData1 = JSON.parse(resultdata);
            }
            this.ArrayData1.forEach(function (item) {
                console.log(item.displayName);
                if (item._objectInstance.phoneNumbers[0] == callingNumber) {
                    console.log("found");
                    this.contactsfoundcallingNumber = item._objectInstance.displayName;
                    console.log(this.contactsfoundcallingNumber);
                }
            });
        }
    });
}
}

本地存储中的价值结构

[
{ "_objectInstance": 
{ 
    "id": "1", 
    "rawId": null, 
    "displayName": "Test contact name", 
    "name": { 
        "familyName": "Test", 
        "givenName": "Test", 
        "middleName": "Test", 
        "formatted": "Test Test Test" 
    }, 
        "nickname": null, 
        "phoneNumbers": [
            { 
                "id": "56", 
                "pref": false, 
                "value": "+91 xxxxx xxxxx", 
                "type": "mobile" 
            }, 
            { 
                "id": "192394", 
                "pref": false, 
                "value": "+91xxxxxxxxxx", 
                "type": "mobile" 
            }, 
            {
                "id": "201516", 
                "pref": false, 
                "value": "+91 xxxxx xxxxx", 
                "type": "mobile" 
            }
        ], 
        "emails": null, 
        "addresses": null, 
        "ims": null, 
        "organizations": null, 
        "birthday": "xxxx-xx-xxT18:30:00.000Z", 
        "note": null, 
        "photos": [
            { 
                "id": "193840", 
                "pref": false, 
                "type": "url", 
                "value": "content://com.android.contacts/contacts/1/photo" 
            }
        ], 
        "categories": null, 
        "urls": null 
    }, "rawId": "8" 
}, 
]

当我使用调试器并使用USB调试在chrome inspect中进行检查时,我得到了此错误和forEach函数中使用的项目。

message: "item is not defined"
stack: "ReferenceError: item is not defined↵    at eval (eval at <anonymous> (http://localhost/pages-home-home-module.js:411:21), <anonymous>:1:1)↵    at http://localhost/pages-home-home-module.js:411:21↵    at Object.callbackFromNative (http://localhost/cordova.js:291:58)↵    at <anonymous>:1:9"
__proto__: Error

2 个答案:

答案 0 :(得分:1)

实际上,在存储中获取数据时会出现问题 var resultdata = localStorage.getItem("testobjdata");

以上操作无效,因为访问内存和获取数据需要时间,因此getItem()将返回一个promise,要解决这个问题,您必须这样做:

let resultdata = [];
localStorage.getItem("testobjdata").then(data => resultData = data);

请注意,由于javascript使用单个线程,因此resultData之前的变量then()的任何函数或任何用法都将被取消

随时查看Storage文档

答案 1 :(得分:0)

只需尝试:

let indexOfCallingNum = this.ArrayData1.indexOf(callingNumber);
console.log("Got it : "+JSON.stringify(this.ArrayData1[indexOfCallingNum]));