Filtering an array of objects that contain arraysWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Sort array of objects by string property valueHow to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Has any country ever had 2 former presidents in jail simultaneously?

How do you make your own symbol when Detexify fails?

What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?

Why is so much work done on numerical verification of the Riemann Hypothesis?

I am looking for the correct translation of love for the phrase "in this sign love"

Problem with TransformedDistribution

Closed-form expression for certain product

Calculating Wattage for Resistor in High Frequency Application?

Yosemite Fire Rings - What to Expect?

Is this toilet slogan correct usage of the English language?

Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed

Why does the Sun have different day lengths, but not the gas giants?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Why is it that I can sometimes guess the next note?

250 Floor Tower

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Store Credit Card Information in Password Manager?

Delivering sarcasm

How much character growth crosses the line into breaking the character

Create all possible words using a set or letters

Aragorn's "guise" in the Orthanc Stone

Is it improper etiquette to ask your opponent what his/her rating is before the game?

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?



Filtering an array of objects that contain arrays


What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Sort array of objects by string property valueHow to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?













6















This is a smaller version of the array i have but it has the same structure



with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order






const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();





what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this










share|improve this question
























  • Might this be better off on codereview.stackexchange.com?

    – rh16
    Mar 8 at 5:18















6















This is a smaller version of the array i have but it has the same structure



with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order






const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();





what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this










share|improve this question
























  • Might this be better off on codereview.stackexchange.com?

    – rh16
    Mar 8 at 5:18













6












6








6


2






This is a smaller version of the array i have but it has the same structure



with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order






const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();





what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this










share|improve this question
















This is a smaller version of the array i have but it has the same structure



with const arr below, i want to create 2 new arrays with unique values that are sorted in ascending order






const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();





what i have above works, but seems a bit messy and was wandering if there was a better/tidier way of doing this






const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();





const arr = [
tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

const finalTags = [];
const finalWeight = [];

// TODO: find a better way to do this
arr.forEach(v =>
if (finalWeight.indexOf(v.weight) === -1) finalWeight.push(v.weight);
v.tags.forEach(val =>
if (finalTags.indexOf(val) === -1) finalTags.push(val);
);
);

// Ascending order
finalTags.sort();
finalWeight.sort();






javascript arrays sorting ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 5:16









Aniket G

2,4211528




2,4211528










asked Mar 8 at 5:05









ace2caseace2case

415




415












  • Might this be better off on codereview.stackexchange.com?

    – rh16
    Mar 8 at 5:18

















  • Might this be better off on codereview.stackexchange.com?

    – rh16
    Mar 8 at 5:18
















Might this be better off on codereview.stackexchange.com?

– rh16
Mar 8 at 5:18





Might this be better off on codereview.stackexchange.com?

– rh16
Mar 8 at 5:18












3 Answers
3






active

oldest

votes


















2














You can use Array.prototype.reduce() combined with Set in order to get an object with the sorted arrays tags: [], weights: []:






const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
const obj = arr.reduce((a, tags, weight) =>
a.tags = [...new Set(a.tags.concat(tags))];
a.weights = [...new Set(a.weights.concat(weight))];
return a;
, tags: [], weights: []);

// final result i want
console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

.as-console-wrapper max-height: 100% !important; top: 0; 








share|improve this answer




















  • 1





    I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

    – Shidersz
    Mar 8 at 6:07












  • Thank you for your comment @Shidersz. I have added a better description and you also have my up..

    – Yosvel Quintero
    Mar 8 at 6:34











  • much nicer than my version, thanks for all your answers

    – ace2case
    Mar 8 at 22:26


















2














One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:






const arr = [

tags: ['f', 'b', 'd'],
weight: 7,
something: 'sdfsdf'
,

tags: ['a', 'b', 'c', 'd', 'e'],
weight: 6,
something: 'frddd'
,

tags: ['f', 'c', 'e', 'a'],
weight: 7,
something: 'ththh'
,

tags: ['a', 'c', 'g', 'e'],
weight: 5,
something: 'ghjghj'

];

let res = arr.reduce((acc, tags, weight) =>

acc.tags = new Set([...acc.tags, ...tags]);
acc.weights.add(weight);
return acc;
, tags: new Set(), weights: new Set());

let sortedWeigths = [...res.weights].sort();
let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

.as-console background-color:black !important; color:lime;
.as-console-wrapper max-height:100% !important; top:0;








share|improve this answer






























    0














    You could use the following code. This basically splits arr up into finalTags and finalWeights.



    .flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])



    finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); remove's the duplicates.






    const arr = [
    tags: ['f', 'b', 'd'],
    weight: 7,
    something: 'sdfsdf'
    ,

    tags: ['a', 'b', 'c', 'd', 'e'],
    weight: 6,
    something: 'frddd'
    ,

    tags: ['f', 'c', 'e', 'a'],
    weight: 7,
    something: 'ththh'
    ,

    tags: ['a', 'c', 'g', 'e'],
    weight: 5,
    something: 'ghjghj'

    ];

    let finalTags = arr.map(e => e.tags);
    finalTags = finalTags.flat();
    finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

    let finalWeight = arr.map(e => e.weight);
    finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

    console.log(finalTags);
    console.log(finalWeight);





    Sources:



    Removing duplicates: https://gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057025%2ffiltering-an-array-of-objects-that-contain-arrays%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use Array.prototype.reduce() combined with Set in order to get an object with the sorted arrays tags: [], weights: []:






      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 








      share|improve this answer




















      • 1





        I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

        – Shidersz
        Mar 8 at 6:07












      • Thank you for your comment @Shidersz. I have added a better description and you also have my up..

        – Yosvel Quintero
        Mar 8 at 6:34











      • much nicer than my version, thanks for all your answers

        – ace2case
        Mar 8 at 22:26















      2














      You can use Array.prototype.reduce() combined with Set in order to get an object with the sorted arrays tags: [], weights: []:






      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 








      share|improve this answer




















      • 1





        I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

        – Shidersz
        Mar 8 at 6:07












      • Thank you for your comment @Shidersz. I have added a better description and you also have my up..

        – Yosvel Quintero
        Mar 8 at 6:34











      • much nicer than my version, thanks for all your answers

        – ace2case
        Mar 8 at 22:26













      2












      2








      2







      You can use Array.prototype.reduce() combined with Set in order to get an object with the sorted arrays tags: [], weights: []:






      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 








      share|improve this answer















      You can use Array.prototype.reduce() combined with Set in order to get an object with the sorted arrays tags: [], weights: []:






      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 








      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 





      const arr = [tags: ['f', 'b', 'd'],weight: 7,something: 'sdfsdf',tags: ['a', 'b', 'c', 'd', 'e'],weight: 6,something: 'frddd',tags: ['f', 'c', 'e', 'a'],weight: 7,something: 'ththh',tags: ['a', 'c', 'g', 'e'],weight: 5,something: 'ghjghj'];
      const obj = arr.reduce((a, tags, weight) =>
      a.tags = [...new Set(a.tags.concat(tags))];
      a.weights = [...new Set(a.weights.concat(weight))];
      return a;
      , tags: [], weights: []);

      // final result i want
      console.log('finalTags:', obj.tags.sort()); // ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
      console.log('finalWeight:', obj.weights.sort()); // [5, 6, 7];

      .as-console-wrapper max-height: 100% !important; top: 0; 






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 9 at 1:41

























      answered Mar 8 at 5:16









      Yosvel QuinteroYosvel Quintero

      11.8k42531




      11.8k42531







      • 1





        I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

        – Shidersz
        Mar 8 at 6:07












      • Thank you for your comment @Shidersz. I have added a better description and you also have my up..

        – Yosvel Quintero
        Mar 8 at 6:34











      • much nicer than my version, thanks for all your answers

        – ace2case
        Mar 8 at 22:26












      • 1





        I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

        – Shidersz
        Mar 8 at 6:07












      • Thank you for your comment @Shidersz. I have added a better description and you also have my up..

        – Yosvel Quintero
        Mar 8 at 6:34











      • much nicer than my version, thanks for all your answers

        – ace2case
        Mar 8 at 22:26







      1




      1





      I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

      – Shidersz
      Mar 8 at 6:07






      I will vote you up because you had a similar idea to mine and was first. However, it will be nice to see some explanation too.

      – Shidersz
      Mar 8 at 6:07














      Thank you for your comment @Shidersz. I have added a better description and you also have my up..

      – Yosvel Quintero
      Mar 8 at 6:34





      Thank you for your comment @Shidersz. I have added a better description and you also have my up..

      – Yosvel Quintero
      Mar 8 at 6:34













      much nicer than my version, thanks for all your answers

      – ace2case
      Mar 8 at 22:26





      much nicer than my version, thanks for all your answers

      – ace2case
      Mar 8 at 22:26













      2














      One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:






      const arr = [

      tags: ['f', 'b', 'd'],
      weight: 7,
      something: 'sdfsdf'
      ,

      tags: ['a', 'b', 'c', 'd', 'e'],
      weight: 6,
      something: 'frddd'
      ,

      tags: ['f', 'c', 'e', 'a'],
      weight: 7,
      something: 'ththh'
      ,

      tags: ['a', 'c', 'g', 'e'],
      weight: 5,
      something: 'ghjghj'

      ];

      let res = arr.reduce((acc, tags, weight) =>

      acc.tags = new Set([...acc.tags, ...tags]);
      acc.weights.add(weight);
      return acc;
      , tags: new Set(), weights: new Set());

      let sortedWeigths = [...res.weights].sort();
      let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
      console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

      .as-console background-color:black !important; color:lime;
      .as-console-wrapper max-height:100% !important; top:0;








      share|improve this answer



























        2














        One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:






        const arr = [

        tags: ['f', 'b', 'd'],
        weight: 7,
        something: 'sdfsdf'
        ,

        tags: ['a', 'b', 'c', 'd', 'e'],
        weight: 6,
        something: 'frddd'
        ,

        tags: ['f', 'c', 'e', 'a'],
        weight: 7,
        something: 'ththh'
        ,

        tags: ['a', 'c', 'g', 'e'],
        weight: 5,
        something: 'ghjghj'

        ];

        let res = arr.reduce((acc, tags, weight) =>

        acc.tags = new Set([...acc.tags, ...tags]);
        acc.weights.add(weight);
        return acc;
        , tags: new Set(), weights: new Set());

        let sortedWeigths = [...res.weights].sort();
        let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
        console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

        .as-console background-color:black !important; color:lime;
        .as-console-wrapper max-height:100% !important; top:0;








        share|improve this answer

























          2












          2








          2







          One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:






          const arr = [

          tags: ['f', 'b', 'd'],
          weight: 7,
          something: 'sdfsdf'
          ,

          tags: ['a', 'b', 'c', 'd', 'e'],
          weight: 6,
          something: 'frddd'
          ,

          tags: ['f', 'c', 'e', 'a'],
          weight: 7,
          something: 'ththh'
          ,

          tags: ['a', 'c', 'g', 'e'],
          weight: 5,
          something: 'ghjghj'

          ];

          let res = arr.reduce((acc, tags, weight) =>

          acc.tags = new Set([...acc.tags, ...tags]);
          acc.weights.add(weight);
          return acc;
          , tags: new Set(), weights: new Set());

          let sortedWeigths = [...res.weights].sort();
          let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
          console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

          .as-console background-color:black !important; color:lime;
          .as-console-wrapper max-height:100% !important; top:0;








          share|improve this answer













          One solution is to use Array.reduce() to create two sets, one with the tags and other with the weights. After this you can transform the sets to arrays and use Array.sort() on they:






          const arr = [

          tags: ['f', 'b', 'd'],
          weight: 7,
          something: 'sdfsdf'
          ,

          tags: ['a', 'b', 'c', 'd', 'e'],
          weight: 6,
          something: 'frddd'
          ,

          tags: ['f', 'c', 'e', 'a'],
          weight: 7,
          something: 'ththh'
          ,

          tags: ['a', 'c', 'g', 'e'],
          weight: 5,
          something: 'ghjghj'

          ];

          let res = arr.reduce((acc, tags, weight) =>

          acc.tags = new Set([...acc.tags, ...tags]);
          acc.weights.add(weight);
          return acc;
          , tags: new Set(), weights: new Set());

          let sortedWeigths = [...res.weights].sort();
          let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
          console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

          .as-console background-color:black !important; color:lime;
          .as-console-wrapper max-height:100% !important; top:0;








          const arr = [

          tags: ['f', 'b', 'd'],
          weight: 7,
          something: 'sdfsdf'
          ,

          tags: ['a', 'b', 'c', 'd', 'e'],
          weight: 6,
          something: 'frddd'
          ,

          tags: ['f', 'c', 'e', 'a'],
          weight: 7,
          something: 'ththh'
          ,

          tags: ['a', 'c', 'g', 'e'],
          weight: 5,
          something: 'ghjghj'

          ];

          let res = arr.reduce((acc, tags, weight) =>

          acc.tags = new Set([...acc.tags, ...tags]);
          acc.weights.add(weight);
          return acc;
          , tags: new Set(), weights: new Set());

          let sortedWeigths = [...res.weights].sort();
          let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
          console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

          .as-console background-color:black !important; color:lime;
          .as-console-wrapper max-height:100% !important; top:0;





          const arr = [

          tags: ['f', 'b', 'd'],
          weight: 7,
          something: 'sdfsdf'
          ,

          tags: ['a', 'b', 'c', 'd', 'e'],
          weight: 6,
          something: 'frddd'
          ,

          tags: ['f', 'c', 'e', 'a'],
          weight: 7,
          something: 'ththh'
          ,

          tags: ['a', 'c', 'g', 'e'],
          weight: 5,
          something: 'ghjghj'

          ];

          let res = arr.reduce((acc, tags, weight) =>

          acc.tags = new Set([...acc.tags, ...tags]);
          acc.weights.add(weight);
          return acc;
          , tags: new Set(), weights: new Set());

          let sortedWeigths = [...res.weights].sort();
          let sortedTags = [...res.tags].sort((a, b) => a.localeCompare(b));
          console.log("weights: ", sortedWeigths, "tags: ", sortedTags);

          .as-console background-color:black !important; color:lime;
          .as-console-wrapper max-height:100% !important; top:0;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 5:21









          ShiderszShidersz

          9,1212833




          9,1212833





















              0














              You could use the following code. This basically splits arr up into finalTags and finalWeights.



              .flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])



              finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); remove's the duplicates.






              const arr = [
              tags: ['f', 'b', 'd'],
              weight: 7,
              something: 'sdfsdf'
              ,

              tags: ['a', 'b', 'c', 'd', 'e'],
              weight: 6,
              something: 'frddd'
              ,

              tags: ['f', 'c', 'e', 'a'],
              weight: 7,
              something: 'ththh'
              ,

              tags: ['a', 'c', 'g', 'e'],
              weight: 5,
              something: 'ghjghj'

              ];

              let finalTags = arr.map(e => e.tags);
              finalTags = finalTags.flat();
              finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

              let finalWeight = arr.map(e => e.weight);
              finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

              console.log(finalTags);
              console.log(finalWeight);





              Sources:



              Removing duplicates: https://gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/






              share|improve this answer





























                0














                You could use the following code. This basically splits arr up into finalTags and finalWeights.



                .flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])



                finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); remove's the duplicates.






                const arr = [
                tags: ['f', 'b', 'd'],
                weight: 7,
                something: 'sdfsdf'
                ,

                tags: ['a', 'b', 'c', 'd', 'e'],
                weight: 6,
                something: 'frddd'
                ,

                tags: ['f', 'c', 'e', 'a'],
                weight: 7,
                something: 'ththh'
                ,

                tags: ['a', 'c', 'g', 'e'],
                weight: 5,
                something: 'ghjghj'

                ];

                let finalTags = arr.map(e => e.tags);
                finalTags = finalTags.flat();
                finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

                let finalWeight = arr.map(e => e.weight);
                finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

                console.log(finalTags);
                console.log(finalWeight);





                Sources:



                Removing duplicates: https://gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/






                share|improve this answer



























                  0












                  0








                  0







                  You could use the following code. This basically splits arr up into finalTags and finalWeights.



                  .flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])



                  finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); remove's the duplicates.






                  const arr = [
                  tags: ['f', 'b', 'd'],
                  weight: 7,
                  something: 'sdfsdf'
                  ,

                  tags: ['a', 'b', 'c', 'd', 'e'],
                  weight: 6,
                  something: 'frddd'
                  ,

                  tags: ['f', 'c', 'e', 'a'],
                  weight: 7,
                  something: 'ththh'
                  ,

                  tags: ['a', 'c', 'g', 'e'],
                  weight: 5,
                  something: 'ghjghj'

                  ];

                  let finalTags = arr.map(e => e.tags);
                  finalTags = finalTags.flat();
                  finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

                  let finalWeight = arr.map(e => e.weight);
                  finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

                  console.log(finalTags);
                  console.log(finalWeight);





                  Sources:



                  Removing duplicates: https://gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/






                  share|improve this answer















                  You could use the following code. This basically splits arr up into finalTags and finalWeights.



                  .flat() flattens the array ([1, [2, [3]]] would become [1, 2, 3])



                  finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort(); remove's the duplicates.






                  const arr = [
                  tags: ['f', 'b', 'd'],
                  weight: 7,
                  something: 'sdfsdf'
                  ,

                  tags: ['a', 'b', 'c', 'd', 'e'],
                  weight: 6,
                  something: 'frddd'
                  ,

                  tags: ['f', 'c', 'e', 'a'],
                  weight: 7,
                  something: 'ththh'
                  ,

                  tags: ['a', 'c', 'g', 'e'],
                  weight: 5,
                  something: 'ghjghj'

                  ];

                  let finalTags = arr.map(e => e.tags);
                  finalTags = finalTags.flat();
                  finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

                  let finalWeight = arr.map(e => e.weight);
                  finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

                  console.log(finalTags);
                  console.log(finalWeight);





                  Sources:



                  Removing duplicates: https://gomakethings.com/removing-duplicates-from-an-array-with-vanilla-javascript/






                  const arr = [
                  tags: ['f', 'b', 'd'],
                  weight: 7,
                  something: 'sdfsdf'
                  ,

                  tags: ['a', 'b', 'c', 'd', 'e'],
                  weight: 6,
                  something: 'frddd'
                  ,

                  tags: ['f', 'c', 'e', 'a'],
                  weight: 7,
                  something: 'ththh'
                  ,

                  tags: ['a', 'c', 'g', 'e'],
                  weight: 5,
                  something: 'ghjghj'

                  ];

                  let finalTags = arr.map(e => e.tags);
                  finalTags = finalTags.flat();
                  finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

                  let finalWeight = arr.map(e => e.weight);
                  finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

                  console.log(finalTags);
                  console.log(finalWeight);





                  const arr = [
                  tags: ['f', 'b', 'd'],
                  weight: 7,
                  something: 'sdfsdf'
                  ,

                  tags: ['a', 'b', 'c', 'd', 'e'],
                  weight: 6,
                  something: 'frddd'
                  ,

                  tags: ['f', 'c', 'e', 'a'],
                  weight: 7,
                  something: 'ththh'
                  ,

                  tags: ['a', 'c', 'g', 'e'],
                  weight: 5,
                  something: 'ghjghj'

                  ];

                  let finalTags = arr.map(e => e.tags);
                  finalTags = finalTags.flat();
                  finalTags = finalTags.filter((item, index) => finalTags.indexOf(item) >= index).sort();

                  let finalWeight = arr.map(e => e.weight);
                  finalWeight = finalWeight.filter((item, index) => finalWeight.indexOf(item) >= index).sort();

                  console.log(finalTags);
                  console.log(finalWeight);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 8 at 5:30

























                  answered Mar 8 at 5:23









                  Aniket GAniket G

                  2,4211528




                  2,4211528



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55057025%2ffiltering-an-array-of-objects-that-contain-arrays%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme