javascript joining similar array

Viewed 52

const word = ["house", "mango", "window", "watermelon"]
const component = ["us", "man", "ous", "go", "water", "melon", "win", "dow"]

const output = [{
  "word": "house",
  "component": ["us", "ous"]
}, {
  "word": "mango",
  "component": ["man", "go"]
}, {
  "word": "window",
  "component": ["win", "dow"]
}, {
  "word": "watermelon",
  "component": ["water", "melon"]
}, ]

const answer = [];

for (let i = 0; i <= word.length - 1; i++) {
  answer.push(word[i]);
}

console.log(answer, output);

so I was practicing a tutorial where I was given two arrays and asked to group them together -- if a component includes a word I should group them together but I have been unable to move past the for loop and run an if check. Please can anybody help?

2 Answers

You could map the words with their found components.

const
    words = ["house", "mango", "window", "watermelon"],
    components = ["us", "man", "ous", "go", "water", "melon", "win", "dow"],
    output = words.map(word => ({
        word,
        components: components.filter(component => word.includes(component))
    }));

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you like to get it a classical way. you could iterate with a for ... of statement.

const
    words = ["house", "mango", "window", "watermelon"],
    components = ["us", "man", "ous", "go", "water", "melon", "win", "dow"],
    output = [];

for (const word of words) {
    const temp = { word, components: [] };
    for (const component of components) {
        if (word.includes(component)) temp.components.push(component);
    }
    output.push(temp);
}

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You need to break this down into logical steps that may help you figure out the solution,

Logically,

  1. Loop through word array
  2. For each item in the word array, loop through the component array to find items that make up the word

Finally, your output array should have 2 keys, word and component.

  1. Structure your output object when you are looping through both the objects as mentioned in steps 1 and 2 above.

Using tradition loops, you could achieve it like so,

const word = ["house", "mango", "window", "watermelon"]
const component = ["us", "man", "ous", "go", "water", "melon", "win", "dow"]

const output = [{
  "word": "house",
  "component": ["us", "ous"]
}, {
  "word": "mango",
  "component": ["man", "go"]
}, {
  "word": "window",
  "component": ["win", "dow"]
}, {
  "word": "watermelon",
  "component": ["water", "melon"]
}]

const answer = [];

for (let i = 0; i <= word.length - 1; i++) { // step 1
  let c = [];
  for (let j = 0; j <= component.length - 1; j++) { // step 2
    if (word[i].includes(component[j])) {
      c.push(component[j]);
    }
  }
  answer.push({ // step 3
    word: word[i],
    component: c
  })
}

console.log(answer, output);

Once you are comfortable with newer array methods, you could go with what @Nina Scholz has answered.

Related