UnderscoreJs : objects sortBy with empty attributes - sorting

Problem is, I want to sort by price an array of users that might contains empty attributes like this one:
var array = [
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Johan",
"pricing": {
"base_price" : "12",
"price_by_hour" : "5"
}
},
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Marco"
},
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Jane",
"pricing": {
"base_price" : "8",
"price_by_hour" : "11"
}
}
];
array = _.sortBy(array, function(item) {
return item.pricing.base_price;
});
console.log(array);
TypeError: Cannot read property 'base_price' of undefined
How can I put the items without the pricing object at the bottom of my list and still sorting it?
In this case, I want to sort the list with Jane first, then Johan, then Marco.

Here's an easy way to do it:
_.sortBy(array, 'pricing.base_price');
When you pass a string as an iteratee to sortBy(), the property() function is used. This function works with property paths and simply returns undefined if the property doesn't exist.

Just put a conditional
array = _.sortBy(array, function(item){
if(item.pricing){
return item.pricing.base_price;
}
});

OK, I just had to return false if the attribute is empty:
array = _.sortBy(array, function(item) {
if(!item.pricing || !item.pricing.base_price){
return -1;
}
return item.pricing.base_price;
});

You need a conditional to avoid getting TypeError. Also you need to cast base_price to Number to get a proper sorting.
array = _.sortBy(array, function(item){
if(item.pricing){
return Number(item.pricing.base_price);
}
});
One alternative would be already initate them as Number.
var array = [
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Johan",
"pricing": {
"base_price" : 12,
"price_by_hour" : 5
}
},
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Marco"
},
{
"created": "2015-11-27T16:33:46.781Z",
"name": "Jane",
"pricing": {
"base_price" : 8,
"price_by_hour" : 11
}
}
];
array = _.sortBy(array, function(item) {
if(item.pricing){
return item.pricing.base_price;
}
});

Related

JSONata array to array manipulation with mapping

I need to transform array to array with some extra logic:
Map field name in array if it exists in the mapping object, if not process it as it is in the source
Sum up values of objects with the same name
Remove objects with zero value
for example here is the source json:
{
"additive": [
{
"name": "field-1",
"volume": "10"
},
{
"name": "field-2",
"volume": "10"
},
{
"name": "field-3",
"volume": "0"
},
{
"name": "field-4",
"volume": "5"
}
]
}
object with mapping config(field-1 and field-2 is mapped to the same value):
{
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
}
and this is the result that I need to have
{
"chemicals": [
{
"name": "field-1-mapped",
"value": 20
},
{
"name": "field-4",
"value": 5
}
]
}
as you can see field-1 and field-2 is mapped to field-1-mapped so the values are summed up, field-3 has 0 value so it is removed and field-4 is passed as it is because it's missing in the mapping.
so my question is: is it possible to make it with JSONata?
I have tried to make it work but I stuck with this lookup function that doesn't return default value when name is missing in mapping:
{
"chemicals": additive # $additive #$.{
"name": $res := $lookup({
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
}, $additive.name)[ $res ? $res : $additive.name],
"amount": $number($additive.volume),
} [amount>0]
}
Probably easiest to break it down into steps as follows:
(
/* define lookup table */
$table := {
"field-1": "field-1-mapped",
"field-2": "field-1-mapped",
"field-3": "field-3-mapped"
};
/* substitute the name; if it's not in the table, just use the name */
$mapped := additive.{
"name": [$lookup($table, name), name][0],
"volume": $number(volume)
};
/* group by name, and aggregate the volumes */
$grouped := $mapped[volume > 0]{name: $sum(volume)};
/* convert back to array */
{
"chemicals": $each($grouped, function($v, $n) {{
"name": $n,
"volume": $v
}})
}
)
See https://try.jsonata.org/0BWeRcRoZ

Laravel - Sort array by string value first and then date

I need help to sort array by couple of logics
[
{
"id": 1,
"status": "pending",
"date": "2019-08-01"
},
{
"id": 2,
"status": "delivered",
"date": "2019-08-01"
},
{
"id": 3,
"status": "pending",
"date": "2019-08-03"
},
{
"id": 4,
"status": "delivered",
"date": "2019-08-03"
},
{
"id": 5,
"status": "delivered",
"date": "2019-08-02"
}
]
what I want to do is to sort the array to status pending show first, and then sort it by the date descending
I already test to using sortByDesc from laravel collection but the array looks like sorted it by just 1 function
$collection = $collection->sortByDesc('date')->sortByDesc(function ($row, $key) {
if($row['status'] == 'pending'){
return 1;
}else{
return 0;
}
});
My expected final result look like this :
[
{
"id": 3,
"status": "pending",
"date": "2019-08-03"
},
{
"id": 1,
"status": "pending",
"date": "2019-08-01"
},
{
"id": 4,
"status": "delivered",
"date": "2019-08-03"
},
{
"id": 5,
"status": "delivered",
"date": "2019-08-02"
},
{
"id": 2,
"status": "delivered",
"date": "2019-08-01"
}
]
Few solutions:
Use a custom callback and return an array source
$products->sortBy(function($product) {
return [$product->param1, $product->param2];
});
This will sort a collection by param2 first, and then by param1
Use a custom callback and return a composite property to sort on source
$posts = $posts->sortBy(function($post) {
return sprintf('%-12s%s', $post->column1, $post->column2);
});
Sort your array by column 1, then split it up by column 2 and then merge it again (untested).
$collection->sortByDesc('date');
$collection->groupBy('status');
$collection->keyBy('status');
EDIT: Also I'm not sure if sortByDesc('date') works with date strings.
Your expected result can be achieved like this.
$sorted = $collection
->sortByDesc('date')
->sortBy(function ($item) {
return 'pending' == $item['status'] ? 0 : 1;
})
->values();
To be more precise:
$collection= $collection->sort(
function ($a, $b) {
if(($a->status== $b->status) &&($a->status== 'pending')){
return ($a->date >= $b->date) ? -1 : 1;
}elseif($a->status== 'pending' && ($a->status!= $b->status)){
return 1;
}else{
return ($a->date <= $b->date) ? 1 : -1;
}
}
);
$collection= $collection->sortByDesc('status');

rethinkdb update nested array

I have the following structure:
{
"id": "3065e957-56e9-4084-8e32-bb4de8d9265a",
"id_service": "570b4abe-70fe-44e0-845e-74eb60081fc4",
"tweets": [
{
"created_at": "2013-10-13 00:58:11",
"id_tweet": "389193311908413440",
"id_user": 12375562,
"name": "elgabo1",
"photo": "https://pbs.twimg.com/profile_images/1827710728/45d1be6d2e0f1c710814e098d6f56c12_normal.png",
"screen_name": "elgabo1",
"status_tweet": 0,
"text": "#profeco Deurope Gran Sur tapa los sellos de suspensiĆ³n con propaganda"
},
{
"created_at": "2013-10-02 06:50:01",
"id_tweet": "385295588377387008",
"id_user": 12375562,
"name": "elgabo1",
"photo": "https://pbs.twimg.com/profile_images/1827710728/45d1be6d2e0f1c710814e098d6f56c12_normal.png",
"screen_name": "elgabo1",
"status_tweet": 1,
"text": "#caspoliciadf Delegacion coyoacan"
}
]
}
I want to set the status_tweet:1 from id_tweet:"389193311908413440"
I can get the element of the array with the id_tweet:"389193311908413440"
r.db('twitter_settings').table('tweets_service').filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})
.map(function(d){
return {
"tweets": d("tweets").filter({'id_tweet':'665305939294056448'})
}
}
)
But when i try to update the field I get an error:
r.db('twitter_settings').table('tweets_service').filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})
.map(function(d){
return {
"tweets": d("tweets").filter({'id_tweet':'665305939294056448'})
}
}
)
.update({"status_tweet": 1})
e: Expected type SELECTION but found SEQUENCE:
VALUE SEQUENCE in:
r.db("twitter_settings").table("tweets_service").filter({id: "3065e957-56e9-4084-8e32-bb4de8d9265a"}).map(function(var_341) { return {tweets: var_341("tweets").filter({id_tweet: "665305939294056448"})}; }).update({status_tweet: 1})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Thanks in advance
We cannot update/delete after using map.
So change your function to run map inside the update function. Something like this will work.
r.db('twitter_settings').table('tweets_service')
.filter({id:'3065e957-56e9-4084-8e32-bb4de8d9265a'})
.update(function(doc) {
return {
tweets: doc('tweets').map(function(tweet) {
return r.branch(
tweet('id_tweet').eq('389193311908413440'),
tweet.merge({status_tweet:1}),
tweet)
})
}
})

Sorting a json array based on a key using knockout

I have a jsonarray something like this:
var jsonarr= [{"displayName":"Rachita Jain","phoneNumbers":[{"value":"(787) 989-6756"},{"value":"
(897) 867-4666"}]},{"displayName":"Akanksha Mittal","phoneNumbers":[{"value":"(678) 456-4677"}]}]
I want to sort this using knockout based on displayName.
Use sort function array:
var jsonarr = [{
"displayName": "Rachita Jain",
"phoneNumbers": [{
"value": "(787) 989-6756"
}, {
"value": "(897) 867-4666"
}]
}, {
"displayName": "Akanksha Mittal",
"phoneNumbers": [{
"value": "(678) 456-4677"
}]
}];
jsonarr.sort(function (item1, item2) { return (item1.displayName > item2.displayName) ? 1 : -1 });
console.log(jsonarr);
See fiddle

jsTree - loading subnodes via ajax on demand

I'm trying to get a jsTree working with on demand loading of subnodes. My code is this:
jQuery('#introspection_tree').jstree({
"json_data" : {
"ajax" : {
url : "http://localhost/introspection/introspection/product"
}
},
"plugins" : [ "themes", "json_data", "ui" ]
});
The json returned from the call is
[
{
"data": "Kit 1",
"attr": {
"id": "1"
},
"children": [
[
{
"data": "Hardware",
"attr": {
"id": "2"
},
"children": [
]
}
],
[
{
"data": "Software",
"attr": {
"id": "3"
},
"children": [
]
}
]
]
}
.....
]
Each element could have a lot of children, the tree is going to be big. Currently this is loading the whole tree at once, which could take some time. What do I have to do to implement on-demand-loading of child-nodes when they are opened by the user?
Thanks in advance.
Irishka pointed me in the right direction, but does not fully resolve my problem. I fiddled around with her answer and came up with this. Using two different server functions is done only for clarity. The first one lists all products at top level, the second one lists all children of a given productid:
jQuery("#introspection_tree").jstree({
"plugins" : ["themes", "json_data", "ui"],
"json_data" : {
"ajax" : {
"type": 'GET',
"url": function (node) {
var nodeId = "";
var url = ""
if (node == -1)
{
url = "http://localhost/introspection/introspection/product/";
}
else
{
nodeId = node.attr('id');
url = "http://localhost/introspection/introspection/children/" + nodeId;
}
return url;
},
"success": function (new_data) {
return new_data;
}
}
}
});
The json data returned from the functions is like this (notice the state=closed in each node):
[
{
"data": "Kit 1",
"attr": {
"id": "1"
},
"state": "closed"
},
{
"data": "KPCM 049",
"attr": {
"id": "4"
},
"state": "closed"
},
{
"data": "Linux BSP",
"attr": {
"id": "8"
},
"state": "closed"
}
]
No static data is needed, the tree is now fully dynamic on each level.
I guess it would be nice to display by default first level nodes and then the children will be loaded on demand. In that case the only thing you have to modify is to add "state" : "closed" to the nodes whose child nodes are going to be loaded on demand.
You might wish to send node's id in ajax call so you modify your code
"json_data": {
//root elements to be displayed by default on the first load
"data": [
{
"data": 'Kit 1',
"attr": {
"id": 'kit1'
},
"state": "closed"
},
{
"data": 'Another node of level 1',
"attr": {
"id": 'kit1'
},
"state": "closed"
}
],
"ajax": {
url: "http://localhost/introspection/introspection/product",
data: function (n) {
return {
"nodeid": $.trim(n.attr('id'))
}
}
}
}
From jsTree documentation
NOTE:
If both data and ajax are set the initial tree is rendered from the data string. When opening a closed node (that has no loaded children) an AJAX request is made.
you need to set root elements as tree data on page load and then you will be able to retrieve their children with an ajax request
$("#introspection_tree").jstree({
"plugins": ["themes", "json_data", "ui"],
"json_data": {
//root elements
"data": [{"data": 'Kit 1', "attr": {"id": 'kit1'}} /*, ... */], //the 'id' can not start with a number
"ajax": {
"type": 'POST',
"data": {"action": 'getChildren'},
"url": function (node) {
var nodeId = node.attr('id'); //id="kit1"
return 'yuorPathTo/GetChildrenScript/' + nodeId;
},
"success": function (new_data) {
//where new_data = node children
//e.g.: [{'data':'Hardware','attr':{'id':'child2'}}, {'data':'Software','attr':{'id':'child3'}}]
return new_data;
}
}
}
});
See my answer to a similar question here (the old part) for more details
I spended hours on this problem. Finally i got it that way:
$("#resourceTree").jstree({
"types": {
"default": {
"icon": "fa fa-folder-open treeFolderIcon",
}
},
"plugins": ["json_data", "types", "wholerow", "search"],
"core": {
"multiple": false,
"data": {
"url" : function(node){
var url = "rootTree.json";
if(node.id === "specialChildSubTree")
url = "specialChildSubTree.json";
return url;
},
"data" : function(node){
return {"id" : node.id};
}
}
},
});
rootTree.json:
[
{
"text": "Opened root folder",
"state": {
"opened": true
},
"children": [
{
"id" : "specialChildSubTree",
"state": "closed",
"children":true
}
]
}
]
specialChildSubTree.json:
[
"Child 1",
{
"text": "Child 2",
"children": [
"One more"
]
}
]
So i mark the node that become the parent of the ajax loaded subtree with an id, i watch for in the core configuration.
NOTE:
That node must have the "state" : "closed" parameter and it must have
the parameter "children" : true.
I am using jsTree.js in version 3.3.3
Above solution is all fine. Here I am also providing similar working solution and very simple for lazy loading of nodes using ajax call vakata. When your API works like
https://www.jstree.com/fiddle/?lazy
and for getting any child nodes
https://www.jstree.com/fiddle/?lazy&id=2
for explanation and for complete solution you can have a look at https://everyething.com/Example-of-jsTree-with-lazy-loading-and-AJAX-call
<script type="text/javascript">
$(function () {
$('#SimpleJSTree').jstree({
'core' : {
'data' : {
'url' : "https://www.jstree.com/fiddle/?lazy",
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
});
</script>

Resources