Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
Build Array Tree with Lodash
const _ = require('lodash');
const arr = [{
"id": 1,
"parentID": 0,
"name": "carl"
},{
"id": 2,
"parentID": 1,
"name": "kiko"
},{
"id": 3,
"parentID": 1,
"name": "yuchen"
},{
"id": 4,
"parentID": 2,
"name": "hello"
}];
const mainKey = "id";
const parentKey = "parentID";
const a = _.keyBy(arr, mainKey);
const result = _
.chain(arr)
.groupBy(parentKey)
.forEach(function(v,k){
k!='0' && (a[k].children=(a[k].children||[]).concat(v));
})
.result('0')
.value();
console.log(JSON.stringify(result));
Convert Array to Hashmap
const _ = require('lodash');
const arr = [{
"id": 1,
"parentID": 0,
"name": "carl"
},{
"id": 2,
"parentID": 1,
"name": "kiko"
},{
"id": 3,
"parentID": 1,
"name": "yuchen"
},{
"id": 4,
"parentID": 2,
"name": "hello"
},{
"id": 5,
"parentID": 0,
"name": "fly"
}];
const map = _.keyBy(arr, 'id');
console.log(JSON.stringify(map));
Find all Parents in Array
const _ = require('lodash');
const arr = [{
"id": 1,
"parentID": 0,
"name": "carl"
},{
"id": 2,
"parentID": 1,
"name": "kiko"
},{
"id": 3,
"parentID": 1,
"name": "yuchen"
},{
"id": 4,
"parentID": 2,
"name": "hello"
},{
"id": 5,
"parentID": 0,
"name": "fly"
}];
const map = _.keyBy(arr, 'id');
console.log(JSON.stringify(map));
function findAncestors (nodeID) {
let ancestors = [];
let parentID = map[nodeID] && map[nodeID].parentID + "";
ancestors.unshift(nodeID);
while(parentID !== undefined && parentID !== "0") {
ancestors.unshift(parentID)
parentID = map[parentID] && map[parentID].parentID + "";
}
return ancestors;
}
const parents = findAncestors("4");
console.log(JSON.stringify(parents));
References:
https://danmartensen.svbtle.com/converting-a-tree-to-a-list-in-javascript
https://blog.wax-o.com/2014/01/how-to-find-deep-and-get-parent-in-javascript-nested-objects-with-recursive-functions-and-the-reference-concept-level-beginner/
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
分享到:
相关推荐
Serverless Design Patterns and Best Practices: Build, secure, and deploy enterprise ready serverless applications with AWS to improve developer productivity
描述在五分钟或更短的时间内引导一个新的Node.js Serverless项目。特征使用在下部署到 。 的包结构。 使用快速,可靠和安全的依赖项管理。 带有可配置选项和参数的示例,这些示例和参数由提供。 使用使用进行。 代码...
提供的资源是名为`tencentcloud_sdk_python-3.0.203-py2.py3-none-any.whl`的轮子文件,这是一种预编译的Python包格式,适用于Python 2和3。安装步骤如下: 1. 首先,确保你的Python环境已经安装了`pip`,这是...
# running Serverless version 1.72.0$ docker run --rm amaysim/serverless:1.72.0 serverless --help在本地构建图像如果您要构建和使用自己的本地映像 # build image locally with latest Serverless version$ ...
Serverless Web Applications with React and Firebase 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
serverless-nodejs-starter 具有Webpack,自定义域和单元测试支持的无服务器框架的Node.js入门 安装后,您可以在几分钟之内创建和部署具有最新ES6功能的功能,并加入棉绒和格式。 使用插件和 。 它支持: 处理...
Serverless computing with Azure and .NET 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
Serverless Applications with Node.js teaches you to design and build serverless web apps on AWS using JavaScript, Node, and Claudia.js. You'll master the basics of writing AWS Lambda functions, along ...
Inside, you’ll discover what Claudia.js brings to the table as you build and deploy a scalable event-based serverless application, based around a pizzeria that’s fully integrated with AWS services,...
Serverless Architectures with AWS begins with an introduction to the serverless model and helps you get started with AWS and AWS Lambda. You'll also get to grips with other capabilities of the AWS ...
paced journey through building real-time applications with Firebase features such as Cloud Storage, Cloud Function, Hosting and the Realtime Database. We will learn how to secure our application by ...
在本地运行API网关使用serverless offline start 支持单元测试运行npm test以运行测试 正确错误消息的源映射 错误消息显示正确的行号 使用CloudWatch进行生产 使用ESLint整理代码 为您的阶段添加环境变量 无需管理...
You will also learn to troubleshoot and monitor your app and master AWS lambda programming concepts with API references. Moving on, you will also learn how to scale up serverless applications and ...
《Python库 Tencentcloud-sdk-python-tsf 3.0.435详解》 在Python的开发环境中,Tencentcloud-sdk-python-tsf库扮演着至关重要的角色。它为开发者提供了与腾讯云TSF(Tencent Serverless Framework)进行交互的工具...