第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

還有另一種方法來查找并返回?cái)?shù)組的對(duì)象嗎?

還有另一種方法來查找并返回?cái)?shù)組的對(duì)象嗎?

忽然笑 2022-07-08 18:13:42
這是我的代碼:https ://repl.it/@OllySmith1/DaringSaltyText-1 ;即使我運(yùn)行 searchContact(),它也會(huì)返回正確的答案,但仍然會(huì)拋出此錯(cuò)誤。順便說一句,在這種情況下,有沒有辦法在數(shù)組中搜索對(duì)象?
查看完整描述

3 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

在這里,您最初沒有加載聯(lián)系人,其次,您循環(huán)的不僅僅是它的數(shù)組,然后是工作鏈接 https://repl.it/repls/ImperturbableImperturbableAbstractions


var fs = require('fs');

var readlineSync = require('readline-sync');

var Member = require('./dataAnalyze');

const uuidv4 = require("uuid/v4");


const contacts = [];


function main() {

    loadData();

    showMenu();

}


function loadData() {

    var data = fs.readFileSync('./data.json', { encoding: 'utf8' });

    var data1 = JSON.parse(data);

    contacts.push(data1) // initial push the data to contact array

}


function showContacts() {

    console.log("Contact List")  // add the console

    for (let contact of contacts) {

        console.log("Name:", contact.name, "phoneNo:",contact.phone, "Id:",contact.id); // modify the console

    };

    return contacts;

}


function showMenu() {

    console.log('1.show contacts');

    console.log('2.add contact');

    console.log('3.edit contact');

    console.log('4.delete contact');

    console.log('5.search contact');

    console.log('6.sort contact');

    console.log('7.sort phone');

    console.log('8.save and exit');

    // loadData();

    var option = readlineSync.question('> ')

    switch (option) {

        case '1':

            showContacts();

            showMenu();

            break;

        case '2':

            addContact();

            showMenu();

            break;

        case '3':

            editContact();

            showMenu();

            break;

        case '4':

            deleteContact();

            showMenu();

            break;

        case '5':

            searchContact();

            showMenu();

            break;

        case '6':

            sortContact();

            showMenu();

            break;

        case '7':

            sortPhone();

            showMenu();

            break;

        case '8':

            saveAndExit();

            Break;

        default:

            console.log('wrong option');

            break;

    }

}


function addContact() {

    var name = readlineSync.question('What is your name?');

    var phone = readlineSync.question('what is your phone number?');

    var id = uuidv4();

    var contact = new Member(name, phone, id);

    contacts.push(contact);

}


function searchContact() {

    showContacts();

    var nameStr = readlineSync.question('Which name?');

    var phoneNum = readlineSync.question('Which phone?');

    for (var i = 0; i < contacts.length; i++) // remove the =, it will give error array out of bound

    {

        if (!nameStr || !phoneNum) { console.log('invalid Id') }

        else if (contacts[i].name.match(nameStr.toLowerCase().trim())) { console.log(contacts[i]) }

        else if (contacts[i].phone.toString().match(phoneNum.trim())) { console.log(contacts[i]) }

    }

    // var result = contacts.map(contact => {

    //   if(contact.name.includes(nameStr.toLowerCase().trim())){

    //     console.log(contact);

    //   }else if(contact.phone.toString().includes(phoneNum.trim())){

    //     console.log(contact);

    //   }

    // })

}


function editContact() {

    showContacts();

    var nameStr = readlineSync.question('Which name?');

    var phoneNum = readlineSync.question('Which phone?');

    for (var i = 0; i < contacts.length; i++)  // remove the = 

    {

        if (!nameStr || !phoneNum) { console.log('invalid Id') }

        else if (contacts[i].name.match(nameStr.toLowerCase().trim())) { contacts[i].name.split(contacts[i].name).join(nameStr) }

        else if (contacts[i].phone.toString().match(phoneNum.trim())) { contacts[i].phone.toString().split(contacts[i].phone).join(nameStr) }

    }

}


main();


查看完整回答
反對(duì) 回復(fù) 2022-07-08
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

首先:第 116 行必須是{contacts[i.name]}.

那么你的錯(cuò)誤在第 95 行。你從i=0until迭代i <= contacts.length。如果您有 1 個(gè)聯(lián)系人,i=0是您的第一個(gè)聯(lián)系人。i=1那么就沒有價(jià)值了。

所以你必須迭代到 for (var i = 0; i < contacts.length; i++)


查看完整回答
反對(duì) 回復(fù) 2022-07-08
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要警惕contacts空數(shù)組的情況。

空數(shù)組的索引為零將是undefined。嘗試檢查該值的屬性將引發(fā)錯(cuò)誤。

解決方案是在您檢查聯(lián)系人數(shù)組時(shí)設(shè)置一個(gè)警衛(wèi)。

例如:

if(!contacts.length) return


查看完整回答
反對(duì) 回復(fù) 2022-07-08
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)