programing

vue.js 2에서 개체 관찰자를 루핑하려면 어떻게 해야 하는가?

prostudy 2022. 3. 26. 16:44
반응형

vue.js 2에서 개체 관찰자를 루핑하려면 어떻게 해야 하는가?

만약 내가console.log(this.list), 이와 같은 결과:

여기에 이미지 설명을 입력하십시오.

this.list.forEach(function (user) {
    selected.push(user.id);
});

오류 있음:

Uncaused TypeError: this.list.forExecute는 함수가 아님

어떻게 하면 이 오류를 해결할 수 있을까?

이다this.list배열 말고?

만약this.list배열과 같다(필요한 것은length그 물체에 대한 속성), 다음을 수행할 수 있어야 한다.

Array.prototype.forEach.call(this.list, user => {
  // ...
})

또는

Array.from(this.list).forEach(user => {
  // ...
})

또는

[...this.list].forEach(user => {
  // ...
})

그렇지 않으면this.list그저 평범한 물건일 뿐, 다음과 같은 일을 할 수 있다.

Object.keys(this.list).forEach(key => {
  const user = this.list[key]
  // ...
})

또는

Object.entries(this.list).forEach(([key, user]) => {
  // ...
})

Vue에서 관찰자 배열을 반복하는 방법은 다음과 같다.

let keys = Object.keys(myObserverArray);

keys.forEach(key => {
   let item = myObserverArray[key];
   //...work with item
})

참조URL: https://stackoverflow.com/questions/47388040/how-can-i-loop-object-observer-on-vue-js-2

반응형