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?
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
javascript arrays sorting ecmascript-6
add a comment |
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
javascript arrays sorting ecmascript-6
Might this be better off on codereview.stackexchange.com?
– rh16
Mar 8 at 5:18
add a comment |
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
javascript arrays sorting ecmascript-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
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
javascript arrays sorting ecmascript-6
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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;
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
add a comment |
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;
add a comment |
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/
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Mar 8 at 5:21
ShiderszShidersz
9,1212833
9,1212833
add a comment |
add a comment |
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/
add a comment |
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/
add a comment |
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/
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);
edited Mar 8 at 5:30
answered Mar 8 at 5:23
Aniket GAniket G
2,4211528
2,4211528
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Might this be better off on codereview.stackexchange.com?
– rh16
Mar 8 at 5:18