1

So I'm pulling an array of collections from mongoDB and one of the documnets in the collection is a "price" document saved in the DB as a number (int32).

const hotAssets = await Asset.find({ hotAsset: true });

Now I feed the array to a function that converts the price documnet to String and add commas every 3 numbers. The weird thing is that the documnet just won't convert no matter what I do.

The function call

Utils.assetPriceToString(hotAssets);

The function

exports.assetPriceToString = (assetsArray) => {
  for (const asset of assetsArray) {
    if (asset.price) asset.price = asset.price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  }
};

example of a collection

{
    location: {
      coordinates: [Array],
      title: 'Central Park Burgas',
      type: 'Point'
    },
    _id: new ObjectId('66420dbd1ada8e8e709e30a7'),
    id: 3698,
    name: 'new flat',
    slug: 'new-flat',
    price: 120000,
    project: 'Central Park Burgas',
    city: 'Burgas',
    type: 'דירה',
    sm: '82 מ"ר',
    oceanView: '',
    rooms: '2',
    bathrooms: 1,
    terraces: '1',
    floor: '18 מ 20',
    readiness: '',
    serviceTax: '',
    description: 'some descriptin', 
    year: 2008,
    mainImage: '/img/asset1.jpg',
    images: [
      '/img/asset1.jpg',  '/img/asset2.jpg',
      '/img/asset3.jpg',  '/img/asset4.jpg',
      '/img/asset5.jpg',  '/img/asset6.jpg',
      '/img/asset7.jpg',  '/img/asset8.jpg',
      '/img/asset9.jpg',  '/img/asset10.jpg',
      '/img/asset11.jpg', '/img/asset12.jpg',
      '/img/asset13.jpg', '/img/asset14.jpg',
      '/img/asset15.jpg', '/img/asset16.jpg',
      '/img/asset17.jpg', '/img/asset18.jpg',
      '/img/asset19.jpg', '/img/asset20.jpg',
      '/img/asset21.jpg', '/img/asset22.jpg',
      '/img/asset23.jpg'
    ],
    hotAsset: true,
    updated_at: 2024-05-13T12:55:24.545Z,
    __v: 0,
    priceNis: 482027
  }

Iv'e tried hard coding another array with objects that have "price" variables and it worked fine.

Iv'e tried to convert using asset.price = String(asset.price) or asset.price = '' + asset.price but it did not convert.

This does not work as well. if i consol.log(new Intl.NumberFormat().format(asset.price)); it logs as a String, same as if I console.log(asset.price.toString()) but when I try to convert it does not work.

asset.price = new Intl.NumberFormat().format(asset.price); 

Please help.

Tried to convert number to string but it just won't convert.

4
  • Look into formatting the number? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 16, 2024 at 19:55
  • Based on your 'hard coding' comment, maybe there is an issue with your object. try to inspect the iteration of exports.assetPriceToString = (assetsArray) => { for (const asset of assetsArray) { // log / inspect asset variable if (asset.price) asset.price = asset.price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } }; Commented May 16, 2024 at 20:02
  • use let, rather than const for your loop? Commented May 16, 2024 at 20:03
  • 1
    Formatting Didn't work... tried with let, didn't work... for sure it's something with the object I just have no idea what. Tried to debug the iteration but I really don't see anything and have no idea what to look for deep inside the toString functions... Commented May 16, 2024 at 20:41

2 Answers 2

1

Instead of manually trying to format as a $x,xxx.xx format, use the built-in functions.

Define your formatting options and then format the number:

let n = 12000;
console.log( new Intl.NumberFormat('en-CA', {style: 'currency', currency: 'CAD'}).format(n));

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

1 Comment

Ok not working... asset.price = new Intl.NumberFormat().format(asset.price); -> asset.price still remains a number if I console.log it after this.
1

Your code works just fine for me. Try it in this code snippet:

function testOnClick( event ) {
  assetPriceToString(hotAssets);
}


let assetPriceToString = (assetsArray) => {
  for (const asset of assetsArray) {
    if (asset.price) {
      console.log( `Before: asset.price ${asset.price} is a ${typeof asset.price}`)
      asset.price = asset.price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      console.log( `After: asset.price ${asset.price} is a ${typeof asset.price}`)
    }
  }
}


let hotAssets = [
  {
    location: {
      coordinates: [Array],
      title: 'Central Park Burgas',
      type: 'Point'
    },
    // _id: new ObjectId('66420dbd1ada8e8e709e30a7'),
    _id: "new ObjectId('66420dbd1ada8e8e709e30a7')",
    id: 3698,
    name: 'new flat',
    slug: 'new-flat',
    price: 120000,
    project: 'Central Park Burgas',
    city: 'Burgas',
    type: 'דירה',
    sm: '82 מ"ר',
    oceanView: '',
    rooms: '2',
    bathrooms: 1,
    terraces: '1',
    floor: '18 מ 20',
    readiness: '',
    serviceTax: '',
    description: 'some descriptin',
    year: 2008,
    mainImage: '/img/asset1.jpg',
    images: [
      '/img/asset1.jpg',  '/img/asset2.jpg',
      '/img/asset3.jpg',  '/img/asset4.jpg',
      '/img/asset5.jpg',  '/img/asset6.jpg',
      '/img/asset7.jpg',  '/img/asset8.jpg',
      '/img/asset9.jpg',  '/img/asset10.jpg',
      '/img/asset11.jpg', '/img/asset12.jpg',
      '/img/asset13.jpg', '/img/asset14.jpg',
      '/img/asset15.jpg', '/img/asset16.jpg',
      '/img/asset17.jpg', '/img/asset18.jpg',
      '/img/asset19.jpg', '/img/asset20.jpg',
      '/img/asset21.jpg', '/img/asset22.jpg',
      '/img/asset23.jpg'
    ],
    hotAsset: true,
    // updated_at: 2024-05-13T12:55:24.545Z,
    updated_at: "2024-05-13T12:55:24.545Z",
    __v: 0,
    priceNis: 482027
  }
]
      <button type="button" onclick="testOnClick(event)">Test</button>

Comments

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.