简体   繁体   中英

how to return result of more than one index inside javascript multidimensional array?

Here is the code to begin with;

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

I would like to be able to return the results for both "Who" & "Where" whilst excluding "How".

So the final result would be [50, 33].

I would also like the method to be able to work with an infinite amount of items, so for instance there could be 100 items in the "questions" array and I would be able to individually pick whichever ones I would like to show regardless of their position inside the array.

I think that I will have to loop through each item and then perhaps do something along the lines of;

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

But this is not working, so I'm not sure where to go from here?

Hope everything is clear, please let me know if you need any more information;

Thanks in advance for any help/advice!

You could iterate over if there are more than one items to search for.

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

Another more dynamic solution could be an iterative recursive approach with a search object.

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

Try to change your code like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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