简体   繁体   English

替换数组中特定字段的逗号?

[英]Replace comma from a specific field in an array?

I am trying to replace commas in a specific field in an array.我正在尝试替换数组中特定字段中的逗号。 For example I have below the following array which represents data collected from a csv file:例如,下面的数组表示从 csv 文件收集的数据:

[
    {
      "Month/Year": "2019-09",
      "group": "blue",
      "flights": "668,620"
    },
    {
        "Month/Year": "2019-10",
        "group": "blue",
        "flights": "662,520"
    },
    {
        "Month/Year": "2019-09",
        "group": "green",
        "flights": "662,520"
    },
    {
        "Month/Year": "2019-10",
        "group": "green",
        "flights": "678,520"
    }
]

Desired期望的

[
    {
      "Month/Year": "2019-09",
      "group": "blue",
      "flights": "668620"
    },
    {
        "Month/Year": "2019-10",
        "group": "blue",
        "flights": "662520"
    },
    {
        "Month/Year": "2019-09",
        "group": "green",
        "flights": "662520"
    },
    {
        "Month/Year": "2019-10",
        "group": "green",
        "flights": "678520"
    }
]

How can I run a cleaning operation on this JSON before I work with it?在使用此 JSON 之前,如何对其进行清理操作? I am thinking when the JSON is brought in I treat it by cleaning it and then I can work with it for graphing purposes.我在想,当 JSON 被带进来时,我通过清洁它来处理它,然后我可以使用它来绘制图形。

You can use array function map:您可以使用数组 function map:

 const data = [ { "Month/Year": "2019-09", "group": "blue", "flights": "668,620" }, { "Month/Year": "2019-10", "group": "blue", "flights": "662,520" }, { "Month/Year": "2019-09", "group": "green", "flights": "662,520" }, { "Month/Year": "2019-10", "group": "green", "flights": "678,520" } ] const result = data.map(item => {item.flights = item.flights.replace(",",""); return item}) console.log(result)

You can go with simple map and replace methods.您可以使用简单的 map 和替换方法来 go 和替换方法。

 let newArray = array.map(obj => { obj.flights = obj.flights.replace(/,/g, ''); return obj; })

Assuming you call this array arr , you can access each of the desired fields by using elem.flights (for each element of the array), and remove its comma by using replace(",", "") :假设您调用此数组arr ,您可以使用elem.flights访问每个所需字段(对于数组的每个元素),并使用replace(",", "")删除其逗号:

 arr = [ { "Month/Year": "2019-09", "group": "blue", "flights": "668,620" }, { "Month/Year": "2019-10", "group": "blue", "flights": "662,520" }, { "Month/Year": "2019-09", "group": "green", "flights": "662,520" }, { "Month/Year": "2019-10", "group": "green", "flights": "678,520" } ]; for(var elem of arr) { elem.flights = elem.flights.replace(",", ""); console.log(elem.flights); }

There are a few different ways to loop over an array in javascript.在 javascript 中有几种不同的方法可以遍历数组。 I like good old fashioned for loops.我喜欢老式的for循环。

var obj = [
{
  "Month/Year": "2019-09",
  "group": "blue",
  "flights": "668,620"
},
{
    "Month/Year": "2019-10",
    "group": "blue",
    "flights": "662,520"
},
{
    "Month/Year": "2019-09",
    "group": "green",
    "flights": "662,520"
},
{
    "Month/Year": "2019-10",
    "group": "green",
    "flights": "678,520"
}];

for(var i = 0; i < obj.length; i++)
{
    obj[i]['flights'] = obj[i]['flights'].split(',').join('');
}

Using .split(',') converts the string to an array and then .join('') converts it back to a string but w/oa separator.使用.split(',')将字符串转换为数组,然后.join('')将其转换回字符串,但没有分隔符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM