Добавление счетчика для отображения количества нажатий кнопки в теге p

Учитывая, что ваш const links = req.renderData.templateFields представляет собой следующий массив объектов (на самом деле это массив, а не такой объект, как вы упомянули в своем вопросе):

[{
    // ... bla bla
    value: 'http://localhost:3000/posts/testing',
},
{
    // ... bla bla
    value: '<p>Testing the post</p>',
}]

Затем это как его улучшить с свойством, содержащим количество (количество) символов для каждого атрибута value:

// Simple way to count the number of letters in a random string,
// see https://stackoverflow.com/a/7349353/1333836 but feel free
// to use any method for calculating the count you prefer.
const countCharacters =
    (str) => str.replace(/[^A-Z]/gi, "").length;

const enhancedWithCount = links.map(link => {
    // for each link, introduce a `count` attribute;
    link.count = countCharacters(link.value);

    return link;
});

// Finally, do res.json(enhancedWithCount)
// or whatever you want with the `enhancedWithCount` array anyways.

Вот как выглядит enhancedWithCount:

[{
    // ... bla bla
    value: 'http://localhost:3000/posts/testing',
    count: 25
},
{
    // ... bla bla
    value: '<p>Testing the post</p>',
    count: 16
}]
1
задан Marco Chavez 19 January 2019 в 07:57
поделиться