0
我想返回我的箭頭函數的參數word
作爲對象鍵的屬性。但返回的對象改爲包含屬性"word"
。作爲對象屬性的箭頭函數返回參數
lines = ["first row","second row","third row"]
let newLines = lines.map((item, index)=> {
let wordsPerLines = item.split("\s");
return wordsPerLines.map(word => ({ word : index}))
}
);
console.log(newLines);
這是輸出:
[
[
{
"word": 0
},
{
"word": 0
}
],
[
{
"word": 1
},
{
"word": 1
}
],
[
{
"word": 2
}
]
]
我想有這個,而不是輸出:
[
[
{
"first": 0
},
{
"row": 0
}
],
[
{
"second": 1
},
{
"row": 1
}
],
[
{
"third": 2
},
{
"row": 2
}
]
]
雖然您的問題已關閉,但您在分割字符串時遇到問題。你正在把一個正則表達式作爲一個字符串。使用'.split('')'或'.split(/ \ s /)' – chazsolo
@chazsolo,你是對的,謝謝! – edkeveked