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 ...
随机推荐
- Git详解之其他系统结合
前言 世界不是完美的.大多数时候,将所有接触到的项目全部转向 Git 是不可能的.有时我们不得不为某个项目使用其他的版本控制系统(VCS, Version Control System ),其中比较常 ...
- 使用Allure+testNG自动生成漂亮强大的测试用例报告
最近领导让我找一个可以每次打包自动生成测试用例的东西,jenkins或者idea都可以, 最后找到了这个allure,也踩了很多坑,废话不多说!,总结一下: 1 使用原生allure 添加依赖: &l ...
- OGG主从表结构不同步,出现OGG-01296错误
一.Cause ogg的err日志出现以下报错 2019-09-10 16:36:55 WARNING OGG-01003 Oracle GoldenGate Delivery for Oracle, ...
- python实现一个客户端与服务端的通信
函数介绍 Socket对象方法: 服务端: 函数 描述 .bind() 绑定地址关键字,AF_INET下以元组的形式表示地址.常用bind((host,port)) .listen() 监听TCP,可 ...
- Head First设计模式——状态模式
糖果机 如下糖果机工作状态图,我们对这个状态图进行编码实现糖果机的工作过程 这个状态图的每个圆圈代表一个状态,可以看到有4个状态同时又4个动作,分别是:“投入1元钱”.“退回1元钱”.“转动曲柄”.“ ...
- python中线程共享资源问题的解决
线程跟进程有些相似,有时被称作轻量级的进程,但不同的是,所有的线程运行在同一个进程中,共享相同的运行坏境. 进程和线程都是实现多任务的一种方式,例如:在同一台计算机上能同时运行多个QQ(进程),一个Q ...
- 编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
需求:编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n package com.Summer_0511.cn; impo ...
- JSP&Servlet学习笔记----第1/2章
HTML(HyperText Markup Language):超文本标记语言 HTTP(HyperText Transfer Protocol):超文本传输协议 URL(Uniform Resour ...
- jmeter 源码修改返回值中文Unicode编码问题
修改jmeter源码,可能会对其他格式的responseData有一定影响,图片或者其他 在 ListenerNotifier 类中找到 notifyListeners 方法,在其下面添加如下代码: ...
- sqlserver install on linux chapter one
Hello The MS open the source to let people download source. You may ask where to download ? Ask goog ...