Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Search products from an array by keywords in javascript

const products = [
    { name: "samsung s3 phone", price: 12000 },
    { name: "asus laptop d34", price: 32000 },
    { name: "apple smart watch", price: 18000 },
    { name: "boshundhara binding paper", price: 80 },
    { name: "lg smart phone", price: 10000 },
    { name: "old granny land phone", price: 100 },
]

function searchProducts(products, searchText) {
    let match = [];
    for (const product of products) {
        if (product.name.indexOf(searchText) != -1) {
            match.push(product);
        }
    }
    return match;
}
const matched = searchProducts(products, "phone");
console.log(matched)

//Output: 
/* [
    { name: 'samsung s3 phone', price: 12000 },
    { name: 'lg smart phone', price: 10000 },
    { name: 'old granny land phone', price: 100 }
] */
Comment

PREVIOUS NEXT
Code Example
Javascript :: concatenation mathematics notation 
Javascript :: date from = to 
Javascript :: javascript escape comma in csv 
Javascript :: insert property to many object with prototype 
Javascript :: get current tab url in chrome extension in popup 
Javascript :: add codegrepper 
Javascript :: usescript +react 
Javascript :: Find speacific object from an array in javascript 
Javascript :: tabindex 
Javascript :: singly linked list create contains 3 nodes insert a node with data 50 
Javascript :: like and dislike function 
Javascript :: Truncate a Stringtarget 
Javascript :: Replacing Specific word from url, replacing url 
Javascript :: scriptable alert 
Javascript :: toast width match parent 
Javascript :: full screen window open and disable all apps by javascript 
Javascript :: Das komplette JavaScript in den Footer laden 
Javascript :: "create a chatbot using javascript only" 
Javascript :: why promise goes to microtask and settimeout to browser api 
Javascript :: chrome extension get current tab 
Javascript :: disable jquery ajax call on init 
Javascript :: get nearest to user location js 
Javascript :: best browser for programmers 
Javascript :: find longest even string in array javascript 
Javascript :: convert text file to string javascript 
Javascript :: javascript activate file input 
Javascript :: cache blogposts for 24 hours react native 
Javascript :: typeorm clear cache 
Javascript :: delete a row in an array react hooks 
Javascript :: date change error 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =