0

I would like to select from an array with multiple keys at the same time. Here is my dictionary:

var votes = {
"abreudia":{"ACDA":["alex"],"ANG":["lou"],"APL":["lea"],"ART":["cecile"],"ASR":["def"],"EC":["abc"],"EGOD":...,
"lou":{"ACDA":["abc"],"ANG":["def"],"APL":["hig"],"ART":["azerty"],"ASR":["def"],"EC":["abc"],"EGOD":...,

Here is my code so far:

var numChecked = 0;
var request = [];

  function checkSelected(selected) {
    if (selected.checked == true) {
      request.push(selected.value);
      numChecked += 1;
    } else if (selected.checked == false) {
      var index = request.indexOf(selected.value);
      if (index !== -1) request.splice(index, 1);
      numChecked -= 1;
    }
    console.log(
      "selected : ",
      selected.value,
      "request : ",
      request,
      "numChecked : ",
      numChecked
    );

    for (var key in votes) {
      if (votes.hasOwnProperty(key)) {
        console.log(key, votes[key][request]);
      }
    }

For example when I select "ASR" everything is fine and the output is the dictionary with only the "ASR". Like so:

"abreudia":"alex","lou":"abc",...
request :  ["ASR"]

If I do more than one selection, request looks like:

request :  ["ASR, ANG"]

In the end I would like an output like this with this request for example:

"abreudia":{"def", "lou"},"lou":{"def","def"},...

1 Answers1

2

Sounds like you are looking for something like this:

var v = {
  "abreudia":{"ACDA":["alex"],"ANG":["lou"],"APL":["lea"],"ART":["cecile"],"ASR":["def"] },
  "lou":{"ACDA":["abc"],"ANG":["def"],"APL":["hig"],"ART":["azerty"],"ASR":["def"],"EC":["abc"] }
};

function processRequest(votes, request) {
  var entries = Object.entries(votes)
    .map(function (entry) {
      return [
        entry[0],
        request.map(function (e) { return entry[1][e]; }).flat(),
      ];
    });
    
  return Object.fromEntries(entries);
}

console.log(processRequest(v, ['APL']));

console.log(processRequest(v, ['ASR', 'ANG']));

Or using ES6 syntax:

var v = {
  "abreudia":{"ACDA":["alex"],"ANG":["lou"],"APL":["lea"],"ART":["cecile"],"ASR":["def"] },
  "lou":{"ACDA":["abc"],"ANG":["def"],"APL":["hig"],"ART":["azerty"],"ASR":["def"],"EC":["abc"] }
};

const processRequest = (votes, request) =>
  Object.fromEntries(
    Object.entries(votes)
      .map(([key, value]) => [key, request.map(e => value[e]).flat()])
  );
  
console.log(processRequest(v, ['APL']));

console.log(processRequest(v, ['ASR', 'ANG']));

Lastly, using ES6 and the objectMap() helper function to do the whole process in one line:

var v = {
  "abreudia":{"ACDA":["alex"],"ANG":["lou"],"APL":["lea"],"ART":["cecile"],"ASR":["def"] },
  "lou":{"ACDA":["abc"],"ANG":["def"],"APL":["hig"],"ART":["azerty"],"ASR":["def"],"EC":["abc"] }
};

// general-purpose helper function
const objectMap = (obj, fn) =>
  Object.fromEntries(
    Object.entries(obj).map(
      ([k, v], i) => [k, fn(v, k, i)]
    )
  );

const processRequest = (votes, request) =>
  objectMap(votes, items => request.map(e => items[e]).flat());
  
console.log(processRequest(v, ['APL']));

console.log(processRequest(v, ['ASR', 'ANG']));
JLRishe
  • 90,548
  • 14
  • 117
  • 150