2016-11-24 46 views
0

如何獲得值爲「類別2中的第二個問題?」的問題的答案根據關鍵值和變化的大小從陣列中獲取價值

我需要用JavaScript來做到這一點。類別名稱將是隨機的,儘管我知道這個值以及我正在尋找答案的問題的值。

類別和問題的數量會有所不同,因此解決方案不得將固定大小依賴於此數組。

不會有任何重複的類別或問題。

任何幫助將是真棒我一直在努力與此。什麼讓我是類別名稱正在改變,我不知道如何匹配一個值。

JS

{ 
    "Category 1":[ 
     { 
      "question":"Fist question in category 1?", 
      "answers":[ 
       "first answer", 
       "second answer", 
       "third answer", 
       "fourth answer" 
      ] 
     }, 
     { 
      "question":"Second question in category 1?", 
      "answers":[ 
       "first answer", 
       "second answer", 
       "third answer", 
       "fourth answer" 
      ] 
     } 
    ], 
    "Category 2":[ 
     { 
      "question":"First question in category 2?", 
      "answers":[ 
       "first answer", 
       "second answer", 
       "third answer", 
       "fourth answer" 
      ] 
     }, 
     { 
      "question":"Second question in category 2?", 
      "answers":[ 
       "first answer", 
       "second answer", 
       "third answer", 
       "fourth answer" 
      ] 
     }, 
     { 
      "question":"Third question in category 2?", 
      "answers":[ 
       "first answer", 
       "second answer", 
       "third answer", 
       "fourth answer" 
      ] 
     } 
    ] 
}; 
+0

你沒事使用圖書館辦呢? – Khang

+1

只是一些簡單的循環應該做的伎倆......我不明白圖書館將如何幫助 – qxz

+0

@khang - 爲什麼他需要一個圖書館,當他需要的功能是建立在本地和相對容易實現? for循環功能強大。 – Derek

回答

3

你可以試試for...in循環。

var obj = {/* Assume that this is your object */}; 
for (let key in obj){ //Loops through object 
    for (let i = 0; i < obj[key].length; i++){ //Loops through array 
     if (obj[key][i].question == "Second question in category 2?"){ 
      var answers = obj[key][i].answers; //Here's your answers in array 
     } 
    } 
} 
+0

謝謝你,這工作得很好。我不得不改變讓var,否則它的工作就像一個魅力! –

1

對每個類別做一個for循環,然後再循環。

for (var i in categories) { 
    var category = categories[i]; 
    for (var k in category) { 
     if (category[k].question == 'First question in category 2?') { 
      console.log(category[k]); 
     } 
    } 
} 
+2

不要使用for-in來遍歷數組。 – Barmar

0

 var questions = { 
 
      "Category 1": [ 
 
      { 
 
       "question": "Fist question in category 1?", 
 
       "answers": ["first answer", 
 
       "second answer", 
 
       "third answer", 
 
       "fourth answer"] 
 
      }, 
 
       { 
 
        "question": "Second question in category 1?", 
 
        "answers": ["first answer", 
 
        "second answer", 
 
        "third answer", 
 
        "fourth answer"] 
 
       }], 
 
      "Category 2": [ 
 
       { 
 
        "question": "First question in category 2?", 
 
        "answers": ["first answer", 
 
        "second answer", 
 
        "third answer", 
 
        "fourth answer"] 
 
       }, 
 
       { 
 
        "question": "Second question in category 2?", 
 
        "answers": ["first answer", 
 
        "second answer", 
 
        "third answer", 
 
        "fourth answer"] 
 
       }, 
 
       { 
 
        "question": "Third question in category 2?", 
 
        "answers": ["first answer", 
 
        "second answer", 
 
        "third answer", 
 
        "fourth answer"] 
 
       }] 
 
     }; 
 
     for(var category in questions) 
 
     { 
 
      var filtered = $.grep(questions[category], function (e, i) { 
 
       return e.question == "Second question in category 2?"; 
 
      }); 
 
      if(filtered.length > 0) 
 
      { 
 
       console.log(filtered[0].answers); 
 
      } 
 
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

OP沒有提到jquery,原生javascript會輕鬆處理 – Derek

0

試試這個代碼:

const obj = { 
 
    "Category 1": [{ 
 
     "question": "Fist question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }], 
 
    "Category 2": [{ 
 
     "question": "First question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 2?", 
 
     "answers": [ "first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer", 
 
     ] 
 
    }, { 
 
     "question": "Third question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }] 
 
}; 
 

 
const values = Object.values(obj); 
 

 
const result = values.reduce((acc, cur) => { 
 
    Array.prototype.push.apply(acc, cur); 
 
    return acc; 
 
}, []).filter(obj => obj.question === 'Third question in category 2?'); 
 

 
console.log(result[0]);

0

如何使用的forEach每個ARR這個簡單的解決方案唉。希望能幫助到你!

var obj = { 
 
    "Category 1": [{ 
 
     "question": "Fist question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }], 
 

 
    "Category 2": [{ 
 
     "question": "First question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer"  ] 
 
    }, { 
 
     "question": "Third question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }] 
 
}; 
 

 
for(var i in obj){ 
 
    obj[i].forEach(function(x){ 
 
     if(x.question === "Second question in category 2?"){ 
 
     console.log(x.answers); 
 
     } 
 
    }); 
 
}

0

你可以嘗試這樣的事情使用循環,

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta charset="ISO-8859-1"> 
 
<title>Insert title here</title> 
 
</head> 
 
<body> 
 
<div id="getAnswers"></div> 
 
<script type="text/javascript"> 
 
var qas={ 
 
    "Category 1": [{ 
 
     "question": "Fist question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 1?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }], 
 
    "Category 2": [{ 
 
     "question": "First question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Second question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }, { 
 
     "question": "Third question in category 2?", 
 
     "answers": ["first answer", 
 
      "second answer", 
 
      "third answer", 
 
      "fourth answer" 
 
     ] 
 
    }] 
 
}; 
 

 
for(obj in qas){ 
 
\t var qasobj=qas[obj]; 
 
\t for(categ in qasobj){ 
 
\t \t console.log("getting second"+qasobj[categ].question); 
 
\t \t if(qasobj[categ].question=="Second question in category 2?"){ 
 
\t \t \t console.log("You have got your answers "+qasobj[categ].answers); 
 
\t \t \t document.getElementById("getAnswers").innerHTML=qasobj[categ].answers; 
 
\t \t } 
 
\t } 
 
} 
 
</script> 
 
</body> 
 
</html>

0

你也可以使用Array.prototype.forEach

var questions = { 
 
    "Category 1": [{ 
 
    "question": "Fist question in category 1?", 
 
    "answers": ["first answer", 
 
     "second answer", 
 
     "third answer", 
 
     "fourth answer" 
 
    ] 
 
    }, { 
 
    "question": "Second question in category 1?", 
 
    "answers": ["first answer", 
 
     "second answer", 
 
     "third answer", 
 
     "fourth answer" 
 
    ] 
 
    }], 
 
    "Category 2": [{ 
 
    "question": "First question in category 2?", 
 
    "answers": ["first answer", 
 
     "second answer", 
 
     "third answer", 
 
     "fourth answer" 
 
    ] 
 
    }, { 
 
    "question": "Second question in category 2?", 
 
    "answers": ["first answer", 
 
     "second answer", 
 
     "third answer", 
 
     "fourth answer" 
 
    ] 
 
    }, { 
 
    "question": "Third question in category 2?", 
 
    "answers": ["first answer - 1", 
 
     "second answer", 
 
     "third answer", 
 
     "fourth answer" 
 
    ] 
 
    }] 
 
}; 
 

 

 
function getAnswer(inputQuestion) { 
 
    var output = {}; 
 

 
    Object.keys(questions).forEach((category) => { 
 
    questions[category].forEach((val) => { 
 
     if (val.question === inputQuestion) { 
 
     output = val.answers; 
 
     } 
 
    }); 
 
    }); 
 
    return output; 
 
} 
 

 

 
var answer = getAnswer("Third question in category 2?"); 
 
console.log(answer);