0

I'm trying to create a small search engine "simulator"(without using any server-side languages) using JavaScript (NOT Node.js).

The idea is that the user is provided with an input field, in which he can type anything. If whatever he types matches ANY of the variables (They will be objects) present in a Javascript array, it will return the variable's(or the object's) properties in an HTML <div> element (div.innerHTML = ...);

The problem is HOW do I do the "any variable in the array"part?

I can use the include() method, but this works only with one expression, not many expressions in an array.

If anyone's got any insight on how to do this I would be grateful. Thanks in advance.

Here's My Code:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link type="text/css" rel="stylesheet" href="contactManager.css" />
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div id="contactDisplay"></div>
    <!--Start of JavaScript Code-->
    <script>
        var displayThem = document.querySelector("#contactDisplay");
    
        class Person {
            constructor(name, phoneNum, address) {
                this.name = name;
                this.phoneNum = phoneNum;
                this.address = address;
            }
            describe() {
                return ("<div>Name: " + this.name + "</div>" +
                    "<div>Phone Number: " + this.phoneNum + "</div>" +
                    "<div>Address: " + this.address + "</div>")
            }
        }
        var John = new Person("John Conway", "1119542742", "4545 Blackwell Street");
        var Micheal = new Person("Micheal Jordan", "1245783367", "3003 Granville Lane");
        var Steve = new Person("Steve Jobs", "1544578332", "2137 Buffalo Creek Road");

        var conatctList = [
            John, Micheal, Steve
        ]

        window.onload = function init() {
            displayThem.innerHTML += John.describe();
        }
    </script>
</body>
</html>
  • You'd use [`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), passing in a function that returns a flag for whether a given entry (which is passed to the function) should be included in the result. See the linked question's answers (and the answers to the many other questions about filtering/searching arrays). – T.J. Crowder Jul 18 '18 at 09:02
  • To search on any property of your contacts you could use filter like so: `var filtered = contactList.filter(function(contactObject) { for(var key of contactObject){if(contactObject[key].toLower() == search.toLower(){return true;}} return false; })` You should assign the value you want to find to the varialbe serach – mortb Jul 18 '18 at 09:07

0 Answers0