D
D
D1mQ42020-12-31 19:41:25
JSON
D1mQ4, 2020-12-31 19:41:25

How to parse 2 JSON files?

I am using InfiniteAjaxScroll in my project. The project - parses some JSON.
I want to combine certain elements from both JSONs by one common field, I know how to do this, but I can’t normally open the .json file for parsing in JS. I tried to open it with various methods and return it through return, but I got all sorts of obscure Promises and so on. I don’t rummage in js, more in php, but the task needs to be completed in js.

function nextHandler(pageIndex) {
    return fetch('csgomarket.php').then(response => response.json()).then((data) => {
        let frag = document.createDocumentFragment();
        let itemsPerPage = 15;
        let totalPages = Math.ceil(data.length / itemsPerPage);
        let offset = pageIndex * itemsPerPage;
        // walk over the movie items for the current page and add them to the fragment
        for (let i = offset, len = offset + itemsPerPage; i < len; i++) {
            const movie = data[i];
            /* Здесь открытие файла api.json и добавление к массиву movie 3 элементов из файла api.json */
            let item = createMovieItem(movie);
            frag.appendChild(item);
        }

        let hasNextPage = pageIndex < totalPages - 1;

        return this.append(Array.from(frag.childNodes)).then(() => hasNextPage);
        // indicate that there is a next page to load
    });
}

There are 2 files api.json and mcsgo.json
There is an array movie , which fetches an object from the file api.json . You need to open the mcsgo.json file and, by their common name field , add the fields from mcsgo.json to the movie array .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-01-01
@D1mQ4

async function nextHandler(pageIndex) {
  const [api, mcsgo] = await Promise.all([
    fetch('api.json').then(response => response.json()),
    fetch('mcsgo.json').then(response => response.json())
  ]);
  // ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question