Extracting values from an array, to create another array

Hi !

Learning step by step to use and manipulate arrays...

So... this time, i finally made my way to get what i needed :crazy_face: :upside_down_face: :metal:
... and my question is : was there a more "straightforward" way to do ?

Starting point :

  • one matrix of 336 buttons
  • one 'small' array of index values (folder buttons ones)
  • wanted to create the big "tracks index" array, without copy/pasting/typing numbers...

Here, the solution i found :

var indexRep = [0,6,12,33,60,61,63,84,103,118,133,142,149,152,155,164,191,209,230,244,272,291,297,304,312,317]

// creating the complete index values array
var TousIndex = []
for (var i = 0; i < 336; i++)
{
 TousIndex.push(i)
}
console.log('tous les index : '+TousIndex)

// deleting all indexRep values found in the TousIndex array,
// to create a new array...
for (let i = 0; i < TousIndex.length; i++) 
  {
    for(let j = 0; j < indexRep.length;j++) 
     {
       if (indexRep[j] === TousIndex[i]) 
       {
        delete TousIndex[i]
        var indexPistes = TousIndex.flat()
       }
     }
  }
  
console.log('index pistes: '+indexPistes) // Tracks index array
console.log('index dossiers: '+indexRep) // Folders index array

So, again... i'm happy with this... but is there some more direct way to do ?...

Thank you

Hello Sylvian,

There are many different ways of achieving a goal in programming. Glad you solved it! This is my approach:

const indexRep = [0,6,12,33,60,61,63,84,103,118,133,142,149,152,155,164,191,209,230,244,272,291,297,304,312,317]

//creates an array, map e filter it chaining methods
const tousIndex = Array(335).fill(0) // creates an array with 335 "zeros"
    .map((el, i) => i) // changes each 0 to be the respective index
    .filter(i => !indexRep.includes(i)) //filter the previous array, excluding all indexes included in indexRep array

console.log(tousIndex);

Thank you very much !

I like your approach, that's what i was trying to achieve. I've spent days reading and testing Array - JavaScript | MDN (mozilla.org)...

push, pop, fill, find, splice, slice... sooooo many things... and as i can't say i'm ok with the very basics of arrays, objects... it quickly turns a brain-killer confusing trip :crazy_face: !

Your code helps a lot :+1:

1 Like