'Array of arrays, get first element and length. Javascript [closed]
Here is picture of my data I have:
Im trying to get array of objects, which will look like this:
[{
length: 2,
item: {
imgSource: "/sushi/test2.png"
ingredients: "Nori, Cucumber, Rice, Cream cheese, Sesame, Unagi sauce"
name: "sushi1"
price: 133
weight: 120}
},
{
length: 1,
item: {
imgSource: "/sushi/test1.png"
ingredients: "Salmon, Nori, Cucumber, Rice, Cream cheese"
name: "sushi2"
price: 119
weight: 120}
}]
The best way i think of is to use reduce function, but i have no idea how to use it right, can anyone help me?
Solution 1:[1]
Make a loop through all items of your array
Compare objects of the current item and the result array (How to determine equality for two JavaScript objects?)
If the same item found - inc the value "length" of that result array.
If it is not found - add a new item to the result array with the length = 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shivansh Bandi |

