亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
for...of loops over iterable values
Key Differences Summary
When to Use Which?
One Common Pitfall
首頁 web前端 js教程 在JavaScript中為...和...的區(qū)別有什麼區(qū)別?

在JavaScript中為...和...的區(qū)別有什麼區(qū)別?

Aug 01, 2025 am 05:44 AM
循環(huán)

for...in 用於遍歷對象的可枚舉屬性,返回屬性名(鍵),適用於對象和數(shù)組,但對數(shù)組可能包含非數(shù)字索引和自定義屬性,不推薦用於數(shù)組值遍歷;2. for...of 用於遍歷可迭代對象(如數(shù)組、字符串、Map、Set等),通過Symbol.iterator 返回實際值,不適用於普通對象;3. 區(qū)別在於for...in 針對對象屬性鍵,for...of 針對可迭代值,應(yīng)根據(jù)數(shù)據(jù)類型和需求選擇,for...in 適合處理對象鍵,for...of 適合獲取數(shù)組或字符串等的值。

What is the difference between for...of and for...in in JavaScript?

The difference between for...of and for...in in JavaScript comes down to what they iterate over — they may look similar, but they serve different purposes.

What is the difference between for...of and for...in in JavaScript?

for...in loops over object properties

for...in is designed to loop through the enumerable properties of an object, including inherited ones (from the prototype chain, unless they're non-enumerable). It works best with plain objects.

 const person = {
  name: 'Alice',
  age: 30,
  city: 'New York'
};

for (let key in person) {
  console.log(key, person[key]);
}
// Output:
// name Alice
// age 30
// city New York

? Key point: for...in gives you the keys (property names) , not the values directly.

What is the difference between for...of and for...in in JavaScript?

It can also be used on arrays, but it returns index strings , not values:

 const arr = ['a', 'b', 'c'];
for (let index in arr) {
  console.log(index); // "0", "1", "2"
}

?? But this is generally not recommended for arrays because:

What is the difference between for...of and for...in in JavaScript?
  • It may include unexpected properties if someone added them to the array object.
  • It iterates over all enumerable properties , not just numeric indices.

for...of loops over iterable values

for...of works with iterable objects like arrays, strings, maps, sets, NodeLists, etc. It gives you the actual values , not the keys.

 const arr = ['a', 'b', 'c'];
for (let value of arr) {
  console.log(value);
}
// Output:
// a
// b
// c

It works on strings too:

 for (let char of 'hello') {
  console.log(char);
}
// h, e, l, l, o

? Use for...of when you want the values from an iterable (especially arrays).

Under the hood, for...of uses the object's Symbol.iterator method to get the sequence of values.


Key Differences Summary

Feature for...in for...of
Purpose Iterate over object properties Iterate over iterable values
Returns Keys (property names) Values
Works with objects ? Yes ? No (objects aren't iterable by default)
Works with arrays ? But returns indices (as strings) ? Yes (returns actual values)
Works with strings ? Yes (returns indices) ? Yes (returns characters)
Uses Symbol.iterator ? No ? Yes

When to Use Which?

  • Use for...in when:

    • You're looping over an object's own enumerable properties.
    • You need the keys (eg, to check or manipulate property names).
     for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        // safely process own properties
      }
    }
  • Use for...of when:

    • You're working with arrays, strings, maps, sets, etc.
    • You want clean access to the values without dealing with indices or keys.
     for (let value of myArray) {
      console.log(value); // clean and clear
    }

One Common Pitfall

 const arr = ['a', 'b', 'c'];
arr.customProp = 'extra';

for (let key in arr) {
  console.log(key); // "0", "1", "2", "customProp" ← unexpected!
}

for (let value of arr) {
  console.log(value); // "a", "b", "c" ← ignores non-iterable props
}

So for...of is safer for arrays.


Bottom line:

  • for...inobjects , get keys
  • for...ofiterables , get values

Choose based on your data type and what you need.

以上是在JavaScript中為...和...的區(qū)別有什麼區(qū)別?的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應(yīng)的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
Go語言中的循環(huán)和遞歸的比較研究 Go語言中的循環(huán)和遞歸的比較研究 Jun 01, 2023 am 09:23 AM

註:本文以Go語言的角度來比較研究循環(huán)和遞歸。在編寫程式時,經(jīng)常會遇到需要對一系列資料或操作進行重複處理的情況。為了實現(xiàn)這一點,我們需要使用循環(huán)或遞歸。循環(huán)和遞歸都是常用的處理方式,但在實際應(yīng)用中,它們各有優(yōu)缺點,因此在選擇使用哪種方法時需要考慮實際情況。本文將對Go語言中的循環(huán)和遞歸進行比較研究。一、循環(huán)循環(huán)是一種重複執(zhí)行某段程式碼的機制。 Go語言中主要有三

lambda表達式跳出循環(huán) lambda表達式跳出循環(huán) Feb 20, 2024 am 08:47 AM

lambda表達式跳出循環(huán),需要具體程式碼範例在程式設(shè)計中,循環(huán)結(jié)構(gòu)是常用的一種重要語法。然而,在特定的情況下,我們可能希望在循環(huán)體內(nèi)滿足某個條件時,跳出整個循環(huán),而不是僅僅終止當前的循環(huán)迭代。在這個時候,lambda表達式的特性可以幫助我們達成跳脫循環(huán)的目標。 lambda表達式是一種匿名函數(shù)的宣告方式,它可以在內(nèi)部定義簡單的函數(shù)邏輯。它與普通的函數(shù)聲明不同,

Java Iterator 與 Iterable:邁入編寫優(yōu)雅程式碼的行列 Java Iterator 與 Iterable:邁入編寫優(yōu)雅程式碼的行列 Feb 19, 2024 pm 02:54 PM

Iterator介面Iterator介面是一個用於遍歷集合的介面。它提供了幾個方法,包括hasNext()、next()和remove()。 hasNext()方法傳回布林值,指示集合中是否還有下一個元素。 next()方法傳回集合中的下一個元素,並將其從集合中刪除。 remove()方法從集合中刪除目前元素。以下程式碼範例示範如何使用Iterator介面來遍歷集合:Listnames=Arrays.asList("John","Mary","Bob");Iterator

PHP傳回數(shù)組所有值,組成一個數(shù)組 PHP傳回數(shù)組所有值,組成一個數(shù)組 Mar 21, 2024 am 09:06 AM

這篇文章將為大家詳細講解有關(guān)PHP返回數(shù)組所有值,組成一個數(shù)組,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。使用array_values()函數(shù)array_values()函數(shù)傳回陣列中所有值的陣列。它不會保留原始數(shù)組的鍵。 $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values將是["bar","qux"]使用迴圈可以使用循環(huán)手動獲取數(shù)組的所有值並將其新增至一個新

foreach和for迴圈的差別是什麼 foreach和for迴圈的差別是什麼 Jan 05, 2023 pm 04:26 PM

區(qū)別:1、for透過索引來循環(huán)遍歷每一個資料元素,而forEach透過JS底層程式來循環(huán)遍歷數(shù)組的資料元素;2、for可以透過break關(guān)鍵字來終止迴圈的執(zhí)行,而forEach不可以;3、 for可以透過控制迴圈變數(shù)的數(shù)值來控制迴圈的執(zhí)行,而forEach不行;4、for在迴圈外可以呼叫迴圈變量,而forEach在迴圈外不能呼叫迴圈變數(shù);5、for的執(zhí)行效率要高於forEach。

python中使用向量化替換循環(huán) python中使用向量化替換循環(huán) Apr 14, 2023 pm 07:07 PM

所有程式語言都離不開循環(huán)。因此,預設(shè)情況下,只要有重複操作,我們就會開始執(zhí)行循環(huán)。但是當我們處理大量迭代(數(shù)百萬/十億行)時,使用循環(huán)是一種犯罪。您可能會被困幾個小時,後來才意識到它行不通。這就是在python中實現(xiàn)向量化變得非常關(guān)鍵的地方。什麼是矢量化?向量化是一種在資料集上實現(xiàn)(NumPy)數(shù)組運算的技術(shù)。在後臺,它將操作一次應(yīng)用於數(shù)組或系列的所有元素(不同於一次操作一行的“for”循環(huán))。接下來我們使用一些用例來示範什麼是向量化。求數(shù)字總和##使用循環(huán)importtimestart

Java函數(shù)中遞歸呼叫有哪些替代方案? Java函數(shù)中遞歸呼叫有哪些替代方案? May 05, 2024 am 10:42 AM

用迭代取代Java函數(shù)中的遞迴呼叫在Java中,遞歸是一個強大的工具,用來解決各種問題。但是,在某些情況下,使用迭代可能是更好的選擇,因為它更有效且不易出現(xiàn)堆疊溢位。以下是迭代的優(yōu)點:效率更高,因為它不需要為每個遞歸呼叫建立新的堆疊幀。不容易發(fā)生堆疊溢出,因為堆疊空間使用受限。替代遞歸呼叫的迭代方法:Java中有幾種方法可以將遞歸函數(shù)轉(zhuǎn)換為迭代函數(shù)。 1.使用棧使用棧是將遞歸函數(shù)轉(zhuǎn)換為迭代函數(shù)最簡單的方法。堆疊是一種後入先出(LIFO)資料結(jié)構(gòu),類似函式呼叫堆疊。 publicintfa

如何處理PHP循環(huán)嵌套錯誤並產(chǎn)生相應(yīng)的報錯訊息 如何處理PHP循環(huán)嵌套錯誤並產(chǎn)生相應(yīng)的報錯訊息 Aug 07, 2023 pm 01:33 PM

如何處理PHP循環(huán)嵌套錯誤並產(chǎn)生對應(yīng)的報錯資訊在開發(fā)中,我們經(jīng)常會用到循環(huán)語句來處理重複的任務(wù),例如遍歷數(shù)組、處理資料庫查詢結(jié)果等。然而,在使用循環(huán)嵌套的過程中,有時會遇到錯誤,例如無限循環(huán)或嵌套層數(shù)過多,這種問題會導致伺服器效能下降甚至崩潰。為了更好地處理這類錯誤,並產(chǎn)生相應(yīng)的報錯訊息,本文將介紹一些常見的處理方式,並給出相應(yīng)的程式碼範例。一、使用計數(shù)器來

See all articles