JavaScript:在最终使用之前修改promise中的JSON条目

时间:2018-06-08 13:36:22

标签: javascript json fetch

所以我遇到了麻烦。我有一个从json文件填充的地图,它完美地运行。但是,客户希望将pin弹出窗口中的描述截断为300个章程(而不是实际编写适合的文本)。我以为我可以改变承诺中的描述,但它不起作用,实际上它根本没有效果。如果有人知道如何做到这一点,我真的很感激。

fetch('/assets/map/markers.json')
    .then(response => {
        if (response.ok) {
            return response.json();
        }
        throw new Error('Network response error.');
    })
    .then(charData => {
        let mapData = charData.map(item => item.description.split(0, 300).join(" "));
        createMap(mapData);
    })
    .catch(error => {
        console.log('There has been a problem: ', error.message);
    });

1 个答案:

答案 0 :(得分:2)

要从字符串中取出一些字符,您可以使用String.prototype.substring()

所以改变

item.description.split(0, 300).join(" ")

item.description.substring(0, 300)