Как определить, какие элементы отсутствуют в неполном массиве, учитывая полный массив? - PullRequest
1 голос
/ 02 августа 2020

Пытаюсь написать функциональное решение этой проблемы. У меня уже есть решение, которое использует al oop и мутацию без рекурсии (я знаю, что то, что я попытался ниже, связано с мутацией, и я пытаюсь избежать этого).

Пожалуйста, смотрите ожидаемый результат в комментариях ниже . Я должен подчеркнуть, что элементы не обязательно уникальны, и сортировка не имеет значения.

const getNamesWhichFillHoles = (namesArrayWithHoles, namesArrayFilled, fillers = []) => {
  const holey = [...namesArrayWithHoles].filter(Boolean);
  const filled = [...namesArrayFilled].filter(Boolean);
  const fillerIndexInFilled = filled.findIndex(name => !holey.includes(name));
  if (fillerIndexInFilled === -1) return fillers;
  const filler = filled[fillerIndexInFilled];
  const fillerIndexInHoley = holey.findIndex(name => name == filler);
  fillers.push(filler);
  filled[fillerIndexInFilled] = null;
  holey[fillerIndexInHoley] = null;
  return getNamesWhichFillHoles(holey, filled, fillers);
}

const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue',];
const fillerNames = getNamesWhichFillHoles(namesArrayWithHoles, namesArrayFilled);

console.log(fillerNames); // [ 'Jim', 'Sam' ]
// should be: [ 'Jim', 'Sam', 'Bob' ]

Ответы [ 2 ]

1 голос
/ 02 августа 2020

Вы можете сделать следующее:

const namesArrayWithHoles = ['Bob', null, null, 'Sue', null];
const namesArrayFilled = ['Jim', 'Bob', 'Bob', 'Sam', 'Sue'];

const result = namesArrayFilled.reduce(
  (acc, elem) => {
    const index = acc[0].indexOf(elem);
    if (index != -1) {
      return [acc[0].slice(0, index).concat(acc[0].slice(index + 1)), acc[1]];
    }
    return [acc[0], acc[1].concat(elem)];
  },
  [namesArrayWithHoles, []]
)[1];

console.log(result);
0 голосов
/ 02 августа 2020

Я пришел к этому решению. Предполагается, что значений null нет, потому что это было просто отвлечение.

const getMissingValues = (partial, complete) => {
  const equals = a => b => b == a;
  const push = (arr, val, count) => count ? push([...arr, val], val, count - 1) : [...arr];
  return [...new Set(complete)].reduce((missing, val) => {
    const { length: a } = partial.filter(equals(val));
    const { length: b } = complete.filter(equals(val));
    const diff = (b - a);
    return push(missing, val, diff);
  }, []);
}

const partial = ['Bob', 'Sue'];
const complete = ['Jim', 'Bob', 'Bob', 'Bob', 'Sue', 'Sue', 'Sam'];
const missing = getMissingValues(partial, complete);

console.log(missing); // [ 'Jim', 'Bob', 'Bob', 'Sue', 'Sam' ]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...