正常情况下我们只需要去循环数组就好了,但是今天写项目的时候碰到了一个循环对象的情况这时候我们就不能在通过正常的for循环和forEach来循环了。
obArr: { 'a': {item : '周'}, 'b': {item : '六'}, 'c': {item : '出'}, 'd': {item : '去'}, 'e': {item : '玩'}, 'f': {item : '喽'} }, str: '', keyStr: ''
下面两种方法可以循环对象
方法一:使用原生的js方法Object
console.log(Object.values(this.obArr),'value') console.log(Object.keys(this.obArr),'key')
打印出来就是
方法二:使用for in循环
//循环对象数组 for (let i in this.obArr) { console.log(this.obArr[i].item) this.str += this.obArr[i].item this.keyStr += i } console.log(this.str) console.log(this.keyStr)
打印出来的结果