V
V
Vic Shostak2018-03-19 12:43:07
JavaScript
Vic Shostak, 2018-03-19 12:43:07

Comparing two JSON arrays and replacing duplicates in modern JavaScript (ES6)?

Good time of the year.
There are two huge JSON arrays with commodity items, which arrives from the backend. The first is a list of goods, prices and modifications common to all users. The second is personal for each user, where there may be a price for any product modification.
The first (main) looks like:

[
    {
        "id": 1,
        "name": "Межкомнатная дверь",
        "price_list": [
            [ 0, 1, 2, 3, 8300 ],
            // где:
            // 0 — ID типоразмера, 1 — ID цвета, 2 — ID коллекции, 
            // 3 — ID стиля, 8300 — цена в руб.
            // + ещё несколько модификаций этой же двери 
            // с ценой в таком же формате
        ]
    },
    {
        // ... следующие 100500 дверей с модификациями и ценами
    }
]

The second (secondary) looks like:
[
    {
        "id": 1,
        "name": "Межкомнатная дверь",
        "user_id": 1, // ID пользователя, у которого своя колонка цен на сайте
        "price_list": [
            [ 0, 1, 2, 3, 4300 ] // своя цена только на эту модель двери и всё
        ]
    }
]

Can you please tell me how can I compare these JSON arrays and replace the found duplicates in the first (general) with those in the second (user)?
Unfortunately, this cannot be done on the backend, so you have to dance from what is - from the front.

I will be glad to sensible comments.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-03-19
@vikkyshostak

something like this
, or sort through the arrays yourself like something like this:

Arr = Arr1.map(el=>Arr2.find(el2=>el2.id === el.id) || el)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question