Horje
Search products from an array by keywords in javascript Code Example
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 }
] */




Javascript

Related
capacitorjs get zip code example Code Example capacitorjs get zip code example Code Example
useLinkPressHandler Code Example useLinkPressHandler Code Example
why array.map returns undefined Code Example why array.map returns undefined Code Example
javascript refresh function every 5 seconds Code Example javascript refresh function every 5 seconds Code Example
vue date helper Code Example vue date helper Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8