0

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");

3
  • 1
    The data is more deeply nested than your search. You're looking for productArray[x].name = 'Sherwani' but the value is at productArray[x].id2.products6.name. Note the extra depth. Commented Mar 31, 2017 at 0:22
  • 1
    Your productArray object looks really wrong, please learn what is an array, and how to represent it in an object first. productArray.length is 1 Commented Mar 31, 2017 at 0:22
  • Object.keys(...) will help you iterate over your object. Look it up Commented Mar 31, 2017 at 0:58

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

7 Comments

Even i agree its a bad structure, but i'm out of ideas. This is supposed to be a simple json structure of an e-commerce site. What should i do?
I will give you my suggestion in about 4 hours from now. I must get to work now
I just update my answer with suggestion for json structure in it
Wow, much thnx. Im very early in JS so this was lifesaving.
Hey @Dika , i'm getting -1 whenever i try to find the index of the same value. Please help me out. Tried mapping,indexOf but something's wrong.
|
0

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");
            }
        }
    }
}

Comments