簡體   English   中英

將具有多個值的對象和鍵推送到 for 循環中的數組

[英]Push object and keys with multiple value to array in for loop

我有一個單詞數組,我想搜索文本數組中的單詞,找到它們並檢查單詞之后的下一個或多個元素是否為數字,將新對象中帶有單詞的一個或多個數字推送到新數組中。 像這樣:

words = ['blue' , 'red' , 'yellow' , 'grin' , 'black' , 'white'];

text = ['xxx'  , 'yyy' , 'red' , 'zzz' , 'black' , 65 , 54 , 'white' , 'aaa' , 'yellow' , 50 , 'ppp'];


Output I want: 

[{'black' : [65 , 54] , 'yellow' : [50]}];  

但我當前的代碼只是返回單詞后的數字並將它們推送到新數組中:

words = ['blue' , 'red' , 'yellow' , 'grin' , 'black' , 'white'];

text = ['xxx'  , 'yyy' , 'red' , 'zzz' , 'black' , 65 , 54 , 'white' , 'aaa' , 'yellow' , 50, 'ppp'];

        const set = new Set(words);
        let result = [];
        for (let i = 0; i < text.length; i++) {
          if (set.has(text[i])) {
            while (typeof text[i + 1] == "number") {
              result.push(text[++i]);
            }
          }
        }

      console.log(result)

//output : [65 , 54 , 50]

那么如何將帶有鍵的數字推送到數組中呢?

如果找到匹配的單詞,您仍然會在result數組中收集數字。 但是您從每個匹配單詞的空數組開始。

您不想在i上使用增量運算符來瀏覽text

res是一個Object ,您可以在其中將匹配的單詞存儲為鍵並將result數組存儲為值。

 words = ['blue' , 'red' , 'yellow' , 'grin' , 'black' , 'white']; text = ['xxx' , 'yyy' , 'red' , 'zzz' , 'black' , 65 , 54 , 'white' , 'aaa' , 'yellow' , 50, 'ppp']; const set = new Set(words); let res = {}; for (let i = 0; i < text.length; i++) { if (set.has(text[i])) { let result = []; let j = i; while (typeof text[j + 1] == "number") { result.push(text[++j]); } if(result.length > 0) res[text[i]] = result; } } console.log(res)

您沒有正確創建對象並推送到主數組。 您正在檢查text的節點是否存在於數組words ,如果找到,則將找到的單詞之后的數字推送到數組正確性。 但是您不應該將其推送到普通數組。 相反,您應該將其推送到具有您找到的匹配項的對象,它最初應聲明為空數組。 之后的數字需要被推送到這個數組。

請找到相同的工作小提琴

 const words = ['blue', 'red', 'yellow', 'grin', 'black', 'white']; // const text = ['black' , 50 , 'black' , 600 , 10 , 'black' , 40]; const text = ['xxx', 'yyy', 'red', 'zzz', 'black', 65, 54, 'white', 'aaa', 'yellow', 50, 'ppp']; const set = new Set(words); const finalResult = []; for (let i = 0; i < text.length; i++) { if (set.has(text[i])) { const newObj = {}; const key = text[i]; const result = []; while (typeof text[i + 1] == "number") { result.push(text[++i]); } if (result.length > 0) { newObj[key] = result finalResult.push(newObj); } } } console.log(finalResult);

您的要求的Array.reduce實現將是這樣的

 const words = ['blue', 'red', 'yellow', 'grin', 'black', 'white']; // const text = ['black' , 50 , 'black' , 600 , 10 , 'black' , 40]; const text = ['xxx', 'yyy', 'red', 'zzz', 'black', 65, 54, 'white', 'aaa', 'yellow', 50, 'ppp']; const modifiedText = text.reduce((acc, curr, index) => { if (typeof text[index] === "string" && typeof text[index + 1] === "number" && words.includes(curr)) { const newArr = []; let startIndex = index + 1; while (text[startIndex] && typeof text[startIndex] === "number") { newArr.push(text[startIndex]); startIndex++; } if(newArr.length) { acc.push({[curr] : newArr }); } } return acc; }, []); console.log(modifiedText);

您需要推送一個包含單詞作為鍵和您想要的數字的對象。

就像是

words = ['blue' , 'red' , 'yellow' , 'grin' , 'black' , 'white'];

text = ['xxx'  , 'yyy' , 'red' , 'zzz' , 'black' , 65 , 54 , 'white' , 'aaa' , 'yellow' , 50, 'ppp'];

        const set = new Set(words);
        let result = [];
        for (let i = 0; i < text.length; i++) {
          if (set.has(text[i]) && typeof text[i + 1] == "number") {
            let key = text[i]
            let arr = []
            while (typeof text[i + 1] == "number") {
              arr.push(++i)
            }
            result.push({[key]: arr})
          }
        }

      console.log(result)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM