惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
C
Cybersecurity and Infrastructure Security Agency CISA
S
Security Affairs
Latest news
Latest news
Security Latest
Security Latest
N
News and Events Feed by Topic
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
The Exploit Database - CXSecurity.com
The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
博客园_首页
S
Secure Thoughts
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AWS News Blog
AWS News Blog
腾讯CDC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
N
News and Events Feed by Topic
A
Arctic Wolf
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
Project Zero
Project Zero
A
About on SuperTechFans
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Know Your Adversary
Know Your Adversary
S
Security @ Cisco Blogs
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
L
LINUX DO - 最新话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript 5 Inconvenient Truths about TypeScript Refactoring in Rust: Introducing Traits Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates JSX is syntactic sugar TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone
JavaScript 101: Arrays
2015-09-04 · via oida.dev | TypeScript, Rust

This was the first contribution I’ve ever made at GitHub, belonging to the original learn.jquery.com website. The original article is now offline, but saved here for the future.

Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays.

To create an array you can either use the object constructor or the literal declaration, by assigning your variable a list of values right after the declaration.

// A simple array
var myArray1 = new Array( 'hello', 'world' ); // with constructor
var myArray2 = [ 'hello', 'world' ]; // literal declaration, the preferred way

The literal declaration is preferred, see the Google Coding Guidelines for more information.

If you don’t know your values yet, it is also possible to declare an empty Array, and add elements either through functions or through accessing by index:

// Creating empty arrays and adding values
var myArray = [];

myArray.push('hello'); // adds 'hello' on index 0
myArray.push('world'); // adds 'world' on index 1
myArray[2] = '!'; // adds '!' on index 2

‘push’ is a function which adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with ‘undefined’;

// Leaving indices
var myArray = [];

myArray[0] = 'hello';
myArray[1] = 'world';
myArray[3] = '!';

console.log(myArray); // logs ['hello', 'world', undefined, '!'];

So ‘push’ is far more safe, especially if you don’t know the size of your array yet. With the index you not only assign values to array items, but also access those.

// Accessing array items by index
var myArray = [ 'hello', 'world', '!'];
console.log(myArray[2]); // logs '!'

Array methods and properties #

length #

The ‘length’ property is used to know the amount of items in your array.

// Length of an array
var myArray = [ 'hello', 'world', '!'];
console.log(myArray.length); // logs 3

You will need the length property for looping through an array:

// For loops and arrays - a classic
var myArray = ['hello', 'world', '!'];
for(var i = 0; i < myArray.length; i = i + 1) {
console.log(myArray[i]);
}

Except when you are using for … in loops:

// or loops and arrays - alternate method
var myArray = ['hello', 'world', '!'];
for(var i in myArray) {
console.log(myArray[i]);
}

concat #

With ‘concat’, you can concatenate two or more arrays

// Concatenating Arrays
var myArray = [2, 3, 4];
var myOtherArray = [5, 6, 7];
var wholeArray = myArray.concat(myOtherArray); // [2, 3, 4, 5, 6, 7]

join #

‘join’ creates a string representation of your array. It’s parameter is as string which works as a seperator between elements (default is a comma);

// Joining elements
var myArray = ['hello', 'world', '!'];
console.log(myArray.join(' ')); // logs "hello world !";
console.log(myArray.join()); // logs "hello,world,!"
console.log(myArray.join('')); // logs "helloworld!"
console.log(myArray.join('!!')) // logs "hello!!world!!!!!";

pop #

‘pop’ removes the last element of an array. It is the opposite method to ‘push’

// pushing and popping
var myArray = [];
myArray.push(0); // [ 0 ]
myArray.push(2); // [ 0 , 2 ]
myArray.push(7); // [ 0 , 2 , 7 ]
myArray.pop(); // [ 0 , 2 ]

reverse #

As the name suggests, the elements of the array are in reverse order after calling this method

// reverse
var myArray = [ 'world' , 'hello' ];
myArray.reverse(); // [ 'hello', 'world' ]

shift #

Removes the first element of an array. With ‘pop’ and ‘shift’ you can recreate the method of a queue

// queue with shift() and pop()
var myArray = [];
myArray.push(0); // [ 0 ]
myArray.push(2); // [ 0 , 2 ]
myArray.push(7); // [ 0 , 2 , 7 ]
myArray.shift(); // [ 2 , 7 ]

slice #

Extracts a part of the array and returns them in a new one. This method takes one parameter, which is the starting index.

// slicing
var myArray = [1, 2, 3, 4, 5, 6, 7, 8];
var newArray = myArray.slice(3);

console.log(myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
console.log(newArray); // [4, 5, 6, 7, 8]

splice #

Removes a certain amount of elements and adds new ones at the given index. It takes at least 3 parameters

// splice method
myArray.splice(idx, len, values, ...);
  • idx = the starting index
  • len = the number of elements to remove
  • values = the values which should be inserted at idx

For example:

// splice example
var myArray = [0, 7, 8, 5];
myArray.splice(1, 2, 1, 2, 3, 4);
console.log(myArray); // [0, 1, 2, 3, 4, 5]

sort #

Sorts an array. It takes one parameter, which is a comparing function. If this function is not given, the array is sorted ascending

// sorting without comparing function
var myArray = [3, 4, 6, 1];
myArray.sort(); // 1, 3, 4, 6
// sorting with comparing function
function descending(a, b) {
return b - a;
}
var myArray = [3, 4, 6, 1];
myArray.sort(descending); // [6, 4, 3, 1]

The return value of descending (for this example) is important. If the return value is less than zero, the index of a is before b, and if it is greater than zero it’s vice-versa. If the return value is zero, the elements index is equal.

unshift #

Inserts an element at the first position of the array

// unshift
var myArray = [];
myArray.unshift(0); // [ 0 ]
myArray.unshift(2); // [ 2 , 0 ]
myArray.unshift(7); // [ 7 , 2 , 0 ]

forEach #

In modern browsers, like Chrome, Firefox and Internet Explorer 9 it is possible to traverse through arrays by a so called ‘forEach’ method, where you pass a function which is called for each element in your array.

The function takes up to three arguments:

  • element - The element itself
  • index - The index of this element in the array
  • array - The array itself

All of the are optional, but you will need at least the ‘element’ parameter in most cases.

// native forEach
function printElement(elem) {
console.log(elem);
}

function printElementAndIndex(elem, index) {
console.log("Index " + index + ": " + elem);
}

function negateElement(elem, index, array) {
array[index] = -elem;
}

myArray = [1, 2, 3, 4, 5];
myArray.forEach(printElement); //prints all elements to the console
myArray.forEach(printElementAndIndex); //prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
myArray.forEach(negateElement); // myArray is now [-1, -2, -3, -4, -5]

Related Articles