3 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
不要使用正則表達(dá)式,.map而是使用:
var array= ["value1", undefined, "value3"];
const newArr = array.map(item => item === undefined ? 'N/A' : item);
console.log(newArr);

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊
不要使用正則表達(dá)式,您可以使用映射或您自己的實(shí)現(xiàn),本例使用:
for(let it in array) if(array[it] == undefined || array[it] == "") array[it] = "N/A";

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
更好的方法是使用Array.prototype.forEach而不是 map,因?yàn)樘鎿Q發(fā)生在適當(dāng)?shù)奈恢谩?/p>
var array = ["value1", undefined, "value3"];
array.forEach((element, index) => {
? ? if (element === undefined) {
? ? ? ? array[index] = "N/A";
? ? }
});
console.log(array);
添加回答
舉報(bào)