如何在javascript多维数组中返回多个索引的结果?

时间:2016-07-01 09:09:47

标签: javascript arrays multidimensional-array

这是开始的代码;

Plunk

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()

我希望能够将“Who”和& “哪里”,但不包括“如何”。

所以最终结果将是[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)
    }
  }

但这不起作用,所以我不确定从哪里开始?

希望一切都清楚,如果您需要更多信息,请告诉我们;

提前感谢您的任何帮助/建议!

2 个答案:

答案 0 :(得分:2)

如果要搜索多个项目,您可以进行迭代。

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);

答案 1 :(得分:0)

尝试更改您的代码:

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]
}