简体   繁体   English

我可以从 JavaScript 中的字符串为关联数组构建索引吗?

[英]Can I build an index to an associative array from a string in JavaScript?

In my JavaScript code I have an associative (two dimension) array ( myObj[x][y] ), that has in every row n elements (I know n for every row - different in every row) where the first value show the amount (n) like在我的 JavaScript 代码中,我有一个关联(二维)数组( myObj[x][y] ),它在每一行都有 n 个元素(我知道每一行的 n - 每行不同),其中第一个值显示数量(n) 喜欢

{"amount"=>"n", "key0"=>"value0","key1"=>"value1"..."keyn"=>"valuen"}

In a loop I need to access all these values like在一个循环中,我需要访问所有这些值,例如

for (i=o; i=n-1;i++){
  current=myObj[row-x].key-y; //code to do something with current
}

How can I build this index .key-y so I can access values?如何构建此索引.key-y以便我可以访问值? (If I create a string like "myObj[row-x].key-"+ i.toString()}" (where that construct the index as literal) can be done in some way? (如果我创建一个像"myObj[row-x].key-"+ i.toString()}"类的字符串(将索引构造为文字)可以以某种方式完成?

JSON ``` JSON```

"[{\"object_type\":\"villa\",\"object_name\":\"Villa at Maries\",\"object_id\":\"1\",\"mainimg\":\"images\/maries\/IMG1.jpg\",\"object_descreption\":\"BLA BLA BLA.\",\"smallimg0\":\"images\/maries\/img1f.jpg\",\"smallimg1\":\"images\/maries\/img1a.jpg\",\"smallimg2\":\"images\/maries\/img1b.jpg\",\"smallimg3\":\"images\/maries\/img1c.jpg\",\"amencount\":4,\"amenitie0\":\"A\/C\",\"amenitie1\":\"Kitchen\",\"amenitie2\":\"Wi Fi\",\"amenitie3\":\"Parking\"},{\"object_type\":\"villa\",\"object_name\":\"Villa at Agia Marina\",\"object_id\":\"2\",\"mainimg\":\"images\\agiamarina\\img1.jpg\",\"object_descreption\":BLA BLA BLA\",\"smallimg0\":\"images\/agiamarina\/img1.jpg\",\"smallimg1\":\"images\/agiamarina\/img2.jpg\",\"smallimg2\":\"images\/agiamarina\/img3.jpg\",\"smallimg3\":\"images\/agiamarina\/img4.jpg\",\"amencount\":3,\"amenitie0\":\"A\/C\",\"amenitie1\":\"Wi Fi\",\"amenitie2\":\"Parking\"},{\"object_type\":\"villa\",\"object_name\":\"Villa at Argasi\",\"object_id\":\"6\",\"mainimg\" "[{\"object_type\":\"villa\",\"object_name\":\"Maries 别墅\",\"object_id\":\"1\",\"mainimg\":\"images \/maries\/IMG1.jpg\",\"object_descreption\":\"BLA BLA BLA.\",\"smallimg0\":\"images\/maries\/img1f.jpg\",\"smallimg1\ ":\"images\/maries\/img1a.jpg\",\"smallimg2\":\"images\/maries\/img1b.jpg\",\"smallimg3\":\"images\/maries\/ img1c.jpg\",\"amencount\":4,\"amenitie0\":\"A\/C\",\"amenitie1\":\"厨房\",\"amenitie2\":\"Wi Fi\",\"amenitie3\":\"Parking\"},{\"object_type\":\"villa\",\"object_name\":\"Villa at Agia Marina\",\"object_id\" :\"2\",\"mainimg\":\"images\\agiamarina\\img1.jpg\",\"object_descreption\":BLA BLA BLA\",\"smallimg0\":\"images\/ agiamarina\/img1.jpg\",\"smallimg1\":\"图片\/agiamarina\/img2.jpg\",\"smallimg2\":\"图片\/agiamarina\/img3.jpg\",\ "smallimg3\":\"图像\/agiamarina\/img4.jpg\",\"amencount\":3,\"amenitie0\":\"A\/C\",\"amenitie1\":\" Wi Fi\",\"amenitiie2\":\"Parking\"},{\"object_type\":\"villa\",\"object_name\":\"Villa at Argasi\",\"object_id\" :\"6\",\"mainimg\" :\"images\/argasi\/img1.jpg\",\"object_descreption\":\"BLA BLA BLA\",\"smallimg0\":\"images\/argasi\/img1.jpg\",\"smallimg1\":\"images\/argasi\/img2.jpg\",\"smallimg2\":\"images\/argasi\/img3.jpg\",\"smallimg3\":\"images\/argasi\/img4.jpg\"}]"``` :\"images\/argasi\/img1.jpg\",\"object_descreption\":\"BLA BLA BLA\",\"smallimg0\":\"images\/argasi\/img1.jpg\",\ "smallimg1\":\"图像\/argasi\/img2.jpg\",\"smallimg2\":\"图像\/argasi\/img3.jpg\",\"smallimg3\":\"图像\/ argasi\/img4.jpg\"}]"```

Thank you all.. got my answer.. so simple and so silly me !!!!!!!!谢谢大家..得到我的答案..这么简单,我太傻了!!!!!!!!!

Sure.当然。 Something like this will loop through all your elements:像这样的东西会循环遍历你的所有元素:

var rowAmount;
for (row=0; row < myObj.length; row++) {
    rowAmount = myObj[row].amount;
    for (key=0; key < myObj[row].length - 1; key++) {
        currentValue = myObj[row]["key" + key];
        //code to do something with currentValue and rowAmount
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM