4

How do I filter an array of arrays (myNumbers) against an array (result) to get only the values that appear in result per each array in myNumbers?

Specifically, given:

var result = [02, 03, 04, 06, 07, 11, 12, 13];

var myNumbers = [
    [01, 03, 04, 05, 09, 10, 12, 14],
    [01, 03, 04, 05, 06, 08, 10, 12],
    [01, 02, 04, 05, 06, 08, 10, 12],
    [01, 03, 04, 05, 06, 09, 12, 13],
    [01, 02, 03, 05, 06, 08, 10, 11]
];

Output should be:

[
    [03, 04, 12],
    [03, 04, 06, 12],
    [02, 04, 06, 12],
    [03, 04, 06, 12, 13],
    [02, 03, 06, 11],
]

I'm only able to filter one array at a time:

// This only filters it for myNumbers[0]
var confereResult = result.filter(function (number) {
    if (myNumbers[0].indexOf(number) == -1)
            return number;
    console.log(number);
});

How do I go through the whole list instead?

3
  • 2
    please add the wanted result as well. Commented Feb 14, 2019 at 15:59
  • 02 in JavaScript results in 2. What is the purpose of writing 02? Commented Feb 14, 2019 at 15:59
  • Also don't think return number is what you want here. With this code 0 could never be in the result. You probably want return true (or something similar). Commented Feb 14, 2019 at 16:01

3 Answers 3

5

You could map the result of the filtering.

var filter = [02, 03, 04, 06, 07, 11, 12, 13],
    array = [[01, 03, 04, 05, 09, 10, 12, 14], [01, 03, 04, 05, 06, 08, 10, 12], [01, 02, 04, 05, 06, 08, 10, 12], [01, 03, 04, 05, 06, 09, 12, 13], [01, 02, 03, 05, 06, 08, 10, 11]],
    result = array.map(a => filter.filter(f => a.includes(f)));

console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

4

Iterate throught the myNumbers array and apply a filter to each row.

var result = [02, 03, 04, 06, 07, 11, 12, 13];

var myNumbers = [
    [01, 03, 04, 05, 09, 10, 12, 14],
    [01, 03, 04, 05, 06, 08, 10, 12],
    [01, 02, 04, 05, 06, 08, 10, 12],
    [01, 03, 04, 05, 06, 09, 12, 13],
    [01, 02, 03, 05, 06, 08, 10, 11]
];
myNumbers.forEach((arr, i) => {
  myNumbers[i] = arr.filter((val) => {
    return result.indexOf(val) != -1;
  });
});
console.log(myNumbers);

Comments

0

You can loop on myNumbers and push result into an array, like

var result = [02, 03, 04, 06, 07, 11, 12, 13];

var myNumbers = [
    [01, 03, 04, 05, 09, 10, 12, 14],
    [01, 03, 04, 05, 06, 08, 10, 12],
    [01, 02, 04, 05, 06, 08, 10, 12],
    [01, 03, 04, 05, 06, 09, 12, 13],
    [01, 02, 03, 05, 06, 08, 10, 11]
];
var confereResult = []
myNumbers.forEach(function (val) {
  confereResult.push(result.filter(function (number) {
      if (val.indexOf(number) == -1)
              return number;
  }));
})

console.log(confereResult)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.