그래서 다음과 같은 객체 배열이 있습니다.
var data = [
{
"title": "scooter",
"materials": ["wood", "metal", "glass"]
},
{
"title": "baseball bat",
"materials": ["wood", "metal"]
},
{
"title": "coffee table",
"materials": ["wood"]
}
];
함수에서 다음을 얻으려고 시도합니다. 재료 배열에서 2 개 이상의 재료가있는 것과 항목이 무엇인지.
나는 다음을 시도했다 :
function testing() {
let container = [];
let items = [];
for (let i = 0; i < data.length; i++) {
container.push(data[i].materials);
for (let j = 0; j < container.length; j++) {
if (container[i][j] >= 2) {
items.push(data[i].title);
}
}
}
return(items);
}
console.log(testing());
길이를 확인하고 전체 목록은 고사하고 제목을 반환하기 위해 중첩 배열을 가져올 수 없습니다.
기준에 따라보고 싶은 내용의 예 :
스쿠터 : "나무", "금속", "유리"야구 방망이 : "나무", 금속 "
코드에서 내가 뭘 잘못하고 있니?
하나의 루프를 통해 재료 길이를 테스트 할 수 있습니다!
let items = [];
for(let i=0 ; i<data.length; i++){
if(data[i].materials.length >= 2){
items.push(data[i].title);
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다