Reputation: 5
I am currently building a simple sales management tool for the sales reps team. I am very extremely new to programming but have enough knowledge in JavaScript to understand the basic concepts and whatnot.
The main functionality of the app is punching in the price, color, manufacturer, type of opportunity, condition and so on through a form. What I want to achieve right now is to compare key, value pairs inside an object which is nested inside an array because I am ultimately trying to compare the type of opportunity (for example, a buyer wanting to pay phones at a certain price would be matched with a seller wanting to sell phones at a certain price. If the buyer is looking to pay $100 and the seller is looking to sell for $100, then it would be a price match. I am not looking for direct matches but simply matching a few conditions I have defined in my models within the MEAN stack.
Here is the snippet of the JSON object I have created through a simple API call from the backend to the frontend. I have been looking around on stackoverflow and Google as in how to compare objects and array but most of them (if not all) have been showing ways to render a direct match between two arrays, two objects and etc. I want to be able to compare only some of the key, value pairs within my object.
I know this is a long question and I can definitely provide more clarification if needed. I am pretty desperate and have been thinking my brains out for this. Any help would be great, thank you!
[
{
"_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
}
]
Upvotes: 0
Views: 1447
Reputation: 63
This function will take in two parameters, first, what you want to compare (eg' "price") and second, your array of data. So if your array of data is named dataArray and you want to compare by price, you would run
results = compare("price", dataArray)
And results will contain an object which lists each different price, and the buyers and sellers for it, as long as there is at least one buyer and seller for that price.
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
}
Upvotes: 0
Reputation: 601
First. Let's separate our sellers from the buyers by filtering each group.
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'});
Now we have 2 arrays buyers
and sellers
. We can now iterate the buyers and search for matching sellers
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.
}
})
})
Upvotes: 1