0

I have an array of objects with nested arrays inside that I want to compare and map through to only display the value that is not equal to one another.

Here is what I tried

const arrayObjOne = [
  { display: ["Stockholm", "Copenhagen", "Berlin"] },
  { display: ["Stockholm", "Copenhagen", "Berlin", "Oslo"] },
];

function Testpage() {
  const results = arrayObjOne.filter(
    ({ display: data1 }) =>
      !arrayObjOne.some(({ display: data2 }) => data1 === data2)
  );
  console.log(results);
  return (
    <div>
      {results.map((data, i) => (
        <ul>
          <li>{data}</li>
        </ul>
      ))}
    </div>
  );
}

export default Testpage;

The final result I need is to display the difference between these nested arrays – the final result should look something like this like this:

as a list, in this case {['Oslo']}

Albah
  • 1
  • 2
  • You should make a JavaScript code pen and share the link and I should be able to show you a solution – Wayne Mar 26 '23 at 17:42
  • Try this: `arrayObjOne.map(a => a.display).flat().filter((item, index, currentArray) => currentArray.lastIndexOf(item) === index && currentArray.indexOf(item) === index) ` – Priyank Kachhela Mar 26 '23 at 17:47
  • Does this answer your question? [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – RubenSmn Mar 26 '23 at 18:15

0 Answers0