gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:29:31 25 4
gpt4 key购买 nike

我目前正在为销售代表团队构建一个简单的销售管理工具。我对编程非常陌生,但有足够的 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 个数组买家卖家。我们现在可以迭代买家并搜索匹配的卖家

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

关于javascript - 比较对象内的某些键、值对 - 无直接匹配 (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41004121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com