Ada array objek:

Saya perlu memeriksa apakah nilai suatu objek mengandung "$", lalu hapus

Saya telah mencoba:

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('$','');
                }
            });
        })

Tetapi mengembalikan Uncaught TypeError: ele.indexOf bukan fungsi

Objeknya bisa lain, yang penting $

-2
Anna 16 Maret 2021, 15:27

2 jawaban

Jawaban Terbaik

Kode Anda gagal karena beberapa nilai Anda bukan string, dan tidak memiliki metode indexOf. Periksa apakah nilainya adalah string, dan jika itu, periksa apakah itu termasuk tanda $ (atau gunakan indexOf).

Selain itu, Anda tidak dapat menetapkan nilai secara langsung (ele). Gunakan Object.entries() alih-alih Object.value() untuk mendapatkan kunci juga, dan tetapkan nilai baru ke 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)
1
Ori Drori 16 Maret 2021, 12:35

Anda mendapatkan kesalahan itu karena beberapa values bukan string dan karenanya tidak memiliki metode .indexOf(). Dan bahkan jika Anda memiliki pemeriksaan yang tepat untuk itu, Anda tidak memengaruhi objek asli dengan cara apa pun.

Berikut solusi untuk mengubah objek di tempat:

arr.forEach(function (obj) {
  Object.entries(obj).forEach(function ([key, val]) {
    if (typeof val == "string" && val.indexOf("$") > -1) {
      obj[key] = val.replace(/\$/g, "");
    }
  });
});
1
Abdellah Hariti 16 Maret 2021, 12:39