2017-02-20 2 views
0

for 루프에서 개체를 변경하려고합니다.개체가 변경되지 않음

console.log(dish)ingredients 속성이 의 배열을 포함하고 dish 개체를 기록 할 것으로 예상됩니다.

dish.ingredients에 기록하면 성분을 기록합니다.

dish을 기록하면 성분없이 dish 개체가 기록됩니다.

왜 이런가요?

for (let dish of dishArray) { 
    dish['ingredients'] = [] 
    for (let ingredient of ingredientsArray) { 
    if (dish._id.equals(ingredient._dishID)) { 
     dish['ingredients'].unshift(ingredient) 
    } 
    } 
    console.log(dish['ingredients'])    <------------- 
    console.log(dish)       <-------------   
} 

dishArraydish는 객체의 어레이가 몽구스 질의로부터 리턴된다. 코드의 자세한 지식없이

+0

'dishArray'이, 당신의 예상 출력 무엇인가의 값이 무엇을 할 것인가? 당신이 console.log를'dish [ 'ingredients']'에서하고있는 것처럼 보이지만,'dish'를'newDishArray'로 밀어 넣는 것은 의도 된 것입니까? –

+0

@DanielT. 어떤 아이디어? – softcode

+0

나는 두 가지 질문을했다. 내가 대답하지 않으면 나는 도울 수 없다. –

답변

0

, 일반 자바 스크립트 ==은 작업

var dishArray = [{ 
 
    _id: '0' 
 
}, { 
 
    _id: '1' 
 
}]; 
 
var ingredientsArray = [{ 
 
    _id: '0', 
 
    _dishID: '0' 
 
}, { 
 
    _id: '1', 
 
    _dishID: '1' 
 
}]; 
 

 
for (let dish of dishArray) { 
 
    dish['ingredients'] = []; 
 

 
    for (let ingredient of ingredientsArray) { 
 
    if (dish._id == ingredient._dishID) { 
 
     dish['ingredients'].unshift(ingredient); 
 
    } 
 
    } 
 

 
    console.log(dish['ingredients']); 
 
    console.log(dish); 
 
}

+0

돌연변이와 친화적으로 재생되지 않는 반환 된 몽구스 개체가 나타납니다 – softcode

+0

다음 제공되는 답변을 따라 몽구스 개체를 기울이거나() 또는 toObject() 할 수 있습니다. http://stackoverflow.com/questions/7503450/how-do-you-turn-a-mongoose-document-to-a-plain-object – rdlopes

+0

그래,'lean()'이 해결했다. 곧 쓸거야. – softcode