Reputation: 236
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);
}
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}
Upvotes: 2
Views: 498
Reputation: 4214
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/
Upvotes: 1
Reputation: 10148
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
Upvotes: 1