比较Javascript / Jquery

时间:2016-02-25 17:06:25

标签: javascript jquery json steam

我只是创建一个网站,我在其中迭代JSON文件中的项目列表。它看起来像这样:

"prices" : [
{
  "market_hash_name" : "★ Bayonet",
  "price" : 141.04,
  "created_at" : 1455877920
},
{
  "market_hash_name" : "AWP | Pit Viper (Minimal Wear)",
  "price" : "1.83",
  "created_at" : 1455878005
}
.
.
. and so on

现在,我已经成功地搜索并选择了价格"通过" market_hash_name"使用$ ajax和$ jquery组合的每个函数。这是:

$.ajax({url: "../json/priceapi.json", success: function(result){  // Get JSON File

$.each(result.prices, function(i, v) {    
   if (v.market_hash_name == market_hash_name) { ... // Parse JSON Data

一切正常,代码似乎没问题,但问题是,在这个巨大的JSON文件中的任何地方都是完全相同的第二个" market_hash_name"但附加一个文字部分。一个例子:

{
  "market_hash_name" : "Souvenir AWP | Pit Viper (Minimal Wear)",                            
  "price" : "102.18",   // Additional text "Souvenir"
  "created_at" : 1455878414
},

由于这个"双标签" $ each函数告诉我,一个键有两个结果。我只想得到确切的结果。你有什么想法怎么做?有没有办法做这样的事情?

$.each(result.prices, function(i, v) {    
  if (v.market_hash_name - "Souvenir" == market_hash_name) { ...

1 个答案:

答案 0 :(得分:0)

感谢briosheje! https://jsfiddle.net/briosheje/f68wfet6/1/效果很好!需要一点,但我的工作:)

var res = {
    "prices": []
};

$.each(json.prices, function(i, e) {
    var exists = false;
  $.each(res.prices, function (c, f){
    console.log(f.market_hash_name + " <> " + e.market_hash_name);
    console.log(f.market_hash_name == e.market_hash_name);
    if (("Souvenir " + f.market_hash_name) == e.market_hash_name) {
        exists = true;
    }
  });

  !exists && res.prices.push(e);
});

console.log(res);