Is there an easy way to perform range queries (e.g. get me all k,v pairs with keys between 50 and 100) on immutable maps with number keys?
This can be done in one of two ways. You can either:
Iterate through the entire contents of the map and filter out values that you are not interested in.
Iterate from min to max and extract all values from the map into a new map
Unfortunately there is not a method built into ImmutableJS that will do this for you.
Here are the bits of code to help you along:
Init
map = Immutable.Map([ [ 10, 'a' ], [ 20, 'b' ], [ 5, 'c' ], [ 100, 'd' ] ])
min = 0
max = 10
method 1
map.filter((_, key) => key >= min && key <= max)
method 2
let range = Array.apply(0, Array(max - min + 1)).map((_, index) => index + min);
return range.reduce((acc, key) => {
return map.has(key) ?
acc.set(key, map.get(key)) :
acc;
}, Immutable.Map())
https://observablehq.com/#alexrass/extract-range-from-immutable-map
Related
Trying to build a report result of orders related information of a laravel project. But struggling to find out a solution on a specific data structure.
My Request :
// Date Ranges
$fromDate = $this->request->fromDate;
$toDate = $this->request->toDate;
So the result should be within a date range.
My orders table :
order_id order_addresses grand_total
------------------------------------------
1 JSON DATA 3,000.00
2 JSON DATA 2,000.00
3 JSON DATA 1,500.00
So My JSON DATA looks like this :
{
"delivery_address":{
"House":"Offline House",
"Street":"Offline Road",
"Area":"Offline Area",
"PostCode":"1230",
"City":"City1",
"Country":"Country1",
"ContactPerson":"Offline User",
"ContactNumber":"01XXXXXXXX"
}
}
I just want the response as :
{
"orders": 3, // How many orders are there in that particular city
"totals": "5,500.00", // Total how much the cost of orders in that city
"groupby": "City1",
},
{
"orders": 5,
"totals": "7,500.00",
"groupby": "City2",
},
...,
...
I am seeking a solution using query builder in laravel with MySQL.
This is an existing project so I can't really change the structure how it was built. So, any suggestions on how I can extract the cities from JSON DATA having relation with the orders identity along with the totals and all.
I just need the order_ids I think city wise then I can structure my result anyway I like to achieve end result.
If anything confusion here, please let me know so that I can make it clear.
Thanks in Advance !
I would suggest grouping fetched data using Laravel Collection functions.
Order Model
class Order extends Model {
// This will cast data to native types.
// so order_address json string to array
protected $casts = [
'order_addresses' => 'array',
];
}
Controller function
// Fetch orders from the database as you usually do
// without considering about grouping.
// with your conditions.
// I will simply use all() for example.
$orders = Order::all();
// First group by city
// Then map to get the result you want.
$groupedOrders = $orders->groupBy(function($order) {
// since we added it to the casts array in the model
// laravel will automatically cast json string to an array.
// So now order_address is an array.
return $order->order_addresses['City'];
})
->map(function($groupedOrders, $groupName) {
return [
'orders' => $groupedOrders->count(),
'totals' => $groupedOrders->sum('grand_total'),
'groupby' => $groupName,
];
});
The following is coded in the Laravel framework:
QUERY
$shipping = DB::table('shipping')->where('country',$delivery->country)->first();
$data= json_decode(json_encode($shipping),true);
RESULT
array (
'id' => 3,
'carrier' => 'EN',
'country' => 'AU',
'rates_json' => '{"rates": [{"international": [{"zone4": [{"to_kg": "2", "total": "1", "from_kg": "1"}, {"to_kg": "4", "total": "2", "from_kg": "3"}]}]}]}',
)
In the MySQL database, I stored "rates_json" in a "JSON" datatype column. The attribute "from_kg" and "to_kg" is a range.
I intend to retrieve the total if a value is between the range. For instance, if value 1.5 is between 1 and 2 then the total is 1.
Your help is appreciated.
Thank You.
Since your question is not clear,
I can only give you a hint, what you can do.
foreach (json_decode($data['rates_json'])->rates as $rates) {
foreach ($rates->international as $international) {
foreach ($international->zone4 as $zone) {
if ($zone->from_kg <= $zone->total && $zone->total <= $zone->to_kg)
{
// do whatever you want here.
dump($zone->total);
}
}
}
}
Let me know for any adjustments.
I am new to typescript in angular 2 and i stuck with a situation.
I have a json array in this format
needle json
[{"empId":100,"orgId":500}
{"empId":201,"orgId":566}]
The above json is in a particular order and we need to keep that order maintained while looking for those in another json array(Haystack)
Haystack json array
[
{"empCode":21,"fname":"Ashish","Lname":"Shukla"},
{"empCode":22,"fname":"John","Lname":"Mark"},
{"empCode":21,"fname":"Vigil","Lname":"Rocker"},
{"empCode":201,"fname":"Rick","Lname":"Mandez"},
{"empCode":21,"fname":"Erik","Lname":"Francis"},
{"empCode":100,"fname":"Alex","Lname":"Mishra"},
{"empCode":21,"fname":"Feeder","Lname":"Kapoor"},
{"empCode":21,"fname":"Dan","Lname":"Rox"},
{"empCode":21,"fname":"Herb","Lname":"Deen"},
{"empCode":21,"fname":"Nate","Lname":"Diaz"},
{"empCode":21,"fname":"Nick","Lname":"Diaz"},
{"empCode":21,"fname":"Conor","Lname":"Pussy"}
]
Now i need to get those values from haystack array whose id matches in needle keeping the order maintained of the needle
{"empCode":100,"fname":"Alex","Lname":"Mishra"},
{"empCode":201,"fname":"Rick","Lname":"Mandez"}
I have achieved the solution to this problem but i guess my solution is not optimal as i am using many loops. Can some one suggest me a good solution.
PLS NOTE: Order of the employee id should be maintained in result json as of the needle json.
Thanks a lot :)
This should work
public needle: any;
public hayStack: any;
this.needle = [
{"empId": 100, "orgId": 500},
{"empId": 201, "orgId": 566}
];
this.hayStack = [
{"empCode":21,"fname":"Ashish","Lname":"Shukla"},
{"empCode":22,"fname":"John","Lname":"Mark"},
{"empCode":21,"fname":"Vigil","Lname":"Rocker"},
{"empCode":201,"fname":"Rick","Lname":"Mandez"},
{"empCode":21,"fname":"Erik","Lname":"Francis"},
{"empCode":100,"fname":"Alex","Lname":"Mishra"},
{"empCode":21,"fname":"Feeder","Lname":"Kapoor"},
{"empCode":21,"fname":"Dan","Lname":"Rox"},
{"empCode":21,"fname":"Herb","Lname":"Deen"},
{"empCode":21,"fname":"Nate","Lname":"Diaz"},
{"empCode":21,"fname":"Nick","Lname":"Diaz"},
{"empCode":21,"fname":"Conor","Lname":"Pussy"}
];
const needleEmpId = this.needle.map(item => item.empId);
const hayStackEmpCode = this.hayStack.map(item => item.empCode);
const result = hayStackEmpCode.map((id, index) => {
if (needleEmpId.indexOf(id) != -1) {
return this.hayStack[index];
}
}).sort().filter(item => (item != undefined));
console.log(result);
Result
0:{empCode: 100, fname: "Alex", Lname: "Mishra"}
1:{empCode: 201, fname: "Rick", Lname: "Mandez"}
In MediaWiki it's possible to access the total number of pages that have been created with one of the magic words, {{NUMBEROFPAGES}}. Is there a way to access the total number of wanted pages, pages that are redlinked? There is Special:WantedPages, and if I go to the last page of results it has the total number. Does it find that only on request, or is that variable accessible in a similar way to {{NUMBEROFPAGES}}?
That's not included in the default magic words and I'm not aware of any extension that would provide it, either. If you don't care too much about performance it is pretty easy to do:
global $wgHooks;
$wgHooks['LanguageGetMagic'][] = function ( &$magicWords, $langCode ) {
// 1 is for case-sensitive
$magicWords['wantedpages'] = [ 1, 'NUMBEROFWANTEDPAGES' ];
};
$wgHooks['MagicWordwgVariableIDs'][] = function ( &$customVariableIds ) {
$customVariableIds[] = 'wantedpages';
};
$wgHooks['ParserGetVariableValueSwitch'][] = function (
&$parser, &$cache, &$magicWordId, &$ret
) {
$db = wfGetDB( DB_REPLICA );
$ret = $db->selectRowCount(
[ 'pagelinks', 'page' ], // tables
'count(*)', //value
[ 'page_id' => null ], // conditions
__METHOD__,
[ 'GROUP BY' => [ 'pl_namespace', 'pl_title' ] ],
[ 'page' => [ 'LEFT JOIN', 'pl_namespace = page_namespace AND pl_title = page_title' ] ] // join conditions
);
};
Note that this is the extreme corner cutter version. If you are writing an extension and want to do it properly, see the manual.
I have a json structure that I'm decoding that looks like this:
person => {
city => "Chicago",
id => 123,
name => "Joe Smith",
pets => {
cats => [
{ age => 6, name => "cat1", type => "siamese", weight => "10 kilos" },
{ age => 10, name => "cat2", type => "siamese", weight => "13 kilos" },
],
dogs => [
{ age => 7, name => "dog1", type => "siamese", weight => "20 kilos" },
{ age => 5, name => "dog2", type => "siamese", weight => "15 kilos" },
],
},
},
}
I'm able to print the city, id, name by doing:
foreach my $listing ($decoded->{person})
{
my $city = $listing->{city};
my $name = $listing->{name};
name - $city - \n";
}
however, I'm unsure of how to print the pets->cats or pets->dogs. I'm able to do a dump of them by:
my #pets = $listing->{pets}->{cats};
dump #pets;
but I'm not sure how to access them through the hash structure.
Digging into a big structure is pretty simple, once you know the rules:
Wrap hash keys in {}
Wrap array indexes in []
If your top level variable is a reference, use -> before the first identifier.
After the first set of braces or brackets, additional arrows (->) are optional.
So:
* $data->{person}{name} returns 'Joe Smith'
* $data->{person}->{name} also returns 'Joe Smith'
* $data->{pets}{cats}[0]{age} returns 6.
For way more detail on this topic, see the Perl Data Structures Cookbook (perldoc perldsc)
When you work with big structures like this there are some important things to be aware of. The biggest of these is autovivification. Autoviv means that Perl will automatically make data structure elements pop into existence for you to make your life easier. Unfortunately it can also make things difficult.
For example, autoviv is great when I do this:
my $data;
$data->{horse}[0]{color} = 'brown';
Autoviv magically turns $data into a hashref that contains the key horse with an array ref as its value. The array ref gets populated by a hash ref. The final hash ref then gets the key value pair of color => brown.
The problem comes in when you are walking a structure and do deep tests for existence:
# Code from above continues:
if( exists $data->{cat}[5]{color} ) {
print "Cat 5 has a color\n";
}
use Data::Dumper;
print Dumper $data;
Here, autovivification burns you by creating a bunch of junk in data, here's the program output:
$VAR1 = {
'cat' => [
undef,
undef,
undef,
undef,
undef,
{}
],
'horse' => [
{
'color' => 'brown'
}
]
};
Now you can guard against this kind of thing by carefully testing each layer of your structure for existence, but it's a huge pain in the butt. Instead, I prefer to use Data::Diver.
use Data::Diver qw( Dive );
my $dog_20_color = Dive( $data, 'dog', 20, 'color' );
print "Dog 20 is $dog_20_color\n" if defined $dog_20_color;
$data is unchanged here.
Also, you may have noticed that since Dive takes a list of keys or indexes, that means its easy to programatically build up a list of keys/indexes and descend an arbitrary path in your code.
Data::Diver can be a real life saver when you have to do a lot of manipulation of big, wonky data structures.
Assuming your $listing is a person you have to dereference array and hash refs.
# as long as we are assuming $listing is a person
# this goes inside the foreach you posted in your
# question.
# this will print all cats' names
foreach my $cat ( #{ $listing->{pets}->{cats} } )
{
# here $cat is a hash reference
say $cat->{name}; # cat's name
}
and so on for other stuff.
To access them from the structure you can do:
say $listing->{pets}->{cats}->[0]->{name}; # this will print 'cat1'
my #pets = $listing->{pets}->{cats};
This isn't doing what you think it is. $listing->{pets}->{cats} contains a reference to an array. Your new #pets array ends up contains just one element - the array reference.
What you actually need is
my #pets = #{ $listing->{pets}{cats} };
This deferences the array reference and gets you the actual array. Notice that I've also dropped the optional second arrow in the expression.
Once you've got the array, each element of it is a hash reference.
foreach (#pets) {
say $_->{name};
# etc ...
}
Of course, you don't need the intermediate array at all.
foreach (#{ $listing->{pets}{cats} }) {
say $_->{name};
}