Merge collection instantly and make a new collection with grouping - javascript

Currently using MongoDB Atlas for a Timesheet system. I have 4 collections in it:
category (has the record of clock in types and staff roles)
employees (has the record of the staff name and a unique pin)
timesheets (has the record of a Pin with the clock in type and date and time)
users (Admin login details)
Data of employees is as follows:
{
"_id": {
"$oid": "62cf98457"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"site": "Port Melbourne",
"email": "test#gmail.com",
"phone": "999 999 999",
"dob": "2022-07-10",
}
Data of timesheet is as follows:
{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "in",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"_id": {
"$oid": "62cf98e47caaa2ac70fba490"
},
"pin": "1234",
"type": "out",
"date": "18-01-2022",
"time": "Tuesday, January 18, 2022 4:25 PM",
"image": "image file url"
}
For now I am querying the employees collection if pin exits or not and if it does filtering in the timesheets collection which has same pin values again filtering them by date and then by types with time to get all employees timesheets details is a list with date.
This process is taking long for a query.
Can we directly create a new collections example employees_timesheets which will group user details and create an object inside filtering dates from timesheets collection.
Example:
{
"_id": {
"$oid": "62"
},
"name": "Name Test",
"pin": "1234",
"role": "Sales",
"date": "18-01-2022",
{
"18-01-2022": [{
"type": "in",
"time": "Tuesday, January 18, 2022 6:25 AM",
"image": "image file url"
},{
"type": "out",
"time": "Tuesday, January 18, 2022 4:25 AM",
"image": "image file url"
}]
}
}

Related

MongoDB Aggregation - Field must be a accumulator object in group stage

I am new to mongodb aggregation. Let's say I have this data:
[
{
"end_year": 2020,
"intensity": 12,
"sector": "Information Technology",
"topic": "robot",
"insight": "62 market research reports study robotics industry",
"url": "http://robohub.org/62-market-research-reports-study-robotics-industry/",
"region": "",
"start_year": 2016,
"impact": "",
"added": "July, 16 2016 02:44:49",
"published": "July, 11 2016 00:00:00",
"country": "",
"relevance": 3,
"pestle": "Economic",
"source": "Robothub",
"title": "Forecasts the industrial robotics market in APAC to grow at a CAGR of 8.7% during the period 2016-2020 in the top 5 segments.",
"likelihood": 4
},
{
"end_year": "",
"intensity": 2,
"sector": "Government",
"topic": "government",
"insight": "Oil, Greed, and Grievances in the Middle East and North Africa",
"url": "https://www.newsecuritybeat.org/2016/07/oil-greed-grievances-middle-east-north-africa/",
"region": "",
"start_year": "",
"impact": "",
"added": "July, 16 2016 01:12:21",
"published": "July, 12 2016 00:00:00",
"country": "",
"relevance": 1,
"pestle": "Political",
"source": "New Security Beat",
"title": "Countries threatened by larger-scale insurgencies might want to consider giving regional groups some share in central government decision-making.",
"likelihood": 2
}
]
This is my query:
// return all the countries associated with a single topic
const data = await Data.aggregate([
{ $match: {} },
{ $group: { _id: "$topic",country: ["$country"],likelihood: ["$likelihood"],relevance: ["$relevance"],intensity: ["$intensity"] } }
])
I want to return all countries,likelihoods,relevances associated with a single/common topic
I know how to get the first and last ones using $first and $last operators. How do I get all of them in an array?
We can use $push operator to add all of the field values:
const data = await Data.aggregate([
{ $match: {} },
{ $group: { _id: "$topic",data: { $push: { country: "$country",likelihood: "$likelihood",relevance: "$relevance",intensity: "$intensity" } } } }
])

How to query data with and conditions in mongoose

I have a document of transactions where a transaction can be of different shops.
I want to query transactions with certain id in certain date. I have a field for date and id which is a MongoDB id, when I query using
Transaction.find({ $and: [{ date: nepali_date.day, shop_id: user.shop_id }] }).lean()
It returns data of all the transaction with the date given, I don't want that, I want the data of date 14 AND shop id "61f3c70c89e5453d271e0ea7"
but instead it returns:
{
"_id": "61f3c81a1410f50644d981b6",
"year": 2078,
"month": 9,
"date": 14,
"title": "Krus",
"amount": 60,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61f3c70c89e5453d271e0ea7",
"edited": false
},
{
"_id": "61f3c89ce0ea4276ddba693f",
"year": 2078,
"month": 9,
"date": 14,
"title": "RAJ",
"amount": 50,
"type": "income",
"author": "Eclipsu",
"authorID": "61ef7e9565a4a88874937e96",
"shopID": "61ef7ea165a4a88874937e9d",
"edited": false
}```

RethinkDB indexing two fields with time

I have RethinkDB with data/table let say "news" with huge number of data row :
[
{
"author": "author1",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author2",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description2",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "sport",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description3",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
},
{
"author": "author3",
"category_id": "business",
"country": "id",
"created_at": "Wed Aug 15 2018 09:26:52 GMT+07:00",
"description": "description4",
"id": "74c25662-7f94-47ef-8a7e-5091924819a9"
}
.....
]
I need to create index for category_id and created_at (timestamp) and query for certain category and filter by day now (certain date) only. I want to optimize and speed up for query result
I can do it in javascript by filter like this for category_id business and day 15 :
r.table("news").filter({category_id: 'business'}).filter(
r.row("created_at").day().eq(15)
)
But how I can create index for category_id and created_at and query it by certain day of created_at.
r.table("news").indexCreate( ???
thanks
Should be something like this:
r.table('news').indexCreate('businessAndDate', function (doc) {
return doc('category_id').add(doc('created_at').day().coerceTo('string'));
});
Then you can:
r.table('news').getAll('business15', {index: 'businessAndDate'})
You may want to insert a separator between the data (like 'business-15'), and use more than just the day (otherwise, why bother having a full timestamp?), that's up to you and how many .add you're willing to write. ;)
According to the reference, this is called an arbitrary index.

How to Call Array Through API and Push to New Array

I am trying to figure out the proper syntax to push a set of array values to a new array after making an API call. I am trying to combine 2 paths from one API call into a single array to use for an image slider for a product page on an eCommerce site. Here's what I have so far. I need help with the second path and push to array. Any input would be appreciated!
JavaScript
$.getJSON("item-data.json", function(results) {
$.each(results.CatalogEntryView, function(index, item) {
var slideshowArray = [];
//the first path - this works
slideshowArray.push(item.Images[0].PrimaryImage[0].image)
//the second path
$.each(item.Images[0].AlternateImages[0].image, function(k, v) {
slideshowArray.push(v);
});
//used to verify the successful array push
console.log(slideshowArray.length);
});
});
JSON
{
"CatalogEntryView": [
{
"CustomerReview": [
{
"Con": [
{
"RatableAttributes": [
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "4"
},
{
"description": "quality",
"name": "QUALITY",
"value": "1"
},
{
"description": "value",
"name": "VALUE",
"value": "1"
}
],
"datePosted": "Mon Mar 11 13:13:55 UTC 2013",
"overallRating": "1",
"review": "Less than 2 months after purchase it completely stopped working. First it wouldn't detect the pitcher when trying to blend a significant amount, a couple weeks later it wouldn't detect the single serve cup. ",
"reviewKey": "b326b0d6-e6ae-4ec5-8080-720f0ad741af",
"screenName": "New York",
"title": "Very unhappy"
}
],
"ConsolidatedRatableAttributes": [
{
"description": "Quality",
"name": "QUALITY",
"value": "4"
},
{
"description": "Easy to Use",
"name": "EASY_TO_USE",
"value": "4.5"
},
{
"description": "Value",
"name": "VALUE",
"value": "3.5"
}
],
"Pro": [
{
"RatableAttributes": [
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"datePosted": "Thu Apr 18 19:42:19 UTC 2013",
"overallRating": "5",
"review": "This blender works amazingly, and blends within seconds. The single serve cups also work really well for smoothies or protein shakes!",
"reviewKey": "d602bcdf-53be-4769-94da-3b3fd2517d21",
"screenName": "Eric",
"title": "Fantastic Blender"
}
],
"Reviews": [
{
"RatableAttributes": [
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "4"
},
{
"description": "quality",
"name": "QUALITY",
"value": "1"
},
{
"description": "value",
"name": "VALUE",
"value": "1"
}
],
"city": "NYC",
"customerId": "110657105",
"datePosted": "Mon Mar 11 13:13:55 UTC 2013",
"helpfulVotes": "39",
"overallRating": "1",
"review": "Less than 2 months after purchase it completely stopped working. First it wouldn't detect the pitcher when trying to blend a significant amount, a couple weeks later it wouldn't detect the single serve cup. ",
"reviewKey": "b326b0d6-e6ae-4ec5-8080-720f0ad741af",
"screenName": "New York",
"state": "NY",
"title": "Very unhappy",
"totalComments": "0",
"totalVotes": "52"
},
{
"Comments": [
{
"city": "",
"commentKey": "CommentKey:ffcefb66-381a-4985-b869-9fcfdd26e7cc",
"commentText": "Separating the men from the boys, separating the amateurs from the professionals when it comes to blenders, when you revealed to us that, -It doesn't pulverize seeds-.I really need a good blender, but there is No way that I would buy this blender now. Thank you so much, Jon",
"postedDate": "Thu Oct 10 04:17:50 UTC 2013",
"screenName": "JON",
"userKey": "118863321",
"userTier": "Trusted"
}
],
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "2"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "3"
},
{
"description": "value",
"name": "VALUE",
"value": "2"
}
],
"city": "Idaho Falls",
"customerId": "116317693",
"datePosted": "Sun Sep 01 03:18:11 UTC 2013",
"helpfulVotes": "16",
"overallRating": "2",
"review": "This blender is not superior to other smoothie blenders, It doesn't pulverize seeds and leaves green smoothies chunky with a lot of pulp. The single serve concept is amazing, however, my single serve cup began to break right from the start. The prongs became chipped because of the difficulty of screwing it in and out of the base. It won't blend for more than a minute without smelling like burned rubber. While the single serve seemed to blend more smoothly, it didn't hold much, especially when adding ice. I was very disappointed and so I returned it,",
"reviewKey": "399853f3-4451-40a8-bcd6-bda2d814d9f4",
"screenName": "London",
"state": "ID",
"title": "Very Disappointed",
"totalComments": "1",
"totalVotes": "21"
},
{
"RatableAttributes": [
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Oakland",
"customerId": "100025104",
"datePosted": "Thu Apr 18 19:42:19 UTC 2013",
"helpfulVotes": "10",
"overallRating": "5",
"review": "This blender works amazingly, and blends within seconds. The single serve cups also work really well for smoothies or protein shakes!",
"reviewKey": "d602bcdf-53be-4769-94da-3b3fd2517d21",
"screenName": "Eric",
"state": "CA",
"title": "Fantastic Blender",
"totalComments": "0",
"totalVotes": "10"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Cambridge",
"customerId": "172227",
"datePosted": "Sat Jan 18 01:20:36 UTC 2014",
"helpfulVotes": "9",
"overallRating": "5",
"review": "I am blown away by this blender. It obliterates ice and frozen fruit - and blends fresh fruits to smooth perfection. It even makes quick work of fresh ginger and tough greens. I did a ton of research before settling on the Ninja. This was a splurge for me - and I spent the extra money to get the single serve cups, thinking I'd take my smoothie to work every morning. But my husband is totally hooked on smoothies now too, so the big pitcher is getting regular use. Tried it out for margaritas tonight... half a lime, half a lemon, half an orange with tequila, honey and ice... unbelievably good. Haven't tried it for soup or sauce yet, but can hardly wait.\n\nI'm impressed by features such as the suction cup feet, the snap-seal lid, and the sensor that prevents the machine from being turned on without the top in place. It cleans up nicely too. \n\nBottom line: I can't stop raving about this thing and have recommended it to all my friends and family.",
"reviewKey": "d8e9ac59-6c3a-47be-8b87-f912715ccd18",
"screenName": "E",
"state": "MA",
"title": "Couldn't be happier",
"totalComments": "0",
"totalVotes": "9"
},
{
"Comments": [
{
"city": "",
"commentKey": "CommentKey:a5b92fc8-ec2a-4772-b4ea-3cf4d473015b",
"commentText": "THANK YOU, THANK YOU!!!!! YOU JUST GAVE ME THE BEST REASON TO -- NOT BUY -- THIS THING ! THANK YOU, JON",
"postedDate": "Thu Oct 10 03:44:47 UTC 2013",
"screenName": "JON",
"userKey": "118863321",
"userTier": "Trusted"
}
],
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "1"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "1"
},
{
"description": "value",
"name": "VALUE",
"value": "1"
}
],
"city": "new york",
"customerId": "116426870",
"datePosted": "Thu Jun 06 04:49:37 UTC 2013",
"helpfulVotes": "38",
"overallRating": "1",
"review": " Upon using this blender it turns out that the food gets into a deep hole at the bottom of the blade assembly , which fits on top of the rotating spindle, which cannot be cleaned. No amount of rinsing or dish washer washing can get to it. A special thin and long brush would be required. Such food deposits can quickly become a place for bacteria growth and accumulate soap from dishwasher etc. A radical design change and going back to the drawing board is required, which Ninja would be unwilling to do. Very poor and harmful product",
"reviewKey": "49add669-1256-4894-9fce-9e0464342887",
"screenName": "gourmet",
"state": "NY",
"title": "bacteria hazard",
"totalComments": "1",
"totalVotes": "69"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Wilmington ",
"customerId": "115016455",
"datePosted": "Sun Mar 16 13:54:36 UTC 2014",
"helpfulVotes": "5",
"overallRating": "5",
"review": "Right out of the box I love this thing. You have to read the instructions: it indicates you must pulse several times THEN blend in order to get the smooth consistency. I'm going now to google soups to make. I'll add on to my review once I've tried more stuff. I know some folks had problems, I can say with total confidence that Ninja backs up what they make. I have a vacuum, steamer and iron and I broke the vacuum and they still fixed it for free. Easy peasy. Be sure to register your purchase. Peace. ",
"reviewKey": "bf2283a9-37a1-46e2-b9b4-3edb757d5375",
"screenName": "Sandra",
"state": "DE",
"title": "Great Blender",
"totalComments": "0",
"totalVotes": "5"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Tucson",
"customerId": "119946555",
"datePosted": "Thu Jan 30 18:50:22 UTC 2014",
"helpfulVotes": "6",
"overallRating": "5",
"review": "My daughter received this Ninja blender and she absolutely loves it. My grandson has Autisim and has very sensitive taste buds. With the Ninja my daughter is able to puree his homemade soups, & refried beans. Life is a little easier for my daughter & him. She is in heaven. \n",
"reviewKey": "7c7ef8c0-e227-45a5-86cd-c29adeb0bd2a",
"screenName": "Flora",
"state": "AZ",
"title": "Ninja Blender",
"totalComments": "0",
"totalVotes": "7"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "4"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Minneapolis",
"customerId": "109690154",
"datePosted": "Sun Jan 12 17:41:43 UTC 2014",
"helpfulVotes": "4",
"overallRating": "5",
"review": "I have to assume that the negative reviewers received an unfortunate "lemon" blender... that, or they didn't read the instruction manual, because I love my Ninja and definitely recommend it.\n\nI've had this blender for over a year and it still works as wonderfully as the day I bought it. I use it primarily for making smoothies, everything from green monsters to peanut butter protein shakes to frozen fruit & yogurt smoothies with chia seeds on top.\n\nIt's like having Jamba Juice in my kitchen, but without the long line of snap-chatting teenagers.\n\nI frequently use the to-go cups to blend and take with me in the car. If you are in the camp lamenting that it doesn't hold enough, you probably also expect that once blended, it will be as full as you originally (over)stuffed it.\n\nRespect the max fill line, people, or use the full-size blender if you are going for NYC Big Gulp size.\n\nI will say, that if you are looking to seriously juice, this is not the blender for you. \n\nIt might take a little experimentation to get the right ratio of liquid to solid\/frozen for a perfectly smooth blend, but once you figure out what works for you, it's easy!",
"reviewKey": "9e0322d2-256e-46a5-80dc-b4468e58359b",
"screenName": "Kari",
"state": "MN",
"title": "Love this blender!",
"totalComments": "0",
"totalVotes": "4"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "5"
}
],
"city": "Houston",
"customerId": "116412794",
"datePosted": "Wed Jun 05 14:26:21 UTC 2013",
"helpfulVotes": "5",
"overallRating": "5",
"review": "[...]\nAll the parts are well made and good quality. The only thing that seems a little flimsy would be the drinking tops for the single serve cups, but those don't even matter because all you are doing is drinking from the tops. All the rest of the machine is top notch.\n\nThis blender is powerful, quiet and very easy to clean. \n\n[...]\nYou will not regret buying this machine. ",
"reviewKey": "4cc67e87-6754-4cab-8eb7-fb3bd738c16c",
"screenName": "Te-Ann",
"state": "TX",
"title": "LOVE LOVE LOVE!!!!",
"totalComments": "0",
"totalVotes": "6"
},
{
"RatableAttributes": [
{
"description": "quality",
"name": "QUALITY",
"value": "5"
},
{
"description": "easy_to_use",
"name": "EASY_TO_USE",
"value": "5"
},
{
"description": "value",
"name": "VALUE",
"value": "4"
}
],
"city": "CENTREVILLE",
"customerId": "102170259",
"datePosted": "Thu Jan 30 05:33:15 UTC 2014",
"helpfulVotes": "3",
"overallRating": "5",
"review": "I'm not sure why there are so many negative reviews about this blender on Target's website, but it's a great blender. The first blender I've own that actually crushes the ice completely! Perfect for shakes!",
"reviewKey": "3e810dba-638f-4146-aee8-190a741d86d5",
"screenName": "SL",
"state": "VA",
"title": "Fantastic blender!!",
"totalComments": "0",
"totalVotes": "3"
}
],
"consolidatedOverallRating": "4",
"totalPages": "2",
"totalReviews": "14"
}
],
"DPCI": "072-04-1840",
"Images": [
{
"AlternateImages": [
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt01"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt02"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt03"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt04"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt05"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt06"
},
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758_Alt07"
}
],
"PrimaryImage": [
{
"image": "http:\/\/target.scene7.com\/is\/image\/Target\/14263758"
}
],
"imageCount": "8",
"source": "internal"
}
],
"ItemDescription": [
{
"features": [
"<strong>Wattage Output:<\/strong> 1100 Watts",
"<strong>Number of Speeds:<\/strong> 3 ",
"<strong>Capacity (volume):<\/strong> 72.0 Oz.",
"<strong>Appliance Capabilities:<\/strong> Blends",
"<strong>Includes:<\/strong> Travel Lid",
"<strong>Material:<\/strong> Plastic",
"<strong>Finish:<\/strong> Painted",
"<strong>Metal Finish:<\/strong> Chrome",
"<strong>Safety and Security Features:<\/strong> Non-Slip Base",
"<strong>Care and Cleaning:<\/strong> Easy-To-Clean, Dishwasher Safe Parts"
]
}
],
"Offers": [
{
"OfferPrice": [
{
"currencyCode": "USD",
"formattedPriceValue": "$139.99",
"priceQualifier": "Online Price",
"priceValue": "13999"
}
]
}
],
"POBoxProhibited": "We regret that this item cannot be shipped to PO Boxes.",
"PackageDimension": [
{
"name": "length",
"unit": "IN",
"value": "17.4"
},
{
"name": "width",
"unit": "IN",
"value": "12.4"
},
{
"name": "height",
"unit": "IN",
"value": "9.9"
},
{
"name": "weight",
"unit": "LB",
"value": "10.85"
}
],
"Promotions": [
{
"Description": [
{
"legalDisclaimer": "Offer available online only. Offer applies to purchases of $50 or more of eligible items across all categories. Look for the "SPEND $50: SHIPS FREE" logo on eligible items. Some exclusions apply. Items that are not eligible are subject to shipping charges. $50 purchase is based on eligible merchandise subtotal. Items that are not eligible, GiftCards, e-GiftCards, gift wrap, tax and shipping and handling charges will not be included in determining merchandise subtotal. Offer valid for orders shipping within the 48 contiguous states, as well as APO\/FPO and for Standard and To the Door shipping methods only. Not valid on previous orders.",
"shortDescription": "SPEND $50, GET FREE SHIPPING"
}
],
"endDate": "2014-05-25 06:59:00.001",
"promotionIdentifier": "10736506",
"promotionType": "Buy catalog entries from category X, get shipping at a fixed price",
"startDate": "2014-05-18 07:00:00.001"
},
{
"Description": [
{
"legalDisclaimer": "Receive a $25 gift card when you buy a Ninja Professional Blender with single serve blending cups or a Ninja MEGA Kitchen System. Not valid on previous orders. On your order summary, the item subtotal will reflect the price of the qualifying item plus the amount of the free gift card, followed by a discount given for the amount of the free gift card. Your price on the order summary will be the price of the qualifying item (the total charges for the qualifying item and gift card). Your account will actually be charged the amount of the qualifying item reduced by the amount of the gift card, and a separate charge for the amount of the gift card. The gift card will be sent to the same address as your order and will ship separately. If you want to return the item you purchased to a Target Store, you may either keep the gift card and just return the qualifying item (you will be refunded the amount of the qualifying item reduced by the amount of the gift card), or you can return the qualifying item and the gift card for a full refund using the online receipt. If you return the item you purchased by mail, keep the gift card; you will be refunded the amount of the qualifying item reduced by the amount of the gift card. Offer expires 05\/24\/14 at 11:59pm PST.",
"shortDescription": "$25 gift card with purchase of a select Ninja Blender"
}
],
"endDate": "2014-05-25 06:59:00.001",
"promotionIdentifier": "10730501",
"promotionType": "Multiple Items Free Gift",
"startDate": "2014-05-11 07:00:00.001"
}
],
"ReturnPolicy": [
{
"ReturnPolicyDetails": [
{
"guestMessage": "View our return policy",
"policyDays": "100",
"user": "Regular Guest"
},
{
"guestMessage": "View our return policy",
"policyDays": "120",
"user": "Best Guest"
}
],
"legalCopy": "refund\/exchange policy<br\/><br\/><p style=\"font-size:13px;\">Most unopened items in new condition returned within 90 days will receive a refund or exchange. Some items have a modified return policy that is less than 90 days. Those items will either show a \"return by\" date or \"return within\" day range under the item on your receipt or packing slip and in the \"Item details, shipping\" tab if purchased on Target.com. Items that are opened or damaged or do not have a packing slip or receipt may be denied a refund or exchange. All bundled items must be returned with all components for a full refund. Bundle components may not all have the same return policy; please check your packing slip for details. Some items, such as gift cards, digital items are never returnable. <br \/><br \/>See the <a href=\"http:\/\/www.target.com\/HelpContent?help=\/sites\/html\/TargetOnline\/help\/returns_and_refunds\/returns_and_refunds.html\">Target return policy<\/a> for complete information.<\/p><br\/>"
}
],
"UPC": "622356532099",
"applyCouponLink": "false",
"buyable": "true",
"callOutMsg": "FREE $25 GIFT CARD",
"catEntryId": "205273068",
"classId": "04",
"department": "072",
"eligibleFor": "ADD_TO_CART",
"inventoryCode": "0",
"inventoryStatus": "Online",
"itemId": "1840",
"itemType": "ItemBean",
"manufacturer": "Euro Pro",
"manufacturerPartNumber": "BL660",
"packageQuantity": "null ",
"partNumber": "14263758",
"purchasingChannel": "Sold Online + in Stores",
"purchasingChannelCode": "0",
"shortDescription": "For the first time EVER - you get the same professional performance power in the Single Serve as well as the XL 72 oz pitcher! The Ninja\u2122 Professional Blender with Single Serve Blending Cups allow you to crush ice into snow, blend whole fruits and vegetables into nutritious beverages, and create resort style blended drinks! Full size blender performance now in individual cups.",
"title": "Ninja\u2122 Professional Blender with Single Serve Blending Cups",
"webclass": "Small Appliances"
}
]
}

Filter search from json using jquery

I am building a onine bus booking web application like redbus.in in which i want to implement the filter search feature like the way it is implemented in redbus.
Here is the workflow
1.There are 4 dropdown boxes present with values droppint point, boardingpoint, bustype,opeartor name
2.when user select one dropping point from the dropdown the result will shows only with list of buses with that dropping point ,if he select dropping point and boarding point then it will show the list of buses that contains only two of this and so on.
Ie whatever the user select from these 4 dropdown menu the result (list of buses ) will contains only with these values.
But i don't know how to implement it especially the filtering .
Here is the sample json for one bus
{
"droppingPoints": [
{
"time": "09:45 PM",
"location": "Kachiguda",
"id": "1283518"
},
{
"time": "09:55 PM",
"location": "Narayanaguda",
"id": "1283519"
},
{
"time": "10:05 PM",
"location": "Lakdi ka pool",
"id": "1283520"
},
{
"time": "10:15 PM",
"location": "Punjagutta",
"id": "1283521"
},
{
"time": "10:25 PM",
"location": "Srinivasa Colony",
"id": "1283522"
},
{
"time": "10:25 PM",
"location": "SR Nagar",
"id": "1283523"
},
{
"time": "10:25 PM",
"location": "Ameerpet",
"id": "1283524"
},
{
"time": "10:30 PM",
"location": "Erragadda",
"id": "1283525"
},
{
"time": "10:35 PM",
"location": "Bharat Nagar",
"id": "1283526"
},
{
"time": "10:40 PM",
"location": "VIVEKANANDA NAGAR",
"id": "1283527"
},
{
"time": "10:40 PM",
"location": "Kukatpally",
"id": "1282840"
},
],
"availableSeats": 4,
"partialCancellationAllowed": false,
"arrivalTime": "07:00 AM",
"boardingPoints": [
{
"time": "10:45 PM",
"location": "K P H B",
"id": "1282841"
},
{
"time": "10:55 PM",
"location": "Hydernagar",
"id": "1283073"
},
{
"time": "10:55 PM",
"location": "Nizampet",
"id": "1283074"
},
{
"time": "11:00 PM",
"location": "MiyaPur",
"id": "1283072"
},
{
"time": "11:05 PM",
"location": "Miyapur Allwin X Road",
"id": "1283422"
},
{
"time": "11:15 PM",
"location": "Kondapur",
"id": "1283423"
},
{
"time": "11:20 PM",
"location": "Gachibowli",
"id": "1283554"
}
],
"operatorName": "Morning Star Travels",
"departureTime": "10:30 PM",
"mTicketAllowed": true,
"idProofRequired": true,
"serviceId": "1572",
"fare": "1090.0, 1190.0",
"busType": "2+1 Sleeper Non A/C",
"routeScheduleId": "84W157YI3YGZKH5B9K0TG4E40VI13",
"commPCT": 7.6,
"operatorId": 1572,
"inventoryType": 2
}
Currently i have this code in my hand
<script>
//when select item from dropping point
$('.drp').on('change',function(){
search_buses();
});
//when select item from boarding point
$('.brp').on('change',function(){
search_buses();
});
//when select item from butype
$('.bt').on('change',function(){
search_buses();
});
//when select item from opearto name
$('.on').on('change',function(){
search_buses();
});
//function for searching
function search_buses(){
//list of buses in json format.
var bus_list;
}
</script>
i Want to implement filter functionality in search_bus function. but i don't know how to do that.
Filtering would be as easy as using the filter and the some javascrip methods:
function searchBusesByLocation(location) {
filteredBuses = busesArray.filter(function (bus) {//this will create a new array with the filtered items
return boardingPointLocationComparisson(bus, location);
});
return filteredBuses;
}
function boardingPointLocationComparisson(bus, location) {//i'm guessin you want to filter by location, you can do by date or adapt this function like you want
return bus.boardingPoints.some(function (boardingPoint) {
//the some function will iterate over the array and return true whenever the comparisson is true, false otherwise
return boardingPoint.location === location;
})
}
Then you call the searchBusesByLocation on the change event with the location the user has entered (you can adapt the code for the other cases).

Categories

Resources