How to dynamically determine if an object’s value contains a character?

There is an array of objects:

I need to check if an object’s value contains “$”, then remove it

I have tried:

var arr = [
    {
        "Account ID": 329,
        "Avg Opportunity Days": 690,
        "Avg Revenue": "$11888801",
        "Create Date": 41648,
        "Factored Revenue": "$1188880",
        "FactoredProductRevenue": "$1188880",
        "Month": "Nov",
        "Month_Number": 11,
        "Opportunity Count": 1,
        "Opportunity Days": 690,
        "Opportunity ID": 428,
        "Opportunity Revenue": "$11888801",
        "Partner ID": 90,
        "Product ID": 2,
        "ProductRevenue": "$11888801",
        "Sales Stage ID": 1,
        "Tot Opportunity Days": 690,
        "Year": 2015

    },
    {
        "Account ID": 194,
        "Avg Opportunity Days": 396,
        "Avg Revenue": "$11706144",
        "Create Date": 41821,
        "Factored Revenue": "$1170614",
        "FactoredProductRevenue": "$1170614",
        "Month": "Aug",
        "Month_Number": 8,
        "Opportunity Count": 1,
        "Opportunity Days": 396,
        "Opportunity ID": 289,
        "Opportunity Revenue": "$11706144",
        "Partner ID": 8,
        "Product ID": 17,
        "ProductRevenue": "$11706144",
        "Sales Stage ID": 1,
        "Tot Opportunity Days": 396,
        "Year": 2015
    }
]

        arr.forEach(function(obj) {
            Object.values(obj).forEach(function(ele) {
                if (ele.indexOf('$') > -1) {
                    ele = ele.replace('$','');
                }
            });
        })

but it returns Uncaught TypeError: ele.indexOf is not a function

The object can be another, the important thing is $

Answer

Your code fails because some of your values are not strings, and don’t have indexOf method. Check if the value is a string, and if its, check if it includes the $ sign (or use indexOf).

In addition, you can’t assign directly to the value (ele). Use Object.entries() instead of Object.value() to get the keys as well, and assign the new value to obj[key].

const arr = [{"Account ID":329,"Avg Opportunity Days":690,"Avg Revenue":"$11888801","Create Date":41648,"Factored Revenue":"$1188880","FactoredProductRevenue":"$1188880","Month":"Nov","Month_Number":11,"Opportunity Count":1,"Opportunity Days":690,"Opportunity ID":428,"Opportunity Revenue":"$11888801","Partner ID":90,"Product ID":2,"ProductRevenue":"$11888801","Sales Stage ID":1,"Tot Opportunity Days":690,"Year":2015},{"Account ID":194,"Avg Opportunity Days":396,"Avg Revenue":"$11706144","Create Date":41821,"Factored Revenue":"$1170614","FactoredProductRevenue":"$1170614","Month":"Aug","Month_Number":8,"Opportunity Count":1,"Opportunity Days":396,"Opportunity ID":289,"Opportunity Revenue":"$11706144","Partner ID":8,"Product ID":17,"ProductRevenue":"$11706144","Sales Stage ID":1,"Tot Opportunity Days":396,"Year":2015}]

arr.forEach(obj => {
  Object.entries(obj).forEach(([key, value]) => {
    if (typeof value === 'string' && value.includes('$')) {
      obj[key] = value.replace('$', '');
    }
  });
})

console.log(arr)

Leave a Reply

Your email address will not be published. Required fields are marked *