I am new to javascript and want to find the index of the given key-value at the bottom but not able to do so. Where am i wrong? The data in the array is copied form a json file which is valid as checked on jsonlint.
var productArray=[
{
"name1":"Electronics",
"id1":{
"products1":{
"id":1.1,
"name":"Microsoft Keyboard",
"description":"good keyboard",
"rating":3,
"price":500,
"freeDeliv":true,
"seller":"MS",
"quanAvl":10
},
"products2":{
"id":1.2,
"name":"ASUS phone",
"description":"good phone",
"rating":4,
"price":10000,
"freeDeliv":true,
"seller":"ASUS",
"quanAvl":10
},
"products3":{
"id":1.3,
"name":"iPhone",
"description":"good phone",
"rating":3,
"price":50000,
"freeDeliv":false,
"seller":"Apple",
"quanAvl":100
}
},
"name2":"Clothing",
"id2":{
"products4":{
"id":2.1,
"name":"Jeans",
"description":"good Jeans",
"rating":3,
"price":800,
"freeDeliv":true,
"seller":"Levis",
"quanAvl":100
},
"products5":{
"id":2.2,
"name":"TShirt",
"description":"good TShirt",
"rating":4,
"price":1000,
"freeDeliv":true,
"seller":"Peter",
"quanAvl":1000
},
"products6":{
"id":2.3,
"name":"Sherwani",
"description":"very good",
"rating":4,
"price":50000,
"freeDeliv":false,
"seller":"Maanyavar",
"quanAvl":1000
}
}},
];
var display=function(productArray,prodKey,value){
for(x in productArray)
{
if(productArray[x][prodKey]==value)
{
console.log(x);
}
else{
alert("Not Found");
}
}
}
display(productArray,"name","Sherwani");
productArray[x].name = 'Sherwani'but the value is atproductArray[x].id2.products6.name. Note the extra depth.productArrayobject looks really wrong, please learn what is an array, and how to represent it in an object first.productArray.lengthis1Object.keys(...)will help you iterate over your object. Look it up