[英]how to return result of more than one index inside javascript multidimensional array?
這是開始的代碼;
data = { "questions": ["Who", "Where", "How"], "names": ["Bill", "Fred", "Lindsey"], "cities": ["Baltimore", "New York", "Chicago"], "values": [ [ [50, 20, 40], [40, 90, 10], [50, 75, 30] ], [ [33, 57, 100], [20, 70, 89], [16, 40, 68] ], [ [3, 26, 54], [62, 69, 86], [23, 81, 98] ] ] } function sortObject() { var values; var question = data.questions.indexOf("Who", "Where") var name = data.names.indexOf("Bill"); var city = data.cities.indexOf("Baltimore"); values = data.values[question][name][city] console.log(values) } sortObject()
我希望能夠同時返回“誰”和“哪里”的結果,但不包括“如何”。
因此最終結果將是[50,33]。
我還希望該方法能夠處理無限量的項目,例如,“問題”數組中可以有100個項目,並且我可以分別選擇我想顯示的項目在數組中的位置。
我認為我將不得不遍歷每個項目,然后也許要做一些事情;
for (var question = 0; question < data.questions.length; question++) {
if (data.questions.indexOf() == "Who" || "Where") {
var name = data.names.indexOf("Bill");
var city = data.cities.indexOf("Baltimore");
values = data.values[question][name][city]
console.log(values)
}
}
但這是行不通的,所以我不確定從這里去哪里?
希望一切都清楚了,如果您需要更多信息,請告訴我;
預先感謝您的任何幫助/建議!
如果要搜索的項目不止一個,則可以進行迭代。
function getData(question, name, city) { var result = []; (Array.isArray(question) ? question : [question]).forEach(function (q) { var r = data.values[data.questions.indexOf(q)] || []; (Array.isArray(name) ? name : [name]).forEach(function (n) { var rr = r[data.names.indexOf(n)] || []; (Array.isArray(city) ? city : [city]).forEach(function (c) { result.push({ question: [q, n, c], value: rr[data.cities.indexOf(c)] }); }); }); }); return result; } var data = { "questions": ["Who", "Where", "How"], "names": ["Bill", "Fred", "Lindsey"], "cities": ["Baltimore", "New York", "Chicago"], "values": [[[50, 20, 40], [40, 90, 10], [50, 75, 30]], [[33, 57, 100], [20, 70, 89], [16, 40, 68]], [[3, 26, 54], [62, 69, 86], [23, 81, 98]]] }, result = getData(["Who", "Where"], "Bill", "Baltimore"); console.log(result);
另一個更具動態性的解決方案可能是使用搜索對象的迭代遞歸方法。
function getData(search) { var result = [], order = ['questions', 'names', 'cities']; function iter(value, level) { if (level === order.length) { return result.push(value); } search[order[level]].forEach(function (a) { iter((value || [])[data[order[level]].indexOf(a)], level + 1); }); } iter(data.values, 0); return result; } var data = { "questions": ["Who", "Where", "How"], "names": ["Bill", "Fred", "Lindsey"], "cities": ["Baltimore", "New York", "Chicago"], "values": [[[50, 20, 40], [40, 90, 10], [50, 75, 30]], [[33, 57, 100], [20, 70, 89], [16, 40, 68]], [[3, 26, 54], [62, 69, 86], [23, 81, 98]]] }, result = getData({ questions: ["Who", "Where"], names: ["Bill"], cities: ["Baltimore"] }); console.log(result);
嘗試像這樣更改代碼:
function sortObject() {
var values = [];
var whoIndex = data.questions.indexOf("Who");
var whereIndex = data.questions.indexOf("Where");
var name = data.names.indexOf("Bill");
var city = data.cities.indexOf("Baltimore");
values.push(data.values[whoIndex][name][city]);
values.push(data.values[whereIndex][name][city]);
console.log(values); //[50, 33]
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.