how to randomize JSON in Angularjs - angularjs

How can i randomize my following JSON in angularjs???
{
"1": {
"id": "1",
"que": "Grand Central Terminal, Park Avenue, New York is the world's",
"options": {
"A": "largest railway station",
"B": "highest railway station",
"C": "longest railway station",
"D": "None of the above"
},
"ans": "A"
},
"2": {
"id": "2",
"que": "Entomology is the science that studies",
"options": {
"A": "Behavior of human beings",
"B": "Insects",
"C": "The origin and history of technical and scientific terms",
"D": "The formation of rocks"
},
"ans": "B"
},
"3": {
"id": "3",
"que": "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of",
"options": {
"A": "Asia",
"B": "Africa",
"C": "Europe",
"D": "Australia"
},
"ans": "B"
}
}

To make your task easier i would suggest you use an array of objects like this one:
var questions=
[
{
"id": "1",
"que": "Grand Central Terminal, Park Avenue, New York is the world's",
"options": {
"A": "largest railway station",
"B": "highest railway station",
"C": "longest railway station",
"D": "None of the above"
},
"ans": "A"
},
{
"id": "2",
"que": "Entomology is the science that studies",
"options": {
"A": "Behavior of human beings",
"B": "Insects",
"C": "The origin and history of technical and scientific terms",
"D": "The formation of rocks"
},
"ans": "B"
},
{
"id": "3",
"que": "Eritrea, which became the 182nd member of the UN in 1993, is in the continent of",
"options": {
"A": "Asia",
"B": "Africa",
"C": "Europe",
"D": "Australia"
},
"ans": "B"
}
]
And then to select a random question use:
$scope.randomQuestion = questions[Math.floor(Math.random()*questions.length)];

One solution would be to convert your questions Object to an Array, so you can randomize the questions order:
http://plnkr.co/edit/PSeh0BLNjK5ydXJrLguw?p=preview
<p ng-repeat="q in questionsArray | orderBy: random">{{q.key}} - {{q.val.que}}</p>

Related

How can I filter values based on other columns?

How can I select certain values and/or values conditional on other columns in vega-lite? For example, below I only want to show values that have "red" in the column "c".
{
"data": {
"values": [
{"a": "A", "b": 2, "c": "red"},
{"a": "A", "b": 7, "c": "red"},
{"a": "A", "b": 4, "c": "blue"},
{"a": "B", "b": 1, "c": "blue"},
{"a": "B", "b": 2, "c": "red"}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "nominal"},
"y": {"aggregate": "average", "field": "b", "type": "quantitative"}
}
}
I have tried adding the following code before "mark": "bar", according to this vega github tutorial, and using the online vega editor, but it doesn't filter column b. I thought I could use it to somehow filter a string as well.
"transform": {
"filter": "datum.b > 3"
},
Follow-up question regarding multiple filtering criteria.
You can do this with a filter transform:
{
"data": {
"values": [
{"a": "A", "b": 2, "c": "red"},
{"a": "A", "b": 7, "c": "red"},
{"a": "A", "b": 4, "c": "blue"},
{"a": "B", "b": 1, "c": "blue"},
{"a": "B", "b": 2, "c": "red"}
]
},
"transform": [{"filter": "datum.c == 'red'"}],
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "nominal"},
"y": {"aggregate": "average", "field": "b", "type": "quantitative"}
}
}

Type-safe npm library for handling normalized data stored in Redux

I'm looking for a library to denormalize data structure with one-to-many, many-to-one, many-to-many and one-to-one relationships in Redux store.
Example:
{
"users": {
"a": { "id": "a", "name": "A" },
"b": { "id": "b", "name": "B" }
}
}
{
"posts": {
"x": { "id": "x", "authorId": "a" },
"y": { "id": "y", "authorId": "b" },
"z": { "id": "z", "authorId": "c" }
}
}
should be converted into something like:
{
"posts": {
"x": { "id": "x", "authorId": "a", "author": { "id": "a", "name": "A" } },
"y": { "id": "y", "authorId": "b", "author": { "id": "b", "name": "B" } },
"z": { "id": "z", "authorId": "a", "author": { "id": "a", "name": "A" } },
}
}
Produced data does not have to be exactly the same as the example, but at least should be strictly typed.
Currently I always use Object.fromEntries(Object.entries(***).map(([key, value]) => ***) in selectors, which is hard to read.

How to get a value in one mongodb collection and use that value to update another document in another collection order & inventory system

Hello I have been stuck for weeks trying to figure how to create a order & inventory system for a project I am working on. I don't know how to properly ask this but my problem is when a user adds items to their cart > I store the order details in a orders collection in mongodb > I then need to figure out how to subtract the quantity of the items in a customers order from my inventory collection. How can I do this with mongodb, Python
This is the document created when a customer places an order
{
"_id": "5eca94b4f56331fd9eade681",
"ordernumber": 343,
"order": {
"order_details": [
{
"customer_info": [
{
"first_name": "John",
"last_name": "Doe",
"email": "email#email.com"
}
],
"shipping_details": [
{
"shipping_address": "Test Address",
"shipping_zip": "12345",
"shippingl_city": "Test city",
"shipping_country": "USA"
}
],
"products_ordered": [
{
"variant_id": "a",
"product_name": "red shirt",
"price": 30,
"quantity": 2,
"image": "imageurl",
"size": "Small"
},
{
"variant_id": "f",
"product_name": "Blue Jeans",
"price": 20,
"quantity": 3,
"image": "imageurl",
"size": "Large"
}
]
}
]
}
}
These are the products in my inventory collection I want inventory order quantity subtracted by the quantity a customer purchased
{
"_id": "5eca0ff4898b8f30a9fee5e5",
"product_id": 1,
"product_name": "red shirt",
"category": "shirts",
"price": 30,
"status": "instock",
"description": "nice red shirt",
"alt": "string",
"images": [
"imageUrl1",
"imageUrl2"
],
"variants": [
{
"Small": [
{
"variant_id": "a",
"inventory": 30
}
],
"Medium": [
{
"variant_id": "b",
"inventory": 10
}
],
"Large": [
{
"variant_id": "c",
"inventory": 10
}
]
}
]
}
{
"_id": "5eca108f898b8f30a9fee5e6",
"product_id": 2,
"product_name": "blue jeans",
"category": "jeans",
"price": 20,
"status": "instock",
"description": "nice blue jeans",
"alt": "string",
"images": [
"ImageURL"
],
"variants": [
{
"Small": [
{
"variant_id": "d",
"inventory": 100
}
],
"Medium": [
{
"variant_id": "e",
"inventory": 150
}
],
"Large": [
{
"variant_id": "f",
"inventory": 70
}
] }
]
}
I would suggest to do it along with the service which creates the order.
I would also like to suggest to refactor the db structure a bit as it would be harder to maintain this in a larger scale.
Because currently we would have to write something like
for ordered_product in products_ordered:
query = { "product_name": ordered_product.get("product_name") }
inventory_product = inventory_collection.find_one(query)
product_id = inventory_product["_id"]
existing_count = inventory_product["variants"][0][ordered_product.size][0]["inventory"]
inventory_product["variants"][0][ordered_product["size"]][0]["inventory"] = existing_count - ordered_product["quantity"]
inventory_collection.update_one({ "_id": product_id }, { "$set": inventory_product })
I have hardcoded the index values of the list. You could use filter() to filter out the variant and size you need.
This code definitely seems messy to me.
Of course you could refactor this code by splitting it into functions inside the model file itself, but I would suggest to refactor the db structure for better scalability.
May be you could move the variants to a seperate collection and use the product_id as a link. You have to think this through before getting on with the code.
Hope this helps.

How to join Json response element

I am making a call to the OpenWeatherApi am facing a dilemma. I am pulling the response, but only need specific data elements. I am successfully pulling certain elements, but am having trouble pulling the CITY element.
This is the response from the API call:
{
"coord": {
"lon": -122.4,
"lat": 45.64
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 287.5,
"pressure": 1022,
"humidity": 82,
"temp_min": 284.25,
"temp_max": 289.25
},
"visibility": 16093,
"wind": {
"speed": 0.96,
"deg": 10.5029
},
"clouds": {
"all": 1
},
"dt": 1537854780,
"sys": {
"type": 1,
"id": 2321,
"message": 0.0172,
"country": "US",
"sunrise": 1537884050,
"sunset": 1537927234
},
"id": 420040945,
"name": "Vancouver",
"cod": 200
}
This is my mapping for the specific elements I need:
const main = response.data["main"]
const sys = response.data["sys"]
const city = response.data.name;
const weather = response.data["weather"][0]
const data = Object.assign(main, weather, sys, city)
res.send(data)
console.log(data)
Finally, this is the response from my mapping:
{
"0": "C",
"1": "i",
"2": "n",
"3": "c",
"4": "i",
"5": "n",
"6": "n",
"7": "a",
"8": "t",
"9": "i",
"temp": 293.74,
"pressure": 1018,
"humidity": 90,
"temp_min": 293.15,
"temp_max": 294.25,
"id": 2179,
"main": "Rain",
"description": "light rain",
"icon": "10n",
"type": 1,
"message": 0.0044,
"country": "US",
"sunrise": 1537874932,
"sunset": 1537918192
}
As you can see, CITY is split up into separate elements. If I only pull CITY, it pulls the accurate City just fine, as
"Cincinnati"
not
"name": "Cincinnati".
How can I join the elements for form the city, or recreate the "name": "City" element altogether?
you are getting wrong result because city is an array and its getting spread. change your code to below code
Object.assign({}, main, weather, sys, {city})
or
Object.assign({}, main, weather, sys, {name:city})
Solution is to convert array value to object property.

Specific json string formatting

I have an odd requirement to prettify a JSON structure in a very precise way. Currently I'm using angular.toJson(jsonObj, true) to auto-prettify it.
However, how could I do customized formatting. Given the following JSON:
{"SomeArray": [
{
"Description": "Some sort of description",
"a": 0,
"b": "foo0",
"c": "bar0"
},
{
"a": 0,
"b": "foo1",
"c": "bar1",
"d": "DoSomething1"
},
{
"a": 0,
"b": "foo2",
"c": "bar2",
"d": "DoSomething2"
},
{
"Description": "Another description",
"a": 0,
"b": "foo3",
"c": "bar3",
"d": "DoSomething3"
},
{
"a": 0,
"b": "foo4",
"c": "bar4",
"d": "DoSomething4"
}]}
How can I format this to look exactly like the following? Basically I need to keep "description" keys on it's own seperate line, while the other keys are on a line immediately below it, as well as keeping the curly brace on the same line as well:
{"SomeArray": [
{ "Description": "Some sort of description",
"a": 0, "b": "foo0", "c": "bar0" },
{ "a": 0, "b": "foo1", "c": "bar1", "d": "DoSomething1" },
{ "a": 0, "b": "foo2", "c": "bar2", "d": "DoSomething2" },
{ "Description": "Another description",
"a": 0, "b": "foo3", "c": "bar3", "d": "DoSomething3" },
{ "a": 0, "b": "foo4", "c": "bar4", "d": "DoSomething4" }]}
Figured I'd circle back around and answer how I wound up solving this. Just wound up using HandleBars.js. It's a pretty slick templating engine. I wrote the template of how I want json output to look, then just passed it the object I wanted formatted. The net results was formatting exactly as I needed it, with very little effort!
If anyone else is struggling with formatting JSOn for whatever reason, I strongly recommend checking out Handlebars.

Resources