Error: 'xxx' is not iterable is not iterable

1. Error message "xxx is not iterable"

今天开发过程中,遇到了下面这个错误报警。

insert image description here
The reason is that because ESLint was turned on today , the for in I used was marked red, and then I didn't think much about it. I changed it to for of , and the warning message was indeed gone, but this error occurred during the test.

what is the reason? In JavaScript, Objects are not iterable unless they implement the iterable protocol. Therefore, you cannot use for of to iterate over the properties of an object.

2. Solution Object.keys( )

可以使用**Object.keys** 来迭代对象的属性或属性值。
	let obj = {
    
    a:1,b:2,c:3};
	for(let key of obj){
    
    
		console.log(key,obj[key]);
	}
	
	// Object.keys + for of
	for(let key of Object.keys(obj)){
    
    
		console.log(key,obj[key]);
	}

	// Object.keys + forEach
	Object.keys(obj).forEach( key =>{
    
    
		console.log(key,obj[key])
	})

3. Example

insert image description here

Guess you like

Origin blog.csdn.net/qq_53931766/article/details/123802103