繁体   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