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 ...
随机推荐
- Flask接口返回JSON格式数据自动解析
一 自定义一个response类 from flask import Response, jsonify # 定义response返回类,自动解析json class JSONResponse(Res ...
- CSS学习 | 思维导图
CSS样式
- vue 新建脚手架项目npm命令
使用国外原镜像 npm install -g @vue/cli //yarn global add @vue/cli 使用淘宝镜像 cnpm install -g @vue/ ...
- 网络收发与Nginx事件间的对应关系
主机A可以想象是家里面的一台笔记本,也就是客户端,主机B可以想象成服务器上跑着nginx 主机A发送一个http的get请求到主机B经历了哪些请求. 在数据流: 应用层发送了一个get请求,传输层中, ...
- ORA-12547: TNS: 丢失连接
今天服务器挂掉了,公司的人弄了一下,,把服务器修好了,,但是我本地链接数据库一直报这个ORA-12547: TNS: 丢失连接,是服务器上的TNS监听没有启动,需要重启一下,
- FFMPEG学习----使用SDL构建音频播放器
ffmpeg版本:ffmpeg-20160413-git-0efafc5 #include <stdio.h> #include <stdlib.h> #include < ...
- LoadIcon的使用
LoadIcon msdn: Loads the specified icon resource from the executable (.exe) file associated with an ...
- 【5min+】 这些C#的运算符您都认识吗?
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...
- Codeforces_451_B
http://codeforces.com/problemset/problem/451/B 取前后第一个不满足条件的位置,逆序,判断. #include<cstdio> #include ...
- 2.5D(伪3D)站点可视化第一弹
楔子 最近要做一个基站站点的可视化呈现项目. 我们首先尝试的是三维的可视化技术来程序,但是客户反馈的情况是他们的客户端电脑比较差,性能效率都会不好,甚至有的还是云主机. 因此我们先做了一个性能比较极致 ...