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

时间:2016-12-06 20:19:32

标签: javascript arrays json javascript-objects

我目前正在为销售代表团队构建一个简单的销售管理工具。我对编程非常陌生,但对JavaScript有足够的知识来理解基本概念和诸如此类的东西。

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

以下是我通过从后端到前端的简单API调用创建的JSON对象的片段。我一直在关注stackoverflow和谷歌,如何比较对象和数组,但大多数(如果不是全部)已经显示出渲染两个数组,两个对象等之间的直接匹配的方法。我希望能够仅比较我对象中的一些键值对

我知道这是一个很长的问题,如果需要,我绝对可以提供更多说明。我非常绝望,一直在考虑我的大脑。任何帮助都会很棒,谢谢!

[
{
"_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
}
]

2 个答案:

答案 0 :(得分:1)

首先。让我们通过筛选每个组来区分我们的卖家和买家。

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.
    }
  })
})

答案 1 :(得分:0)

此功能将包含两个参数,第一个是您要比较的内容(例如“价格”),第二个是您的数据数组。因此,如果您的数据数组名为dataArray,并且您希望按价格进行比较,则可以运行

<script>
    $(document).ready(function(){
        var navTop = $('.nav').position().top; // returns and assigns the offset top of the nav bar

        $(window).bind('scroll', function() {
            if($(window).scrollTop() >= navTop) { // condition met if the scroll top value is greater than or equal to the offset top of the nav bar
                $('nav').addClass('fixed');
            }
            else if($(window).scrollTop() < navTop) { //condition met if the scroll top value is only lower than the offset top of the nav bar
                $('nav').removeClass('fixed');
            }
        });
    });
</script>

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

results = compare("price", dataArray)