liurubin / learn_bj Goto Github PK
View Code? Open in Web Editor NEWbj
bj
function list_to_tree(list) {
var map = {}, node, roots = [], i;
for (i = 0; i < list.length; i += 1) {
map[list[i].id] = i; // initialize the map
list[i].children = []; // initialize the children
}
for (i = 0; i < list.length; i += 1) {
node = list[i];
if (node.parentId !== "0") {
// if you have dangling branches check that map[node.parentId] exists
list[map[node.parentId]].children.push(node);
} else {
roots.push(node);
}
}
return roots;
}
var entries = [{
"id": "12",
"parentId": "0",
"text": "Man",
"level": "1",
"children": null
},
{
"id": "6",
"parentId": "12",
"text": "Boy",
"level": "2",
"children": null
},
{
"id": "7",
"parentId": "12",
"text": "Other",
"level": "2",
"children": null
},
{
"id": "9",
"parentId": "0",
"text": "Woman",
"level": "1",
"children": null
},
{
"id": "11",
"parentId": "9",
"text": "Girl",
"level": "2",
"children": null
}
];
console.log(list_to_tree(entries));
function fib(n){
let arr = [1,1]
let i = 2
while(i<=n){
arr[i] = arr[i-1]+arr[i-2]
i++
}
return arr[n]
}
let array = [
["Viola", "Orsino"],
["Orsino", "Olivia"],
["Olivia", "Cesario"]
];
let resultMap = array.reduce((memo,curr) => {
memo[curr[0]] = curr[1]
return memo
},{})
const numbers = [-1,0,2,3,4,5,6];
const doubleNumbers = numbers.reduce((acc, curr) => {
if (curr > 0) {
acc.push(curr * 2)
}
return acc;
},[])
const array = ['a','b','a','c','d','c','e']
const orderedArray = array.reduce((acc, cur) => {
acc.indexOf(cur) === -1 ? acc.push(cur) : acc
return acc
},[])
let books = [
{
title: "Showings",
author: "Julian of Norwich",
checkouts: 45
},
{
title: "The Triads",
author: "Gregory Palamas",
checkouts: 32
},
{
title: "The Praktikos",
author: "Evagrius Ponticus",
checkouts: 29
}
];
let total = books.map(function (b) { return b.checkouts })
.reduce(function (p, c) { return p + c} )
let flatted = [[1,2],[3,4],[4,5]].reduce((acc, cur) => acc.concat(cur), [])
const numbers = [3,4,7,8,9]
const max = numbers.reduce((acc, cur) => {
if (cur > acc) {
acc = cur
}
return acc
})
const numbers = [3,4,6,3,10,90,6,3]
const result = numbers.reduce((acc, cur) => {
acc[cur] ? acc[cur]++ : acc[cur] = 1
return acc
}, {})
var tree = {
id: 1,
children: [
{
id: 3,
parentId: 1,
children: [
{
id: 5,
parentId: 3,
children: []
}
]
}
]
}
// We will flatten it down to an object that just holds the id with the object
var lookup = {}
function mapIt (node) {
lookup[node.id] = node;
//recursive on all the children
node.children && node.children.forEach(mapIt);
}
mapIt(tree)
// This takes a node and loops over the lookup hash to get all of the ancestors
function findAncestors (nodeId) {
var ancestors = []
var parentId = lookup[nodeId] && lookup[nodeId].parentId
while(parentId !== undefined) {
ancestors.unshift(parentId)
parentId = lookup[parentId] && lookup[parentId].parentId
}
return ancestors;
}
// Let us see if it works
console.log("5: ", findAncestors(5))
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.