Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Push object and keys with multiple value to array in for loop

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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!