This question already has answers here:
How can I create every combination possible for the contents of two arrays?
(16 answers)
Cartesian product of multiple arrays in JavaScript
(35 answers)
Closed 1 year ago.
Ive got 3 parent arrays that could sometimes be different lengths which are called Options
Each Option has values array which can also be different lengths
Example of this would be something like this
[
{
name: "Option 1",
values: ["Blue","Red","Orange"]
},
{
name: "Option 2",
values: ["Small","Medium","Large"]
},
{
name: "Option 3",
values: ["Cotton","Satin"]
}
]
The outcome should generate an array like this
[
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Small/Satin",
price: "10.00"
},
{
title: "Blue/Small/Cotton",
price: "10.00"
},
{
title: "Blue/Medium/Satin",
price: "10.00"
},
]
And So on.
Ive tried mapping through the options but knowing the size of the multiple arrays is what I couldnt figure out
SOLUTION
I managed to get a solution
product.options.reduce(
(a, b) => a.flatMap((x) => b.values.map((y) => [...x, y])),
[[]]
);
Related
This question already has answers here:
Filter array of objects with another array of objects
(11 answers)
Closed 4 months ago.
I have two sample arrays below.
let arr1 = [
{ key: "WIHUGDYVWJ", className: "science" },
{ key: "qwljdhkuqwdnqwdk", className: "english" },
{ key: "likubqwd", className: "robotics" }
];
let arr2 = [
{ key: "WIHUGDYVWJ", title: "math" },
{ key: "qwljdhkuqwdnqwdk", title: "english" },
{ key: "likubqwd", title: "robotics" }
];
How can I filter arr1 to get only items that have 'className' value that matches arr2's item's 'title' value? (expecting only items with 'english' and 'robotics' to remain)
How can I filter arr1 to get only items that have 'className' value that do not match arr2's item's 'title' value? (expecting only items with 'science' to remain)
Thanks!
let arr1 = [
{ key: "WIHUGDYVWJ", className: "science" },
{ key: "qwljdhkuqwdnqwdk", className: "english" },
{ key: "likubqwd", className: "robotics" },
{ key: "abcd", className: "history" }
];
let arr2 = [
{ key: "WIHUGDYVWJ", title: "math" },
{ key: "qwljdhkuqwdnqwdk", title: "english" },
{ key: "likubqwd", title: "robotics" }
];
// q1 answer
console.log(arr1.map(arr1Item => arr2.filter(arr2Item => arr1Item.className === arr2Item.title)).flat());
// q2 answer
console.log(arr1.filter(arr1Item => !arr2.some(arr2Item => arr2Item.title === arr1Item.className)));
i wrote without using a for, if/else statement as much as possible.
this code may not be the best. i think it could be more improved.
I hope my answer is helpful
This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 3 years ago.
I need to sort list of items by its priority(ascending order) ?
var priorityIssuesList = [
{ name: "Danger", status: "RED", priority:4},
{ name: "Moderate", status: "BLUE", priority:2},
{ name: "Safe", status: "GREEN",priority:1},
{name: "Warning", status: "Yellow",priority:3}
]
sorting should be in ascending order or status
[
{"name":"Safe","status":"GREEN","priority":1},
{"name":"Moderate","status":"BLUE","priority":2},
{"name":"Warning","status":"Yellow","priority":3},
{"name":"Danger","status":"RED","priority":4}
]
what is the best way to do this?
try Array.sort() functionality
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for ascending order
priorityIssuesList.sort((a, b) => b.priority - a.priority); //for descending order
console.log(priorityIssuesList);
This question already has answers here:
Build tree array from flat array in javascript
(34 answers)
Javascript: Building a hierarchical tree
(6 answers)
Closed 5 years ago.
I'm trying to make build nested hierarchy from the defined input data, and I`m faced some problem with the deep level hierarchy. So, as an input there is array of objects, looks like this:
[
{
id: 1,
parentId: 1,
},
{
id: 2,
parentId: 1,
},
{
id: 3,
parentId: 2,
},
{
id: 4,
parentId: 3,
},
...
]
Every element has his own id and parentId.
As outcome, I want to have the structure, grouped by the parent id's, smth like this:
{
id: 1,
sub: [
{
id: 2,
parentId: 1,
sub: [
{
id: 3,
parenId: 2,
},
....
],
},
],
}
So, the problem is how to build algorithm that will recursively goes through this input data, and build such structured outcome? When I know the level of hierarchy, I could build algorithm with defined level of nested loops, but the problem is with unknown number of deeper.
This question already has answers here:
JavaScript merging objects by id [duplicate]
(18 answers)
Closed 6 years ago.
I have an object called probe. It has a property called sensors which is an array.
var probe = {
sensors = [
{ id: 1, checked: false },
{ id: 2, checked: true },
{ id: 3, checked: false },
... //more sensors
],
... //other properties
}
I have separate array which has an updated list of sensors like below.
var updatedSensors = [
{ id: 1, checked: true },
{ id: 3, checked: true }
];
I want to update the sensors array in the probe object from the values in the updatedSensors. How would I do that?
This can be easily achieved by using a couple of for-loops. But for-loops are not the pretty way of iterating in JavaScript, so I was wondering how to do this the preferred way.
Edit:
The objects in the updatedSensors is a subset of objects in probe.sensors. In other words, updatedSensors does not have all the objects (ids) that are there in the probe.sensors, but probe.sensors has all the objects (ids) that are in the updatedSensors.
Thanks.
Try this bit of code:
probe.sensors.map(function (sensor) {
// Loop through updated sensors & match sensor.id so we can reassign val
updatedSensors.map(function (f) {
if (f.id == sensor.id) {
sensor.checked = f.checked
}
})
return sensor;
})
This question already has answers here:
Deleting array elements in JavaScript - delete vs splice
(29 answers)
Closed 8 years ago.
I am deleting an element from an array using delete
I ave this object:
obj = {
Variables: [ {
Name: "test",
Type: "Int",
Control: "U",
Value: "123"
}, {
Name: "ftr",
Type: "DateTime",
Control: "UA",
Value: "123123"
}, {
Name: "wertwe",
Type: "Int",
Control: "SA",
Value: "435345"
} ]
};
using this code:
delete data["Variables"][2];
Now obj contains the value:
{"Variables":[{"Name":"test","Type":"Int","Control":"U","Value":"123"},{"Name":"ftr","Type":"DateTime","Control":"UA","Value":"123123"},null]}
Is there any way to delete an element without a null value appearing in the object?
You punctured a hole in the array. You will need to use splice to remove the hole. I found the dupe after writing this answer so I'm going to vote to close with the Q/A where the example is.