Priya Mishra
Priya Mishra

Reputation: 1

how to find a particular entry from json store using id/name

how to find a particular entry from json store using id/name? using javascript function

for eg:

var products=[{
    "productId": 101,
    "productName": "Nilkamal Shoe Rack",
    "productPrice": 2223,
    "productOriginalPrice": 2470,
    "productDiscount": "20 %",
    "productColor": "Sandy Brown",
    "productSpecifications": [{
        "Brand": "Nilakamal",
        "Type": "Shoe Rack",
        "Material": "Polypropylene"
    }],
    "productServices": ["Installation and Demo not Required", "10 Days Replacement"]
},

{
    "productId": 102,
    "productName": "Seagate Backup Plus Slim 1TB wired external hard disk",
    "productPrice": 3899,
    "productOriginalPrice": 7450,
    "productDiscount": "47 %",
    "productColor": "White",
    "productSpecifications": [{
        "Brand": "Seagate",
        "Type": "External Hard Disk",
        "Capacity": "1 TB",
        "Connectivity": "USB 3.0",
        "Ports": "1 Port"
    }],
    "productServices": ["3 years warranty", "10 Days Replacement"]
}]

How can I find all the details of one of the following product using id/name?

Upvotes: 0

Views: 46

Answers (1)

NiravAdatiya
NiravAdatiya

Reputation: 334

you can simply use array.find like this

 var myObj = products.find(obj => obj.productId == 102);
 console.log(myObj);

if you want function then

function getObjbyId(id){
     var myObj = products.find(obj => obj.productId == id);
     return myObj;
}

Upvotes: 1

Related Questions