从javascript数组内的对象中删除字符

Removing characters from objects inside a javascript array?

本文关键字:删除 字符 对象 javascript 数组      更新时间:2023-09-26

从我的数据中删除双引号字符有一些麻烦。我知道这个问题被问了很多次,但我找不到一个有用的答案…

我有一个包含对象的数据数组。每个对象都有这样的字符串(取自Chrome DevTools):

0: Object
coID: 1
country: "ireland"
email: "email"
history: "history line 1<br>history line 2<br>history line 3<br>history line 4"
name: "My Company"
phone: "0872780000"
1: Object
coID: 2
country: ""ireland""
email: ""email@mail.com""
history: ""history line 1"<br>"history line 2"<br>"history line 3"<br>"history line 4""
name: ""Other Company""
phone: ""0872780000""

如你所见,第二个对象到处都有双引号。为了删除它们,我试着这样做:

// replace any instances of the quotes character.
for(m=0; m<listOfCompanies.length; m++) { 
    for(n=0; n<listOfCompanies[m].length; n++) {
        dataIn[m][n].replace(/'"/g,'');
    }
}

它不做任何事情,并且引号在第二个对象中保持不变。

还有这个(来自Matt S):

// replace any instances of the quotes character.
for(m=0; m<listOfCompanies.length; m++) { 
    for(n=0; n<listOfCompanies[m].length; n++) {
        dataIn[m][n] = dataIn[m][n].replace(/'"/g,'');
    }
}

我能对这种类型的数据使用这个方法吗?还是我做错了什么?

replace返回更新后的字符串。它不会替换inline。

dataIn[m][n] = dataIn[m][n].replace(/'"/g,'');