我有一個長字串,我想把那個長字串分成 128 個單詞的陣列對。例如。一個字串有 500 個單詞。那么最終輸出應該是一個包含 3 個元素的陣列
元素有前 128 個詞,第二個元素有接下來的 128 個詞,第三個元素應該有其余的詞。
我嘗試了很多方法,但沒有為我作業。如果有人知道,請幫助我
提前致謝。
uj5u.com熱心網友回復:
如果您希望每個陣列包含 128 個單詞而不是字符,您可以先用空格分割字串以將單詞存盤在陣列中,然后將其分割成如下所示的塊:
const string = "hello world hello world hello world "
const words = string.split(" ")
function sliceIntoChunks(arr, chunkSize) {
const noEmptyStrsArr = arr.filter(item => item.length > 0)
const res = [];
for (let i = 0; i < noEmptyStrsArr.length; i = chunkSize) {
const chunk = noEmptyStrsArr.slice(i, i chunkSize);
res.push(chunk);
}
return res;
}
console.log(sliceIntoChunks(words, 2))
uj5u.com熱心網友回復:
謝謝,@jessica-98 為您提供解決方案。如果有人想要 128 個單詞作為字串而不是單獨的單詞,我已經添加了幾行。那么你可以考慮這個解決方案。
const string = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum,\"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of "
const words = string.split(" ")
function sliceIntoChunks(arr, chunkSize) {
const noEmptyStrsArr = arr.filter(item => item.length > 0)
const res = [];
for (let i = 0; i < noEmptyStrsArr.length; i = chunkSize) {
const chunk = noEmptyStrsArr.slice(i, i chunkSize);
res.push(chunk);
}
return res;
}
var res=sliceIntoChunks(words, 128)
var newArr=[]
for(let i=0;i<res.length;i ){
/* for(let j=0;j<res[i].length;j ){
newArr.push()
}*/
newArr.push(res[i].join(" "))
}
console.log(newArr)
uj5u.com熱心網友回復:
reduce()您可以使用,slice()和來做更短的方法flatMap():
const longStr = [...Array(500)].reduce((acc, curr) => acc ? `${acc} random` : "random", ''); //Generate a long string with 500 words
const words = longStr.split(" ");
const ans = [...Array(Math.ceil(words.length/128))].reduce((acc, _, i)=> [...acc, words.slice(i*128, (i*128) 128)],[]).flatMap((subArr) => subArr.join(" "));
console.log(ans);
uj5u.com熱心網友回復:
由于這里已經有可行的答案,我將分享我的遞回方法。但在未來,請分享您自己所做的作業。如果您無法撰寫任何代碼,請分享您的一些思考程序。
在這里,我們在一個可重用函式的基礎上構建它,chunk它將任何陣列分成相等大小的塊,加上一個可能更小的最終塊。我們的chunkWords函式在空白處拆分字串,然后chunk在結果上運行。
const chunk = (n) => (xs) =>
xs .length <= n ? [xs] : [xs .slice (0, n), ... chunk (n) (xs .slice (n))]
const chunkWords = (n) => (s) =>
chunk (n) (s .split (/\s /)) // .map (ss => ss .join (' '))
console .log (
chunkWords (3) ('Now is the time for all good men to come to the aid of their party')
)
如果您希望將結果轉換回字串,只需取消注釋.map. chunkWords那將產生["Now is the", "time for all", "good men to", "come to the", "aid of their", "party"]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/529166.html
上一篇:發現物體之間的關系
下一篇:A*搜索演算法示例
