Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript对象-如何正确访问项并对其排序_Javascript_List_Object - Fatal编程技术网

Javascript对象-如何正确访问项并对其排序

Javascript对象-如何正确访问项并对其排序,javascript,list,object,Javascript,List,Object,我在javascript中有一个对象,其中有一个属性列表。看起来是这样的: var PRICES = {}; PRICES["1/1"] = { "price_unit" : "0 €", "price_total": "0 €", "price_total_vat" : "0 €" }; PRICES["6/1"] = { "price_unit" : "50,00 €", "price_total": "300,00 €", "price_total_vat" : "357,00 €

我在javascript中有一个对象,其中有一个属性列表。看起来是这样的:

var PRICES = {};

PRICES["1/1"] = { "price_unit" : "0 €", "price_total": "0 €", "price_total_vat" : "0 €" };   
PRICES["6/1"] = { "price_unit" : "50,00 €", "price_total": "300,00 €", "price_total_vat" : "357,00 €" };  
PRICES["12/1"] = { "price_unit" : "40,00 €", "price_total": "480,00 €", "price_total_vat" : "571,20 €" }; 
PRICES["24/1"] = { "price_unit" : "33,00 €", "price_total": "792,00 €", "price_total_vat" : "942,48 €" };  
PRICES["1/3"] = { "price_unit" : "94,99 €", "price_total": "284,97 €", "price_total_vat" : "339,11 €" };  
PRICES["6/3"] = { "price_unit" : "47,50 €", "price_total": "855,00 €", "price_total_vat" : "1.017,45 €" }; 
PRICES["12/3"] = { "price_unit" : "38,00 €", "price_total": "1.368,00 €", "price_total_vat" : "1.627,92 €" }; 
PRICES["24/3"] = { "price_unit" : "31,35 €", "price_total": "2.257,20 €", "price_total_vat" : "2.686,07 €" }; 
PRICES["1/10"] = { "price_unit" : "90,24 €", "price_total": "902,40 €", "price_total_vat" : "1.073,86 €" };
PRICES["6/10"] = { "price_unit" : "45,12 €", "price_total": "2.707,20 €", "price_total_vat" : "3.221,57 €" };
PRICES["12/10"] = { "price_unit" : "36,10 €", "price_total": "4.332,00 €", "price_total_vat" : "5.155,08 €" };
PRICES["24/10"] = { "price_unit" : "29,78 €", "price_total": "7.147,20 €", "price_total_vat" : "8.505,17 €" }; 
PRICES["1/50"] = { "price_unit" : "85,73 €", "price_total": "4.286,50 €", "price_total_vat" : "5.100,94 €" };
PRICES["6/50"] = { "price_unit" : "42,86 €", "price_total": "12.858,00 €", "price_total_vat" : "15.301,02 €" }; 
PRICES["12/50"] = { "price_unit" : "34,29 €", "price_total": "20.574,00 €", "price_total_vat" : "24.483,06 €" };
我试图访问每个列表中名为
price\u unit
的第一项,但我不确定如何访问。我通过以下途径获得object的密钥:

for (var key in PRICES) {
    $("ul.first").append("<li>"+ key +"</li>");
}
for(风险值输入价格){
$(“ul.first”)。追加(“
  • ”+键+”
  • ”; }
    但我的试价单位是:

    for (var key in PRICES) {
        $("ul.second").append("<li>"+ PRICES.price_unit +"</li>");
    }
    
    for(风险值输入价格){
    $(“ul.second”)。追加(“
  • ”+PRICES.price\u unit+”
  • ”; }
    返回未定义。什么是正确的方法

    我还想知道如何首先按键对这些列表进行排序。我很清楚如何对它们进行排序,以便它们的键按如下顺序出现在列表中:“1/1、1/3、1/10、6/1、6/3、6/10…”等等

    您可以在此处查看和编辑我的JSFIDLE:

    for(var输入价格){
    $(“ul.second”)。追加(“
  • ”+价格[key]。价格单位+”
  • ”; }

    下面是一个如何进行排序的示例:

    PRICES[key]。price\u unit
    这么简单吗?非常感谢。只是想弄清楚如何以正确的方式对它们进行排序。。。我会尽快接受你的答复
    for (var key in PRICES) {
        $("ul.second").append("<li>"+ PRICES[key].price_unit +"</li>");
    }