2

I am trying to apply a money formatter in a variable because it is only accessible to be formatted after I have already push the array. Is it possible for me to format a variable containing several json objects using map? for example:

            for (var j = 0; j < totalOfPurchased.length; j++) {
                // Declare Variables Containing Extend Details
                var purchasedProductNested = totalOfPurchased[j].purchasedProduct;
                var quantityOfPurchasedNested = totalOfPurchased[j].quantityPurchased;
                var totalOfPurchasedNested = totalOfPurchased[j].totalOfPurchased;
                var totalOfDiscountsPurchasedNested = totalOfPurchased[j].discountsToConsider;

                // Here's The Point I'm Having Trouble
                totalOfPurchasedNested = totalOfPurchasedNested.map(v => v.replace(/\./g, "").replace(",", "."));

                // Final JSON Array Containing Extend Details
                var allResponseTwoWithExtendDetails = {
                    product: purchasedProductNested,
                    quantity: quantityOfPurchasedNested,
                    totalPurchased: totalOfPurchasedNested,
                    totalDiscounts: totalOfDiscountsPurchasedNested
                };

                // Push Group With Received JSON In Native Array
                receivedDataOne.push(allResponseTwoWithExtendDetails);
            }

- Live project in JSFiddle.

When I try to apply a map to a variable an error is printed saying that totalOfPurchasedNested it is not a function :C

totalOfPurchasedNested = totalOfPurchasedNested.map(v => v.replace(/\./g, "").replace(",", "."));

I intend to use this array formatted in a grouping and accounting linQ function of best products sell, example:

{product: "botijão 13KG", quantity: "10", totalPurchased: "220,00", totalDiscounts: "200,00"}
{product: "botijão 13KG", quantity: "4", totalPurchased: "208,00", totalDiscounts: "0,00"}
{product: "botijão 13KG", quantity: "13", totalPurchased: "666,00", totalDiscounts: "10,00"}

I'm going to clean up this final array, but this is what I want to be able to use in my map function in variables containing json so that I can format the money that is being stored and thanks for the help :D

{product: "botijão 13KG", quantity: 9, totalOfPurchasedNested: 26901.97},
{product: "botijão 25KG", quantity: 4, totalOfPurchasedNested: 4000.96}
2
  • when debugging you can see that you call .map of string "220,00", because your totalPurchased is string. map is available on arrays. Commented May 9, 2018 at 21:37
  • You should use reduce instead of map because with reduce you can map and filter in one function Commented May 9, 2018 at 21:40

2 Answers 2

1

If I'm understanding correctly you no longer need map since you're already running a loop through each object in the array.

Your code here is already there: totalOfPurchasedNested = totalOfPurchasedNested.replace(/\./g, "").replace(",", "."));

I forked your fiddle here: https://jsfiddle.net/vzy29zju/

Sign up to request clarification or add additional context in comments.

1 Comment

it's easier than I thought I thought I could not perform this action without a function, thx m8 :D
1

1- .map does not modify the array data if you want you can use .forEach

2- totalOfDiscountsPurchasedNested is not an array rather it's a string so you better use

 totalOfPurchasedNested = totalOfPurchasedNested.replace(/\./g, "").replace(",", ".");

See the updated fiddle

3 Comments

map does not modify the array data? What do you mean by that?
Array#map creates a new array of the created items and returns that. You need to return each item from the function. After you need to assign the returned result.
Yes Joseph I did, not need to start another function since I am already looping through each object in the array any way thanks for the suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.