私の矢印関数のパラメータ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