Replace string in javascript array

后端 未结 7 2188
萌比男神i
萌比男神i 2020-12-08 14:10

I have an array in javascript. This array has strings that contains commas (\",\"). I want all commas to be removed from this array. Can this be done?

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 14:55

    You can use array.map or forEach. According to our scenario, array.map creates or given a new array. forEach allows you to manipulate data in an already existing array. Today I used it like this for me.

    document.addEventListener("DOMContentLoaded", () => {
        // products service
        const products = new Products();
        // get producsts from API.
        products
        .getProducts()
        .then(products => {
            /*
            raw output of data "SHEPPERD'S SALLAD" to "SHEPPERDS SALLAD"
            so I want to get an output like this and just want the object 
            to affect the title proporties. other features should stay 
            the same as it came from the db.
            */ 
            products.forEach(product => product.title = product.title.replace(/'/g,''));
            Storage.saveProducts(products);
        });
    });
    

提交回复
热议问题