繁体   English   中英

比较对象中的某些键,值对-无直接匹配(JavaScript)

[英]Comparing certain key, value pairs within an object - No Direct Matches (JavaScript)

我目前正在为销售代表团队构建一个简单的销售管理工具。 我对编程非常陌生,但是对JavaScript有足够的了解,可以理解基本概念。

该应用程序的主要功能是通过表格输入价格,颜色,制造商,机会类型,条件等。 我现在想要实现的是比较嵌套在数组内的对象内的键,值对,因为我最终试图比较机会的类型(例如,想要以一定价格付款的买方可能会与希望以某个价格出售电话的卖方匹配。如果买方希望支付100美元,而卖方希望以100美元出售, 那将是一个价格匹配。我不是在寻找直接匹配,而是简单地匹配一个我在MEAN堆栈中的模型中定义的几个条件。

这是我通过从后端到前端的简单API调用创建的JSON对象的代码段。 我一直在研究stackoverflow和Google,就像如何比较对象和数组一样,但是其中大多数(如果不是全部)一直在展示呈现两个数组,两个对象等之间直接匹配的方法。我希望能够仅比较对象中的一些键,值对

我知道这是一个漫长的问题,如果需要,我绝对可以提供更多说明。 我非常绝望,为此一直在思考。 任何帮助将是巨大的,谢谢!

[
{
"_id": "583e77e4be1fb20bce420ca1",
"created_at": "2016-11-30T06:55:32.291Z",
"updated_at": "2016-11-30T06:55:32.291Z",
"model": "6S Plus",
"storage": "32GB",
"condition": "New",
"color": "Rose Gold",
"country": "Hong Kong",
"quantity": 200,
"price": 140,
"salesRep": "Ernie",
"type": "Seller",
"carrier": "Locked",
"__v": 0
},
{
"_id": "583e7ab02da4470dc1b2d2ae",
"created_at": "2016-11-30T07:07:28.019Z",
"updated_at": "2016-11-30T07:07:28.019Z",
"model": "5S",
"storage": "64GB",
"condition": "Like New",
"color": "Space Grey",
"country": "India",
"quantity": 203,
"price": 120,
"salesRep": "Ernie",
"type": "Buyer",
"carrier": "Locked",
"__v": 0
},
{
"_id": "583e86681a670110db9d7587",
"created_at": "2016-11-30T07:57:28.765Z",
"updated_at": "2016-11-30T07:57:28.765Z",
"manufacturer": "Apple",
"model": "7",
"storage": "128GB",
"condition": "New",
"color": "New",
"country": "United States",
"quantity": 300,
"price": 530,
"salesRep": "Emil",
"type": "Buyer",
"carrier": "AT&T",
"__v": 0
},
{
"_id": "583e86f9d3a5bb11984fcb44",
"created_at": "2016-11-30T07:59:53.950Z",
"updated_at": "2016-11-30T07:59:53.950Z",
"manufacturer": "Samsung",
"model": "Galaxy S7",
"storage": "64GB",
"condition": "Like New",
"color": "Black Onyx",
"country": "Hong Kong",
"quantity": 140,
"price": 340,
"salesRep": "Robert",
"type": "Seller",
"carrier": "Verizon",
"__v": 0
},
{
"_id": "583f2113ff9cf5134bb39a66",
"created_at": "2016-11-30T18:57:23.214Z",
"updated_at": "2016-11-30T18:57:23.214Z",
"manufacturer": "Apple",
"model": "5S Plus",
"storage": "32GB",
"condition": "Refurbished",
"color": "Rose Gold",
"country": "Hong Kong",
"quantity": 500,
"price": 450,
"salesRep": "Zee",
"type": "Seller",
"carrier": "AT&T",
"__v": 0
},
{
"_id": "5845e8f827841a30e813bde8",
"created_at": "2016-12-05T22:23:52.123Z",
"updated_at": "2016-12-05T22:23:52.123Z",
"manufacturer": "Apple",
"model": "7",
"storage": "128GB",
"condition": "New",
"color": "Space Grey",
"country": "Hong Kong",
"quantity": 500,
"price": 760,
"salesRep": "Zee",
"type": "Buyer",
"carrier": "Verizon",
"__v": 0
},
{
"_id": "5846f8e2133d170c7435b6ea",
"created_at": "2016-12-06T17:44:02.126Z",
"updated_at": "2016-12-06T17:44:02.126Z",
"manufacturer": "Apple",
"model": "6S",
"storage": "64GB",
"condition": "New",
"color": "Rose Gold",
"country": "United States",
"quantity": 200,
"price": 340,
"salesRep": "Emil",
"type": "Seller",
"carrier": "Unlocked",
"__v": 0
},
{
"_id": "5846f90d133d170c7435b6eb",
"created_at": "2016-12-06T17:44:45.880Z",
"updated_at": "2016-12-06T17:44:45.880Z",
"manufacturer": "Apple",
"model": "6S",
"storage": "64GB",
"condition": "New",
"color": "Rose Gold",
"country": "United States",
"quantity": 200,
"price": 340,
"salesRep": "Ernie",
"type": "Buyer",
"carrier": "Unlocked",
"__v": 0
}
]

第一。 让我们通过筛选每个组将卖方与买方分开。

var data = [{...}] // assume is the long list of data you posted
var buyers = data.filter(function(item) {return item.type === 'Buyer'});
var sellers = data.filter(function(item) {return item.type === 'Seller'});

现在我们有2个阵列的buyerssellers 我们现在可以迭代买家并搜索匹配的卖家

buyers.forEach(function(buyer) {
  sellers.forEach(function(seller) {
    // Here we can compare our buyers and sellers.
    // For each buyer we'll iterate over all the sellers and look for a match.
    if (buyer.price >= seller.price) {
      // You've found a match! now do something with it.
      // Of course here we are comparing only price, you may want to compare
      // multiple keys, like if it's the same product.
    }
  })
})

此函数将包含两个参数,首先是您要比较的参数(例如“价格”),其次是数据数组。 因此,如果您的数据数组名为dataArray,并且要按价格进行比较,则可以运行

results = compare("price", dataArray)

结果将包含一个对象,该对象列出每个不同的价格以及该价格的买方和卖方,只要该价格至少有一个买方和卖方即可。

var compare = function(whatToCompare, data) {
  var results = {}
  for (var i = 0; i < data.length; i++) {
    if (!results[data[i][whatToCompare]]) {
      results[data[i][whatToCompare]] = []
    }
    results[data[i][whatToCompare]].push(data[i])
  }
  for (var entry in results) {
    if (results[entry].length < 2){
      delete results[entry]
    } else {
      var buyer = false
      var seller = false
      for (var j = 0; j < results[entry].length; j++){
        if (results[entry][j]["type"] === "Seller"){
          seller = true
        }
        if (results[entry][j]["type"] === "Buyer"){
          buyer = true
        }
      }
      if (buyer === false || seller === false){
        delete results[entry]
      }
    }
  }
  return results
}

暂无
暂无

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

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