繁体   English   中英

如何返回JavaScript多维数组内多个索引的结果?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM