Travase Objects and four method of array
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var iterableObj = {
"lastName":'luo',
"firstName":'xu',
"age":19
}
let keys = Object.keys(iterableObj); //return array of keys in the iterableObj
// console.log(keys) //Array(3) [ "lastName", "firstName", "age" ]
let vals = Object.values(iterableObj);
//console.log(vals) //["luo","xu",19]
let entries = Object.entries(iterableObj)
//console.log(entries) //[["lastName", "luo"],[ "firstName", "xu"],["age", 19]] 2-D arrays
const companies = [ //array
{name: "Company One", category: "Finance", start: 1981, end: 2003}, //object
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
//1. forEach()
for (let i = 0; i < companies.length; i++){
// console.log(companies[1]);
}
companies.forEach(function (company, index , companies) { //we can pass 2 params
// console.log(company);
});
//2. filter()
//store the person'age over 21, who can drink
var canDrink = [];
for (let i=0; i< ages.length ; i++){
if (ages[i] >= 21){
canDrink.push(ages[i]);
}
}
canDrink = ages.filter(function (age) {
if (age >= 21){
return true;
}
});
canDrink = ages.filter(age => age >=21);
console.log(canDrink);
//get companies that lasted ten years or more
const lastedTenYears = companies.filter((company, index) => (company.end - company.start >=10));
console.log(lastedTenYears);
//3. map()
var testMap = companies.map(function (company,index ) {
return company.name;
}); //Array(9) [ "Company One", "Company Two", "Company Three", "Company Four", "Company Five", "Company Six", "Company Seven", "Company Eight", "Company Nine" ]
testMap = companies.map(function (company ) {
return `${company.name } [${company.end} - ${company.start}]`;
}) //Array(9) [ "Company One [2003 - 1981]", "Company Two [2008 - 1992]", "Company Three [2007 - 1999]", "Company Four [2010 - 1989]", "Company Five [2014 - 2009]", "Company Six [2010 - 1987]", "Company Seven [1996 - 1986]", "Company Eight [2016 - 2011]", "Company Nine [1989 - 1981]" ]
console.log(testMap)
//4. sort()
//sort by start year
var sorted = companies.sort(function (a,b ) {
if (a.start > b.start){
return 1;
}else {
return -1;
}
});
sorted = companies.sort((a,b) => a >b ? 1 : -1);
//sort ages
sorted =ages.sort((a,b) => a-b);
console.log(sorted);
//5. reduce()
var total = ages.reduce(function (total, age) {
return total+age;
});
total = ages.reduce((total, age) => total +age);
console.log(total);
</script>
</body>
</html>
Travase Objects and four method of array的更多相关文章
- Why does typeof array with objects return “Object” and not “Array”?
https://stackoverflow.com/questions/4775722/check-if-object-is-an-array One of the weird behaviour a ...
- OBJC运行时方法替换(Method swizzling)
在上周associated objects一文中,我们开始探索Objective-C运行时的一些黑魔法.本周我们继续前行,来讨论可能是最受争议的运行时技术:method swizzling. Me ...
- iOS 判断数组array中是否包含元素a,取出a在array中的下标+数组方法详解
目前找到来4个解决办法,第三个尤为简单方便 NSArray * arr = @["]; //是否包含 "]) { NSInteger index = [arr indexOfObj ...
- JavaScript : Array assignment creates reference not copy
JavaScript : Array assignment creates reference not copy 29 May 2015 Consider we have an array var a ...
- matlab中cell array的理解
1. matlab中有一个函数iscell() 用于判断一个数组是不是cell array 参考:MATLAB Function Reference iscell Determine whether ...
- JavaScript Array.map
Array.prototype.map() History Edit This article is in need of a technical review. Table of Contents ...
- Design Pattern ->Factory Method
Layering & Contract Philosophy With additional indirection Factory Method The example code is as ...
- JavaScript 中Array数组的几个内置函数
本文章内容均参考<JavaScript高级程序设计第三版> 今天在看JavaScript书籍的时候,看到之前没有了解过的JavaScript中Array的几个内置函数对象,为了之后再开发工 ...
- ios method swizzling
阅读器 iOS开发iOS 本文由TracyYih[博客]翻译自NSHipster的文章Method Swizzling. 在上周associated objects一文中,我们开始探索Ob ...
随机推荐
- geoserver wfs属性查询
Geoserver参考连接:http://docs.geoserver.org/latest/en/user/services/wfs/reference.html 使用实例: http://loca ...
- Word文档分节设置页码
在一篇论文中需要将摘要和目录作为一部分设置罗马数字页码,正文部分设置阿拉伯数字页码. 大致效果如下图所示: 这里面用到了分节符,步骤如下: 1 :点击开始菜单栏下 显示/隐藏编辑标记 2:点击插入菜单 ...
- Shell脚本深入教程(1):快速入门
Shell脚本基础入门 Bash注释 Bash只支持单行注释,使用#开头的都被当作注释语句: # 整行注释 echo hello world # 行尾注释 通过Bash的一些特性,可以取巧实现多行注释 ...
- 文件系统(02):基于SpringBoot框架,管理Xml和CSV文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.XML文档 XML是可扩展标记语言,是一种用于标记电子文件使其具有结构性的标记语言.标记指计算机所能理解的信息符号,通过 ...
- 7天用Go动手写/从零实现Web框架Gee
设计一个框架 大部分时候,我们需要实现一个 Web 应用,第一反应是应该使用哪个框架.不同的框架设计理念和提供的功能有很大的差别.比如 Python 语言的 django和flask,前者大而全,后者 ...
- Jmeter源码编译缺bouncycastle包
Jmeter源码下载后install没问题,运行newDrive时会包包不存在,因为下载时缺少三个包没下载成功,点击链接下载并放到lib目录下即可 下载
- TensorFlow相关博客
Tensor官方教程 极客学院TensorFlow中文文档 xf__mao的博客
- meta 的作用 搜集
Meta标签中的format-detection属性及含义 format-detection翻译成中文的意思是“格式检测”,顾名思义,它是用来检测html里的一些格式的,那关于meta的forma ...
- Net Core 中WebAPI有关 Session的设置,及获取
步骤一: 在Startup 文件中做相应的设置 ConfigureServices方法里添加 //ConfigureServices添加: services.AddSession(options =& ...
- Go语言实现:【剑指offer】连续子数组的最大和
该题目来源于牛客网<剑指offer>专题. HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向 ...