Home > Mobile >  Split array based on element contents?
Split array based on element contents?

Time:03-31

Let's say my elements look like this:

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose',
  ...
]

What I'd like is to split this into chunks where 'goose' is the final element in a chunk:

const words = [
 [
   'duck foo bar',
   'duck',
   'duck bing',
   'bing',
   'Bloop#12 goose',
 ], 
 [
   'duck 12',
   'duck goose',
 ], 
 [
  ...
 ],
];

There's no regularity to how many elements precede a 'goose', nor what is in each element except that 1) goose is always the last part of an element, and 2) goose never appears in any other element besides the one I want a chunk to end on (i.e. I never get 'goose foo', but I might get 'duck goose')

CodePudding user response:

You can archive this with a simple forEach loop. In my example below i store the values first temporally. the i push the chunk it to the new array if the word contains goose.

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose',
 'abc'
]

const n = []
let tmp = [];
words.forEach((w) => {
  if (! w.includes('goose')) {
    tmp.push(w);
  } else {
    tmp.push(w);
    n.push(tmp);
    tmp = [];
  } 
  
});

console.log(n)

CodePudding user response:

Here's the first solution that came up in my mind.

We keep pushing elements to temp array until we get a string containing the word goose.

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose',
];

const splits = [];
let temp = [];

words.forEach(word => {
 if(word.includes('goose')) {
    temp.push(word);
  splits.push(temp);
  temp = [];
 } else {
    temp.push(word);
 }
})

console.log(splits)

CodePudding user response:

This would also work.

const words = [
  "duck foo bar",
  "duck",
  "duck bing ",
  "bing",
  "Bloop#12 goose",
  "duck 12",
  "duck goose",
];

const output = [];

let chunk = [];
words.forEach((item) => {
  const splits = item.split(" ");
  const last =  splits[splits.length - 1];
  if(last === "goose") {
    chunk.push(item)
    output.push(chunk)
    chunk = [];
  } else {
    chunk.push(item)
  }
})

console.log(output);

CodePudding user response:

Try this. This should do what you're looking for.

const words = [
 'duck foo bar',
 'duck',
 'duck bing ',
 'bing',
 'Bloop#12 goose',
 'duck 12',
 'duck goose'
]

const answer = []
let temp = []
for(let i = 0; i < words.length; i  ){
  if(words[i].includes('goose')){
    temp.push(words[i])
    answer.push(temp)
    temp = []
  } else{
    temp.push(words[i])
  }
}

console.log(answer)

CodePudding user response:

You insert the items to the array like so:

const words = [
  "duck foo bar",
  "duck",
  "duck bing ",
  "bing",
  "Bloop#12 goose",
  "duck 12",
  "duck goose"
];
let result = [];
words.forEach(function(word) {
  // insert a new array if
  // we're inserting for the first time or 
  // last item in last array is a goose
  if (result.length === 0 || /goose$/.test(result.slice(-1).pop().slice(-1).pop())) {
    result.push([]);
  }
  result[result.length - 1].push(word);
});
console.log(result);

  • Related