I'm working with a tree of hierarchical data, on the server side. I need to compute some parameters for each branch with scheduled functions after updating the tree hierarchy.
My problem is: how do I take a d3.hierarchy on one side, and an array of new documents on the other, and get a new hierarchy?
here is some data:
let collection = [
{ id: 0, rank: 0 },
{ id: 1, parent: 0},
{ id: 2, parent: 0},
{ id: 3, parent: 1},
{ id: 4, parent: 1},
{ id: 5, parent: 1},
{ id: 6, parent: 1},
{ id: 7, parent: 2},
{ id: 8, parent: 2},
{ id: 9, parent: 2},
{ id: 10, parent: 3},
{ id: 11, parent: 3},
{ id: 12, parent: 3},
{ id: 13, parent: 3},
{ id: 14, parent: 4},
{ id: 15, parent: 5},
{ id: 16, parent: 5},
{ id: 17, parent: 6},
{ id: 18, parent: 6},
{ id: 19, parent: 6},
{ id: 20, parent: 6},
{ id: 21, parent: 7},
{ id: 22, parent: 7},
{ id: 23, parent: 7},
{ id: 24, parent: 8},
{ id: 25, parent: 8},
{ id: 26, parent: 9},
{ id: 27, parent: 9},
]
let root = d3
.stratify()
.id(d => d.id)
.parentId(d => d.parent)(collection)
console.log(root)
vvvvvvvvvvvv prints vvvvvvvvv
Node {
data: { id: 0, rank: 0 },
height: 3,
depth: 0,
parent: null,
id: '0',
children:
[ Node {
data: [Object],
height: 2,
depth: 1,
parent: [Circular],
id: '1',
children: [Array] } ] }
const newElements = [
{ id: 28, parent: 16},
{ id: 29, parent: 6},
{ id: 30, parent: 12},
{ id: 31, parent: 19},
{ id: 32, parent: 28},
{ id: 33, parent: 7},
{ id: 34, parent: 33},
]
So root is the ('object'??) I'm working with on the server, and newElements the things I would like to add to root.
Related
const SHOP_DATA = [
{
title: 'Hats',
items: [
{
id: 1,
name: 'Brown Brim',
imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png',
price: 25,
},
{
id: 2,
name: 'Blue Beanie',
imageUrl: 'https://i.ibb.co/ypkgK0X/blue-beanie.png',
price: 18,
},
{
id: 3,
name: 'Brown Cowboy',
imageUrl: 'https://i.ibb.co/QdJwgmp/brown-cowboy.png',
price: 35,
},
{
id: 4,
name: 'Grey Brim',
imageUrl: 'https://i.ibb.co/RjBLWxB/grey-brim.png',
price: 25,
},
{
id: 5,
name: 'Green Beanie',
imageUrl: 'https://i.ibb.co/YTjW3vF/green-beanie.png',
price: 18,
},
{
id: 6,
name: 'Palm Tree Cap',
imageUrl: 'https://i.ibb.co/rKBDvJX/palm-tree-cap.png',
price: 14,
},
{
id: 7,
name: 'Red Beanie',
imageUrl: 'https://i.ibb.co/bLB646Z/red-beanie.png',
price: 18,
},
{
id: 8,
name: 'Wolf Cap',
imageUrl: 'https://i.ibb.co/1f2nWMM/wolf-cap.png',
price: 14,
},
{
id: 9,
name: 'Blue Snapback',
imageUrl: 'https://i.ibb.co/X2VJP2W/blue-snapback.png',
price: 16,
},
],
},
{
title: 'Sneakers',
items: [
{
id: 10,
name: 'Adidas NMD',
imageUrl: 'https://i.ibb.co/0s3pdnc/adidas-nmd.png',
price: 220,
},
{
id: 11,
name: 'Adidas Yeezy',
imageUrl: 'https://i.ibb.co/dJbG1cT/yeezy.png',
price: 280,
},
{
id: 12,
name: 'Black Converse',
imageUrl: 'https://i.ibb.co/bPmVXyP/black-converse.png',
price: 110,
},
{
id: 13,
name: 'Nike White AirForce',
imageUrl: 'https://i.ibb.co/1RcFPk0/white-nike-high-tops.png',
price: 160,
},
{
id: 14,
name: 'Nike Red High Tops',
imageUrl: 'https://i.ibb.co/QcvzydB/nikes-red.png',
price: 160,
},
{
id: 15,
name: 'Nike Brown High Tops',
imageUrl: 'https://i.ibb.co/fMTV342/nike-brown.png',
price: 160,
},
{
id: 16,
name: 'Air Jordan Limited',
imageUrl: 'https://i.ibb.co/w4k6Ws9/nike-funky.png',
price: 190,
},
{
id: 17,
name: 'Timberlands',
imageUrl: 'https://i.ibb.co/Mhh6wBg/timberlands.png',
price: 200,
},
],
},
]
export default SHOP_DATA;
const UiUxCategories = () => {
return (
<div>
{
Object.keys(SHOP_DATA).map((key) => {
const products = SHOP_DATA[key];
return (
<CategoryPreview key={key} title={key} products={products} />
)
})
}
</div>
)
}
error- products.filter is no a function in categoryPreview component
const CategoryPreview = ({ title, products }) => {
return (
<div >
<h2>{title}</h2>
<div>
{products
.filter((_, idx) => idx < 4)
.map((product) => (
<ProductCard key={product.id} product={product} />
))}
</div>
</div>
);
}
As i cleaned up my computer and reinstalled chrome latest version and when i tried using grepper code extension it was not working. It was not showing any answers and not even letting me add a new answer. I tried researching for a while but couldn't find a solution to it.
Then i tried opening it inside the firefox and it was working perfectly and i can even see suggestions and even add new answers.
Then When i checked inside my console log i found some errors while doing a google search and the errors were related to the grepper cross origin issue. I am attaching the screenshot of the issue that occurred
I solved this error by allowing the cross origin access using the following extension :
https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
and then just reloading the page the errors were gone and you can also check if it is working or not inside the test panel of the extension :
https://webbrowsertools.com/test-cors/
I am attaching some screenshots that will define my solution.
/* #api */
define([
'jquery',
'mageUtils'
], function ($, utils) {
'use strict';
var types = [
{
title: 'Visa',
type: 'VI',
pattern: '^4\\d*$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVV',
size: 3
}
},
{
title: 'MasterCard',
type: 'MC',
pattern: '^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'American Express',
type: 'AE',
pattern: '^3([47]\\d*)?$',
isAmex: true,
gaps: [4, 10],
lengths: [15],
code: {
name: 'CID',
size: 4
}
},
{
title: 'Diners',
type: 'DN',
pattern: '^(3(0[0-5]|095|6|[8-9]))\\d*$',
gaps: [4, 10],
lengths: [14, 16, 17, 18, 19],
code: {
name: 'CVV',
size: 3
}
},
{
title: 'Discover',
type: 'DI',
pattern: '^(6011(0|[2-4]|74|7[7-9]|8[6-9]|9)|6(4[4-9]|5))\\d*$',
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CID',
size: 3
}
},
{
title: 'JCB',
type: 'JCB',
pattern: '^35(2[8-9]|[3-8])\\d*$',
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVV',
size: 3
}
},
{
title: 'UnionPay',
type: 'UN',
pattern: '^(622(1(2[6-9]|[3-9])|[3-8]|9([[0-1]|2[0-5]))|62[4-6]|628([2-8]))\\d*?$',
gaps: [4, 8, 12],
lengths: [16, 17, 18, 19],
code: {
name: 'CVN',
size: 3
}
},
{
title: 'Maestro International',
type: 'MI',
pattern: '^(5(0|[6-9])|63|67(?!59|6770|6774))\\d*$',
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Maestro Domestic',
type: 'MD',
pattern: '^6759(?!24|38|40|6[3-9]|70|76)|676770|676774\\d*$',
gaps: [4, 8, 12],
lengths: [12, 13, 14, 15, 16, 17, 18, 19],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Hipercard',
type: 'HC',
pattern: '^((606282)|(637095)|(637568)|(637599)|(637609)|(637612))\\d*$',
gaps: [4, 8, 12],
lengths: [13, 16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Elo',
type: 'ELO',
pattern: '^((509091)|(636368)|(636297)|(504175)|(438935)|(40117[8-9])|(45763[1-2])|' +
'(457393)|(431274)|(50990[0-2])|(5099[7-9][0-9])|(50996[4-9])|(509[1-8][0-9][0-9])|' +
'(5090(0[0-2]|0[4-9]|1[2-9]|[24589][0-9]|3[1-9]|6[0-46-9]|7[0-24-9]))|' +
'(5067(0[0-24-8]|1[0-24-9]|2[014-9]|3[0-379]|4[0-9]|5[0-3]|6[0-5]|7[0-8]))|' +
'(6504(0[5-9]|1[0-9]|2[0-9]|3[0-9]))|' +
'(6504(8[5-9]|9[0-9])|6505(0[0-9]|1[0-9]|2[0-9]|3[0-8]))|' +
'(6505(4[1-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-8]))|' +
'(6507(0[0-9]|1[0-8]))|(65072[0-7])|(6509(0[1-9]|1[0-9]|20))|' +
'(6516(5[2-9]|6[0-9]|7[0-9]))|(6550(0[0-9]|1[0-9]))|' +
'(6550(2[1-9]|3[0-9]|4[0-9]|5[0-8])))\\d*$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Aura',
type: 'AU',
pattern: '^5078\\d*$',
gaps: [4, 8, 12],
lengths: [19],
code: {
name: 'CVC',
size: 3
}
}
];
return {
/**
* #param {*} cardNumber
* #return {Array}
*/
getCardTypes: function (cardNumber) {
var i, value,
result = [];
if (utils.isEmpty(cardNumber)) {
return result;
}
if (cardNumber === '') {
return $.extend(true, {}, types);
}
for (i = 0; i < types.length; i++) {
value = types[i];
I have a Business model and an Hour model. The Business model overrides the protected $with method to eager load it's hours() hasMany relationship.
When I ::first() a given business I receive something like this:
App\Business {#770
id: 5,
user_id: 5,
name: "Wehner-Hudson",
slug: "wehner-hudson",
lat: "55.33593500",
lng: "112.34818600",
created_at: "2018-01-04 13:00:48",
updated_at: "2018-01-04 13:00:48",
hours: Illuminate\Database\Eloquent\Collection {#753
all: [
App\Hour {#802
id: 13,
business_id: 5,
weekday_id: 3,
open: 1,
split_shift: 1,
},
App\Hour {#803
id: 14,
business_id: 5,
weekday_id: 5,
open: 0,
split_shift: 1,
},
App\Hour {#804
id: 15,
business_id: 5,
weekday_id: 2,
open: 1,
split_shift: 0,
},
],
},
},
],
}
I would like to key the hours: Illuminate\Database\Eloquent\Collection {#753 by weekday_id to facilitate processing on the client side. Something like this:
Illuminate\Database\Eloquent\Collection {#763
all: [
1 => App\Hour {#796
id: 1,
business_id: 1,
weekday_id: 1,
open: 1,
split_shift: 1,
},
5 => App\Hour {#767
id: 2,
business_id: 1,
weekday_id: 5,
open: 0,
split_shift: 0,
},
2 => App\Hour {#765
id: 3,
business_id: 1,
weekday_id: 2,
open: 1,
split_shift: 1,
},
],
}
I tried to use keyBy on the relationship in the Business model:
public function hours()
{
return $this->hasMany(Hour::class)->keyBy('weekday_id');
}
But it is not working, as I believe that at that point the returned object is a builder, not a collection.
Try to define an accessor, like this:
public function getHoursByWeekdayAttribute()
{
return $this->hours->keyBy('weekday_id');
}
What about using groupby in your controller.
Business::with(['hours' => function($query){ $query->groupBy('weekend_id'); }])->get();
In 2D I write:
Dim matr2D(,) As Integer = { {10, 20, 30}, {11, 21, 31}, {12, 22, 32} }.
In 3D?
Found:
Dim matr3D(, ,) As Integer = { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }
https://msdn.microsoft.com/it-it/library/2yd9wwz4.aspx
i have an array of hashes, eg
array = [
{ id: 1, name: 'root' parent: null},
{ id: 2, name: 'first' parent: 1},
{ id: 5, name: 'first step' parent: 2},
{ id: 6, name: 'second step' parent: 2},
{ id: 3, name: 'second' parent: 1},
{ id: 7, name: 'first step' parent: 3},
{ id: 4, name: 'third' parent: 1},
{ id: 2, name: 'first' parent: 1},
]
and i need to build something like that
hash = {
{
id: 1,
name: 'root',
parent: null,
childrens: [
{ id: 2,
name: 'first',
parent: 1,
childrens: [
{
id: 5,
name: 'first step',
parent: 2
},
{
id: 6,
name: 'second step',
parent: 2
},
]},
...
}
I am newbie at ruby and doesnot understand how to do this.
Probably i need to use recursive functions? Or not?
# Put all your nodes into a Hash keyed by id This assumes your objects are already Hashes
object_hash = nodes.index_by {|node| node[:id]}
object_hash[0] = {:root => true}
# loop through each node, assigning them to their parents
object_hash.each_value {|node|
next if node[:root]
children = object_hash[node[:parent_id]][:children] ||= []
children << node
}
#then your should have the structure you want and you can ignore 'object_hash' variable
tree = object_hash[0]
From the answer:
Algorithm for parsing a flat tree into a non-flat tree