通过object属性循环遍历对象数组

时间:2017-06-02 11:37:54

标签: javascript arrays object

我有一个对象数组,其中包含一些我想要触发的pop的信息。它看起来像这样:

[  
   {  
      "url":"www.fakeurl/redirect1",
      "providerName":"ebay",
      "retailerName":"eBay",
      "providerAndRetailer":"ebayeBay",
      "popAllowed":true
   },

   {  
      "url":"www.fakeurl/redirect2",
      "providerName":"pricerunner",
      "retailerName":"Currys",
      "providerAndRetailer":"pricerunnerCurrys",
      "popAllowed":true
   },
   {  
      "url":"www.fakeurl/redirect3",
      "providerName":"kelkoo",
      "retailerName":"Currys",
      "providerAndRetailer":"kelkooCurrys",
      "popAllowed":true
   },
   {  
      "url":"www.fakeurl/redirect4",
      "providerName":"ebay",
      "retailerName":"eBay",
      "providerAndRetailer":"ebayeBay",
      "popAllowed":true
   },
   {  
      "url":"www.fakeurl/redirect5",
      "providerName":"pricerunner",
      "retailerName":"Argos",
      "providerAndRetailer":"pricerunnerArgos",
      "popAllowed":true
   },
   {  
      "url":"www.fakeurl/redirect6",
      "providerName":"ebay",
      "retailerName":"eBay",
      "providerAndRetailer":"ebayeBay",
      "popAllowed":true
   },
   {  
      "url":"www.fakeurl/redirect7",
      "providerName":"ecn",
      "retailerName":"Argos",
      "providerAndRetailer":"ecnArgos",
      "popAllowed":true
   }
]

我还有一系列提供商,我希望在其中触发订单:

let acceptedProviders = ['ebay', 'connexity', 'ecn', 'kelkoo', 'nexttag', 'pricerunner', 'shopping'];

我想按照我接受的提供商的顺序循环我的数组。当我在对象中找到与“providerName”属性匹配时,我希望循环停止并添加“popAllowed”属性:false,这样我就可以将其从列表中过滤掉。然后它会再次循环,仍然在我接受的提供者数组中查找名字,如果它没有找到它,它会移动到下一个名称,直到列表用完为止。我试过一个for循环,但我似乎无法得到我想要的结果。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:0)

通过在接受的提供者数组中查找该提供者来循环遍历数据并在每个迭代集popAllowed中

let acceptedProviders = ['ebay', 'connexity', 'ecn', 'kelkoo', 'nexttag', 'pricerunner', 'shopping'];
let data = get_data();

data.forEach((item)=>{
  item.popAllowed = acceptedProviders.indexOf(item.providerName) === -1;
});

console.log(data);

function get_data() {
  return [{
      "url": "www.fakeurl/redirect1",
      "providerName": "ebay",
      "retailerName": "eBay",
      "providerAndRetailer": "ebayeBay",
      "popAllowed": true
    },

    {
      "url": "www.fakeurl/redirect2",
      "providerName": "pricerunner",
      "retailerName": "Currys",
      "providerAndRetailer": "pricerunnerCurrys",
      "popAllowed": true
    },
    {
      "url": "www.fakeurl/redirect3",
      "providerName": "kelkoo",
      "retailerName": "Currys",
      "providerAndRetailer": "kelkooCurrys",
      "popAllowed": true
    },
    {
      "url": "www.fakeurl/redirect4",
      "providerName": "ebay",
      "retailerName": "eBay",
      "providerAndRetailer": "ebayeBay",
      "popAllowed": true
    },
    {
      "url": "www.fakeurl/redirect5",
      "providerName": "pricerunner",
      "retailerName": "Argos",
      "providerAndRetailer": "pricerunnerArgos",
      "popAllowed": true
    },
    {
      "url": "www.fakeurl/redirect6",
      "providerName": "ebay",
      "retailerName": "eBay",
      "providerAndRetailer": "ebayeBay",
      "popAllowed": true
    },
    {
      "url": "www.fakeurl/redirect7",
      "providerName": "ecn",
      "retailerName": "Argos",
      "providerAndRetailer": "ecnArgos",
      "popAllowed": true
    }
  ];
}



//console.log(data)