7
I have an array of objects that have this structure below:
listaProdutos =
[
{nome:'Teclado',preco:28.00},
{nome:'Mouse',preco:36.00},
{nome:'Monitor',preco:500.00},
{nome:'CPU',preco:800.00},
{nome:'Teclado',preco:28.00},
{nome:'Monitor',preco:500.00},
];
My intention would be to generate an array of objects with the result below:
novoLista =
[
{nome:'Teclado',preco:56.00},
{nome:'Mouse',preco:36.00},
{nome:'Monitor',preco:1000.00},
{nome:'CPU',preco:800.00}
];
You could help me with the best approach to this situation ?
See what the hash table structure/ scattering table would be / hash table in the JS (set of key/value pairs in which the key does not repeat)? Go through the array and see if the names already exist as hash table keys, if they already exist you update the value of the array (add what was already with the object), or add the new pair.
– Piovezan
Table hash is an essential and ubiquitous data structure in computing, present in languages under several names (dictionaries, maps, hashmaps, etc.), if you do not know it, I recommend that you study and understand its working principle well, because it applies to a diversity of situations. Here at Sopt itself there is a good explanation: https://answall.com/q/27827/357
– Piovezan
I’m keeping a benchmark with every answer to this question. In order not to be unfair with any response, I suggest that the imminent user of any of the codes below change the default values of the number of elements of the benchmark to mirror the needs of the project. The performance of each solution varies significantly according to the number of unique items (
NAMES_COUNT
) and of total elements (ITEMS_COUNT
). Here is the link.– Luiz Felipe
@Luizfelipe made simulations with 3 test scenarios, with 1000 items, 100 and 10 and the results are completely different, it would be interesting to assemble these 3 scenarios to have a good report at the end
– Ricardo Pontual
@Ricardopunctual as it is, and also varies with orders of magnitude greater than
1e3
. Try with1e5
or1e6
, for example. P That’s why I suggested that the user test several according to the approximate amount that will work. But let’s face it, JS really isn’t the language for this kind of operation with many items, so this idea of benchmark is kind of pointless first of all...– Luiz Felipe
Guys, thank you so much for being helpful. I’m still a layman when it comes to testing benchmark performance. This question is based on a course that I am doing and I was left with doubts. As I did not want to do strange things (which was how I knew how to do) I preferred to consult here, to add knowledge. But honestly, in your opinion. Which of the answers would perform better with "few data" and "lots of data"? Would the difference be noticeable with "too much data"? From what I saw there, with a layman’s vision, Ricardo Pontual’s was the one that seemed to have the best result.
– Gato de Schrödinger
@Gatodeschrödinger, with few data the difference is minimal and almost imperceptible, since (although percentually the difference is visible) everything will be very fast. In larger quantities, it will make a difference. In your case, if the amount of data is small, choose the code you like best. No doubt all the solutions posted here are very elegant. :-)
– Luiz Felipe
@Gatodeschrödinger depends on from which number Voce considers "too much data" or "too little data". The nice thing about these answers is that they are distinctive solutions, with different benchmark results depending on the situation. In Bench. that Luiz Felipe showed, his solution is excellent for cases of many objects in the array (+1000), but for the simplest case, Ricardo Pontual’s solution is the one that best adapts. I even think we should study a little more about the
Map
. For me this was the question with the most interesting answers of the year :D .– Cmte Cardeal
Yes, @Cmtecardeal, But the idea is really this. For me it may be "few data", but in the future I may need the solution for "lots of data". Or other users who enter after a solution, can vary between wanting the answer to "too much data" and "too little data". The idea is that the answer really fits me. But the general spirit is that I can help future users too, with needs equal to mine, but with amounts of data that may vary from one to another.
– Gato de Schrödinger
i confess that I really liked all approaches, and the test makes clear the strengths and weaknesses of each implementation in different scenarios, it makes a lot of difference and leaves the question much richer with beyond answers, analysis comparing each of them :)
– Ricardo Pontual
Please avoid long discussions in the comments; your talk was moved to the chat
– Wallace Maxters