JSFiddle - React, Tailwind, and code Playground

by Florian Bar

JavaScript

const getAllSelectedOptions = values => {
  const allSelectedOptions = [];

  const addUniqueItem = value => {
    if (!allSelectedOptions.includes(value)) {
      allSelectedOptions.push(value);
    }
  };

  Object.keys(values).forEach(rowId => {
    if (Array.isArray(values[rowId])) {
      values[rowId].forEach(item => {
        addUniqueItem(item);
      });
    } else {
      addUniqueItem(values[rowId]);
    }
  });

  return allSelectedOptions;
};

console.log(getAllSelectedOptions({ "abc": "123", "def": "456", "ghi": "456" }));
console.log(getAllSelectedOptions({ "abc": ["123", "456", "9234"], "def": ["123", "456", "789"], }));
console.log(getAllSelectedOptions({"dsadsa": ["abc", "def"], "dsads": ["abc", "ghi"]}));