Generate a list of all possible variations of an array of objects in Javascript
Note: This question is different than other permutation questions because it's an array of objects and not an array of integers.
I have an array of objects like this:
var options =
[
{
name: "Size",
choices: [
{
label: "Small",
price: 0
},
{
label: "Medium",
price: 10
}
]
},
{
name: "Color",
choices: [
{
label: "Yellow",
price: 0
},
{
label: "Purple",
price: 10
}
]
}
]
I want to make a function that generates all possible variations into an object like this:
{
"Size-Small--Color-Yellow": {
label: "Size: Small, Color: Yellow",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Small--Color-Purple": {
label: "Size: Small, Color: Purple",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Purple",
price: 10,
}
},
},
"Size-Medium--Color-Yellow": {
label: "Size: Medium, Color: Yellow",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Medium--Color-Purple": {
label: "Size: Medium, Color: Purple",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Purple",
price: 10,
}
},
}
}
The real array of objects is much larger which a lot more options and choices, so the solution would need to work with various array sizes, from small like this to very large sets of options.
Here is what I tried so far:
function generateVariations() {
var variations = {};
for(var i in options) {
var option = options[i];
var key = '';
var keyArray = ;
for(var j in option.choices) {
var choice = option.choices[j];
console.log(choice, 'choice value');
keyArray.push(option.name + '-' + choice.label)
}
key = keyArray.join('--');
console.log(key, 'key made for object');
variations[key] = {}; // TODO: After generating all of the keys, assign them correct values.
}
return variations;
}
I'm having trouble wrapping my head around the recursive aspect of this, since there could potentially be unlimited numbers of variations, not sure if it needs recursion or not.
javascript
add a comment |
Note: This question is different than other permutation questions because it's an array of objects and not an array of integers.
I have an array of objects like this:
var options =
[
{
name: "Size",
choices: [
{
label: "Small",
price: 0
},
{
label: "Medium",
price: 10
}
]
},
{
name: "Color",
choices: [
{
label: "Yellow",
price: 0
},
{
label: "Purple",
price: 10
}
]
}
]
I want to make a function that generates all possible variations into an object like this:
{
"Size-Small--Color-Yellow": {
label: "Size: Small, Color: Yellow",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Small--Color-Purple": {
label: "Size: Small, Color: Purple",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Purple",
price: 10,
}
},
},
"Size-Medium--Color-Yellow": {
label: "Size: Medium, Color: Yellow",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Medium--Color-Purple": {
label: "Size: Medium, Color: Purple",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Purple",
price: 10,
}
},
}
}
The real array of objects is much larger which a lot more options and choices, so the solution would need to work with various array sizes, from small like this to very large sets of options.
Here is what I tried so far:
function generateVariations() {
var variations = {};
for(var i in options) {
var option = options[i];
var key = '';
var keyArray = ;
for(var j in option.choices) {
var choice = option.choices[j];
console.log(choice, 'choice value');
keyArray.push(option.name + '-' + choice.label)
}
key = keyArray.join('--');
console.log(key, 'key made for object');
variations[key] = {}; // TODO: After generating all of the keys, assign them correct values.
}
return variations;
}
I'm having trouble wrapping my head around the recursive aspect of this, since there could potentially be unlimited numbers of variations, not sure if it needs recursion or not.
javascript
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
1
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18
add a comment |
Note: This question is different than other permutation questions because it's an array of objects and not an array of integers.
I have an array of objects like this:
var options =
[
{
name: "Size",
choices: [
{
label: "Small",
price: 0
},
{
label: "Medium",
price: 10
}
]
},
{
name: "Color",
choices: [
{
label: "Yellow",
price: 0
},
{
label: "Purple",
price: 10
}
]
}
]
I want to make a function that generates all possible variations into an object like this:
{
"Size-Small--Color-Yellow": {
label: "Size: Small, Color: Yellow",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Small--Color-Purple": {
label: "Size: Small, Color: Purple",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Purple",
price: 10,
}
},
},
"Size-Medium--Color-Yellow": {
label: "Size: Medium, Color: Yellow",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Medium--Color-Purple": {
label: "Size: Medium, Color: Purple",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Purple",
price: 10,
}
},
}
}
The real array of objects is much larger which a lot more options and choices, so the solution would need to work with various array sizes, from small like this to very large sets of options.
Here is what I tried so far:
function generateVariations() {
var variations = {};
for(var i in options) {
var option = options[i];
var key = '';
var keyArray = ;
for(var j in option.choices) {
var choice = option.choices[j];
console.log(choice, 'choice value');
keyArray.push(option.name + '-' + choice.label)
}
key = keyArray.join('--');
console.log(key, 'key made for object');
variations[key] = {}; // TODO: After generating all of the keys, assign them correct values.
}
return variations;
}
I'm having trouble wrapping my head around the recursive aspect of this, since there could potentially be unlimited numbers of variations, not sure if it needs recursion or not.
javascript
Note: This question is different than other permutation questions because it's an array of objects and not an array of integers.
I have an array of objects like this:
var options =
[
{
name: "Size",
choices: [
{
label: "Small",
price: 0
},
{
label: "Medium",
price: 10
}
]
},
{
name: "Color",
choices: [
{
label: "Yellow",
price: 0
},
{
label: "Purple",
price: 10
}
]
}
]
I want to make a function that generates all possible variations into an object like this:
{
"Size-Small--Color-Yellow": {
label: "Size: Small, Color: Yellow",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Small--Color-Purple": {
label: "Size: Small, Color: Purple",
choices: {
Size: {
label: "Small",
price: 0
},
Color: {
label: "Purple",
price: 10,
}
},
},
"Size-Medium--Color-Yellow": {
label: "Size: Medium, Color: Yellow",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Yellow",
price: 0,
}
},
},
"Size-Medium--Color-Purple": {
label: "Size: Medium, Color: Purple",
choices: {
Size: {
label: "Medium",
price: 10
},
Color: {
label: "Purple",
price: 10,
}
},
}
}
The real array of objects is much larger which a lot more options and choices, so the solution would need to work with various array sizes, from small like this to very large sets of options.
Here is what I tried so far:
function generateVariations() {
var variations = {};
for(var i in options) {
var option = options[i];
var key = '';
var keyArray = ;
for(var j in option.choices) {
var choice = option.choices[j];
console.log(choice, 'choice value');
keyArray.push(option.name + '-' + choice.label)
}
key = keyArray.join('--');
console.log(key, 'key made for object');
variations[key] = {}; // TODO: After generating all of the keys, assign them correct values.
}
return variations;
}
I'm having trouble wrapping my head around the recursive aspect of this, since there could potentially be unlimited numbers of variations, not sure if it needs recursion or not.
javascript
javascript
edited Jan 2 at 23:15
Jordash
asked Jan 2 at 23:11
JordashJordash
98841535
98841535
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
1
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18
add a comment |
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
1
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
1
1
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18
add a comment |
1 Answer
1
active
oldest
votes
Using recursion is probably the most elegant way here, and I would use a generator function as you can generate the values step by step, which will also work if there is an infinite number of combimations:
function* combine(options, index = 0, previous = {}) {
if(index >= options.length) yield previous;
const { name, choices } = options[index];
for(const { label, price } of choices) {
yield* combine(options, index + 1, { ...previous, [name]: { label, price }});
}
}
Usable as:
for(const combo of combinations(options))
console.log(combo);
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
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%2f54014374%2fgenerate-a-list-of-all-possible-variations-of-an-array-of-objects-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using recursion is probably the most elegant way here, and I would use a generator function as you can generate the values step by step, which will also work if there is an infinite number of combimations:
function* combine(options, index = 0, previous = {}) {
if(index >= options.length) yield previous;
const { name, choices } = options[index];
for(const { label, price } of choices) {
yield* combine(options, index + 1, { ...previous, [name]: { label, price }});
}
}
Usable as:
for(const combo of combinations(options))
console.log(combo);
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
add a comment |
Using recursion is probably the most elegant way here, and I would use a generator function as you can generate the values step by step, which will also work if there is an infinite number of combimations:
function* combine(options, index = 0, previous = {}) {
if(index >= options.length) yield previous;
const { name, choices } = options[index];
for(const { label, price } of choices) {
yield* combine(options, index + 1, { ...previous, [name]: { label, price }});
}
}
Usable as:
for(const combo of combinations(options))
console.log(combo);
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
add a comment |
Using recursion is probably the most elegant way here, and I would use a generator function as you can generate the values step by step, which will also work if there is an infinite number of combimations:
function* combine(options, index = 0, previous = {}) {
if(index >= options.length) yield previous;
const { name, choices } = options[index];
for(const { label, price } of choices) {
yield* combine(options, index + 1, { ...previous, [name]: { label, price }});
}
}
Usable as:
for(const combo of combinations(options))
console.log(combo);
Using recursion is probably the most elegant way here, and I would use a generator function as you can generate the values step by step, which will also work if there is an infinite number of combimations:
function* combine(options, index = 0, previous = {}) {
if(index >= options.length) yield previous;
const { name, choices } = options[index];
for(const { label, price } of choices) {
yield* combine(options, index + 1, { ...previous, [name]: { label, price }});
}
}
Usable as:
for(const combo of combinations(options))
console.log(combo);
edited Jan 3 at 10:00
answered Jan 2 at 23:21
Jonas WilmsJonas Wilms
64.6k53457
64.6k53457
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
add a comment |
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
What is the * ?
– Jordash
Jan 3 at 0:19
What is the * ?
– Jordash
Jan 3 at 0:19
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Can't get this to work in a browser
– Jordash
Jan 3 at 0:29
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
Actually looks like a generator function, can you show how to console log the generated result? I've never used that before
– Jordash
Jan 3 at 0:53
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%2f54014374%2fgenerate-a-list-of-all-possible-variations-of-an-array-of-objects-in-javascript%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
why is the price of medium + purple 10?
– Jonas Wilms
Jan 2 at 23:14
@JonasWilms Sorry was a typo, that's fixed
– Jordash
Jan 2 at 23:17
1
This question is different than other permutation questions because it's an array of objects and not an array of integers. — think of each option as a place value in a number, with as many possible values as there are choices. Thus if there are 10 option types with 3 choices each, your job is just enumerating all 59,049 values.
– Pointy
Jan 2 at 23:18