在javascript对象数组中查找元素的索引

时间:2017-03-31 00:02:45

标签: javascript

我是javascript的新手,想在底部找到给定键值的索引但不能这样做。我哪里错了?数组中的数据从json文件复制,该文件在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 个答案:

答案 0 :(得分:2)

这就是答案。我试过这个

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

但我同意,这是一个非常糟糕的对象结构。你应该改变json结构,这样你就可以更容易地解析它

更新:你应该尽可能避免创建json对象(以" {"以#34;}结束;"),然后创建json数组(以&#34开头) ; ["以"]"结束。

这是一个更好的结构以及如何解析它

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

答案 1 :(得分:0)

您在示例中寻找的名称更深一层。这应该有效:

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