JS迭代对象数组并替换string中的值

时间:2018-04-18 22:33:29

标签: javascript

我正在使用一系列javascript对象。密钥是动态生成的,并且会有未知数量的密钥。

我试图遍历对象并替换其值中的一段数据。

以下是一个例子:

        if (source == buttonRun) {
        timer = new Timer(1000, this);
        timer.start();
        System.out.println("The program's timer has started");
        if (Maze == Maze1) {

            for (int i=0;i<2;i++) {
            moveLeft();
            moveLeft();
            moveLeft();
            //2nd section//
            moveDown();
            moveDown();
            moveDown();
            //3rd section//
            moveLeft();
            moveLeft();
            //4th section
            moveDown();
            moveDown();
            moveDown();
            //5th section//
            moveRight();
            moveRight();
            //6th section//
            moveDown();
            moveDown();
            moveDown();
            //7th section//
            moveRight();
            moveRight();
            moveRight();
            //8th section//
            moveDown();
            moveDown();
            moveDown();
            //9th section//
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
        }
    }
    }

在对象中,我正在尝试将var obj = [{ name: 'Joe', age: 21, randomCol1: 'Blah<br>Blah<br>Blah', randomCol2: 'Blah<br>Blah<br>Blah', randomCol3: 'Blah<br>Blah<br>Blah' }, { name: 'Bob', age: 25, randomCol1: 'Blah<br>Blah<br>Blah', randomCol2: 'Blah<br>Blah<br>Blah', randomCol3: 'Blah<br>Blah<br>Blah' }] for (var key in obj) { if (obj.hasOwnProperty(key)) { obj[key].replace(/<br>/g,'\r\n') } } 替换为<br>。列名称并不总是已知。

我应该通过循环或某种类型的映射来做到这一点吗?只需在将数据传递给excel导出之前需要转换这些数据,我需要利用换行符。

4 个答案:

答案 0 :(得分:1)

  

我应该通过循环或某种类型的映射来做到这一点吗?

是的,你确实需要一个循环。一种方法是使用两者的组合 -

  • 一个map用于循环遍历数组并返回新数组
  • 地图中的for循环以遍历对象中的所有属性

工作代码段

&#13;
&#13;
var objArray = [{
    name: 'Joe',
    age: 21,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
  },
  {
    name: 'Bob',
    age: 25,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
  }
];

var newObjArray = objArray.map(function(obj) { // loop through array

  for (var key in obj) { // loop through object properties

    if (typeof obj[key] === 'string') { // only if the value is a string type

      obj[key] = obj[key].replace(/<br>/g, '\r\n'); // same logic as you were using before

    }

  }

  return obj;
});

console.log(newObjArray);
&#13;
&#13;
&#13;

注意:如果您不想更改原始输入数组而是生成新的结果数组,请使用map。否则,请使用forEach

答案 1 :(得分:0)

您的代码存在一些问题,..

首先,您没有遍历数组。名称obj在这里略有误导,因为你有一个数组。

您还需要在调用替换之前检查类型,例如。年龄是一个数字,它没有替换方法。

最后当你打电话给替换时,你需要存储结果,即使它在同一个地方。

所以考虑到所有这些因素,如果您修改代码以执行我认为您的操作,请在下面进行考虑。

&#13;
&#13;
var obj = [{
    name: 'Joe',
    age: 21,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
},
{
    name: 'Bob',
    age: 25,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
}]


for (var i = 0; i < obj.length; i += 1) {
  var obj2 = obj[i];
  for (var key in obj2) {
      if (obj2.hasOwnProperty(key)) {
         if (typeof obj2[key] === "string")
            obj2[key] = obj2[key].replace(/<br>/g,'\r\n')
      }
  }
}
  
console.log(obj);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

循环浏览对象并使用.replace,只要它是string

&#13;
&#13;
const obj = [{
    name: 'Joe',
    age: 21,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
  },
  {
    name: 'Bob',
    age: 25,
    randomCol1: 'Blah<br>Blah<br>Blah',
    randomCol2: 'Blah<br>Blah<br>Blah',
    randomCol3: 'Blah<br>Blah<br>Blah'
  }
]


obj.forEach((e) => {

  for (let k in e)
    (e[k] && typeof e[k] == 'string') && (e[k] = e[k].replace(/<br>/g, '\n\r'));

});


console.log(obj)
&#13;
&#13;
&#13;

答案 3 :(得分:0)

for或while循环总是会更快。但是根据数组的结构和大小,我只会这样做:

obj.forEach(function(d,i){
    Object.keys(d).forEach(function(dd,ii){
        typeof d[dd] === "string" && (d[dd] = d[dd].replace(/<br>/g,'\r\n'))
    })
})