V
V
valtermild2019-07-16 03:53:10
JavaScript
valtermild, 2019-07-16 03:53:10

How to collect json?

There are elements on the page, an indefinite number of views

<div class=cartitem">item 1</div>
<div class=cartitem">item 2</div>
...

We need to convert them to this format:
"items": [
        {
          "name": "item 1"
        },
        {
          "name": "item 2"
        }
      ]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2019-07-16
@valtermild

If you are interested in an option with lower memory and CPU costs than Alexander

const items = Array.from(
  document.getElementsByClassName('cartitem'),
  ({textContent}) => ({name: textContent})
);
const itemsJSON = JSON.stringify(items);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question