I have an array of words, and I want to search the words in text array , find them and check if next element or elements after word are numbers , push the number or numbers with word in new object and push object to new array. like this:
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]}];
But my current code just returns the numbers after word and push them in new array:
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]
So how can I push numbers with their keys to an array?
You still collect numbers in result array if you find a matching word. But you start with an empty array for every matching word.
You don't want to use the increment operator on i to go through text.
res is an Object in which you store matching words as keys and result arrays as values.
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)
You have not the object creation and pushing to main array properly. You are checking whether the nodes from text is there in array words, if thats found you are pushing the numbers after that found word to an array correclty. But you should not push that to a plain array. instead you should push it to an object having the key that you found as the match, it should be declared as empty array initially. There after numbers after that needs to be pushed to this array.
Please find the working fiddle for same
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 implementation of your requirement will be like this
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);
You need to push an object containing the word as a key and the numbers you want.
Something like
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)