How to remove a key from an object in Javascript?

Question

I have an object below:

1
2
3
4
5
var thisIsObject= {
'Cow' : 'Moo',
'Cat' : 'Meow',
'Dog' : 'Bark'
};

I want to remove the key Dog and its value. What should I do?

Answer

There are three ways to remove a key from an object.

1
2
3
4
5
6
7
8
9
// Example 1
var key = "Dog";
delete thisIsObject[key];

// Example 2
delete thisIsObject["Dog"];

// Example 3
delete thisIsObject.Dog;

Reference


This is the end of post

How to push an array into an array inside an object in Javascript?

Question

There is an object like below:

1
2
3
4
var myObj = {
arrayOne: [],
arrayTwo: []
};

And one array is var newArray = ['A', 'B'];

I want to add newArray into arrayOne: [], the result should be like below

1
2
3
4
{ 
arrayOne: [['A', 'B']],
arrayTwo: []
};

What should I do?

Answer

There are two ways to add newArray inside arrayOne of myObj.

  1. obj.arrayOne.push(newArray);
  2. obj['arrayOne'].push(newArray);

Reference


This is the end of post

如何理解 Javascript 中的 For 循环?

Javascript 里的 For 循环有很多种,接下来我们一一来解析一遍。

For Loop

这是最基础的用法,用 For loop 来遍历一个数组,如下所示:

1
2
3
for (var index = 0; index < myArray.length; index++) {
console.log(myArray[index]);
}

forEach - ES 5

forEach 是 ES 5 里的一个用法,如下所示:

1
2
3
myArray.forEach(function (value) {
console.log(value);
});

for…in - ES 5

for...in 循环实际是为循环可枚举 (enumerable) 对象而设计的,如下所示:

1
2
3
4
5
6
7
8
9
10
var obj = {a:1, b:2, c:3};

for (var prop in obj) {
console.log("obj." + prop + " = " + obj[prop]);
}

// 输出:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

for…of - ES 6

for ... of 是 ES 6 里的新用法,语法如下:

1
2
3
for (var value of myArray) {
console.log(value);
}

for...of 的语法看起来跟 for...in 很相似,但它的功能却丰富的多,它能循环很多东西。

循环一个数组 Array

1
2
3
4
5
6
7
8
let iterable = [10, 20, 30];

for (let value of iterable) {
console.log(value);
}
// 10
// 20
// 30

循环一个字符串 String

1
2
3
4
5
6
7
8
let iterable = "boo";

for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"

循环一个类型化的数组 TypedArray

1
2
3
4
5
6
7
let iterable = new Uint8Array([0x00, 0xff]);

for (let value of iterable) {
console.log(value);
}
// 0
// 255

循环一个 Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3

for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]

循环一个 Set

1
2
3
4
5
6
7
8
let iterable = new Set([1, 1, 2, 2, 3, 3]);

for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3

循环一个 DOM 集合

1
2
3
4
5
6
7
// Note: This will only work in platforms that have
// implemented NodeList.prototype[Symbol.iterator]
let articleParagraphs = document.querySelectorAll("article > p");

for (let paragraph of articleParagraphs) {
paragraph.classList.add("read");
}

循环一个生成器 Generators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function* fibonacci() { // a generator function
let [prev, curr] = [0, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}

for (let n of fibonacci()) {
console.log(n);
// truncate the sequence at 1000
if (n >= 1000) {
break;
}
}

Reference


This is the end of post

How to change background color upon hovering a div?

Question

There is a div as below.

1
2
<div class="e">Wei Xia</div>
<a href="http://weixia.info/">weixia.info</a>

How to change the background color when I hover those two element?

Answer

1
2
3
4
5
6
7
.e:hover {
background-color: #ff0000;
}

a:hover {
background-color: yellow;
}

By using :hover will update the background color. You can view the demo below.

Live Demo

Reference


This is the end of post

What is the order for tabindex in HTML?

Question

Like title says, what is the order for the tabindex attribute in html?

Answer

Code example:

1
2
3
4
5
6
7
<input tabindex="3">
<input tabindex="0">
<input tabindex="-1">
<input>
<input tabindex="2">
<input tabindex="1">
<span tabindex="4"> This wouldn't normally receive focus</span>

Order:


(3) Will receive focus third

(0) In normal source order

(-1) Will not receive focus

(n/a) In normal source order

(2) Will receive focus second

(1) Will receive focus first

<span tabindex="4">This wouldn't normally receive focus</span> has (4) largest value. Next tab will give focus to the rest of tabnavigable elements in code source order.

Reference


This is the end of post

How to add accessibility label in a tag in HTML?

Question

My code is below:

1
<li id="id_name" class="class_names"><a href="http://linktothepage.com/page" aria-label="TITLE OF MY PAGE">TITLE OF MY PAGE</a></li>

I added aria-label in the <a> tag, but it doesn’t work with screen reader software.

What should I do?

Answer

aria-label sometimes doesn’t work with JAWS in Windows PC. Need to use title in <a> tag instead.

The solution is:

1
<li id="id_name" class="class_names"><a href="http://linktothepage.com/page" title="TITLE OF MY PAGE">TITLE OF MY PAGE</a></li>

Reference


This is the end of post

How to click on the element and move to the first in javascript?

Question

There is a list as below.

1
2
3
4
5
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>

If I want to move A or B or C to the first place after clicking on it, what should I do?

Answer

By using JQuery method prepend to make it happen.

Solution:

1
2
3
4
5
6
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>

1
2
3
$('ul li').click(function(event) {
$('ul').prepend($(this))
});

Reference


This is the end of post

How to get data attribute value from the element in Javascript?

Question

There is a html as below.

1
2
3
<a id="option1" data-id="10" data-option="21" href="#">
Click to do something
</a>

I want to get the data-id and data-option values while clicking this element.

How to make it happen?

Answer

Solution:

1
2
3
4
5
$('#option1').on('click', function () {
var $el = $(this);

console.log($el.data('id'), $el.data('option'));
});

The console log will be 10 and 21.

Reference


This is the end of post