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");
2 Answers 2
this is the answer. I have tried this
var display=function(productArray_,prodKey,value){
p = productArray_[0]["id2"];
found = false;
for (var key in p) {
if (p.hasOwnProperty(key)) {
p2 = p[key];
for(var key2 in p2){
if(p2.hasOwnProperty(key2)){
if(key2 == prodKey && p2[key2] == value){
console.log(key2); //index of value you looking for
console.log(p2[key2]) //value from index you looking for
alert(key2);
found = true;
}
}
}
}
}
if(!found)
alert("not found");
}
display(productArray,"name","Sherwani");
but I'm agree, this is a very bad object structure. you should change the json structure so you can parse it more easily
UPDATE: you should avoid creating json object wherever possible (begins with "{" and ends with "}"), and instead create json array (begins with "[" and ends with "]").
here is a better structure and how to parse it
var productArray=[
{
"name": "Electronics",
"list": [
{
"code": "products1",
"id": 1.1,
"name": "Microsoft Keyboard",
"description": "good keyboard",
"rating": 3,
"price": 500,
"freeDeliv": true,
"seller": "MS",
"quanAvl": 10
},
{
"code": "products2",
"id": 1.2,
"name": "ASUS phone",
"description": "good phone",
"rating": 4,
"price": 10000,
"freeDeliv": true,
"seller": "ASUS",
"quanAvl": 10
},
{
"code": "products3",
"id": 1.3,
"name": "iPhone",
"description": "good phone",
"rating": 3,
"price": 50000,
"freeDeliv": false,
"seller": "Apple",
"quanAvl": 100
}
]
},
{
"name": "Clothing",
"list": [
{
"code": "products4",
"id": 2.1,
"name": "Jeans",
"description": "good Jeans",
"rating": 3,
"price": 800,
"freeDeliv": true,
"seller": "Levis",
"quanAvl": 100
},
{
"code": "products5",
"id": 2.2,
"name": "TShirt",
"description": "good TShirt",
"rating": 4,
"price": 1000,
"freeDeliv": true,
"seller": "Peter",
"quanAvl": 1000
},
{
"code": "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){
found = false;
for(var key in productArray_){
for(var key2 in productArray_[key]){
if(key2 == "list"){
for(var key3 in productArray_[key][key2]){
for(var key4 in productArray_[key][key2][key3]){
if(key4 == prodKey
&& productArray_[key][key2][key3][key4] == value
){
console.log(key4);
console.log(productArray_[key][key2][key3][key4]);
alert(key4);
found = true;
}
}
}
}
}
}
if(!found)
alert("not found");
}
display(productArray,"name","Sherwani");
7 Comments
Name you are looking for in your example is one level deeper. This should work:
var display = function(productArray, prodKey, value){
for (x in productArray) {
for (prod in productArray[x]) {
if (productArray[x][prod][prodKey] == value) {
console.log(x);
} else {
alert("Not Found");
}
}
}
}
productArray[x].name = 'Sherwani'
but the value is atproductArray[x].id2.products6.name
. Note the extra depth.productArray
object looks really wrong, please learn what is an array, and how to represent it in an object first.productArray.length
is1
Object.keys(...)
will help you iterate over your object. Look it up