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

                      Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                      Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

                      How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page