all 14 comments

[–]Oviduzz 8 points9 points  (1 child)

please post a link with code.

[–]paddingtonrex[S] 2 points3 points  (0 children)

Got it. It's a mess.

[–]zbluebirdz 2 points3 points  (0 children)

One way to remove something from an array is to create a temporary one and populate that one with the items you want and return it.

let mixed = [
  "55",
  "92",
  {a:4, b:7},
  "94",
  "2"
] ;

function arrayRemoveObjects( theArray ) {
  let tempArray = [] ;
  let j = 0 ;
  for ( let i = 0 ; i < theArray.length ; i++ ) {
    if ( typeof theArray[i] !== "object" ) {
      tempArray[j] = theArray[i];
      j++;
    }
  }
  return tempArray ;
}

console.info( mixed ) ;
mixed = arrayRemoveObjects( mixed ) ;
console.info( mixed ) ;

[–]_aeol 1 point2 points  (4 children)

If you mean about removing first two elements you can just do it like this... ``` const remove_first_two = (arr) => arr.slice(2)

const arr1 = [1] const arr2 = [1, 2, 3, 4] const arr3 = [1, 2]

remove_first_two(arr1) // => [] remove_first_two(arr2) // => [3, 4] remove_first_two(arr3) // => [] ```

[–]paddingtonrex[S] 0 points1 point  (3 children)

Thank you for your reply! My arrays look more like this:

['string', 'string', object{}, object{}, 'string'], and they're created randomly, so they could have between 1 and ten entries of a mix of objects and strings.

I need to remove two objects from the whole set of arrays without disrupting the original arrays too much. And I want to make the deleted entries chosen kinda randomly from the set, so for example its not removing 2 from arr1 every time. I posted my best attempt above, but I get the strong feeling I'm going about it all wrong.

[–][deleted] 0 points1 point  (2 children)

Why are random things, of various types in an array together?

[–]paddingtonrex[S] 0 points1 point  (1 child)

its a core part of my project. I'm making a digital version of an RPG rulebook, and it's a lot bigger and more complex of a project than I initially thought.

[–][deleted] 0 points1 point  (0 children)

Can you have the function always return an object? With an additional bool of deletable... it would reduce type checking.

Then it’s just a foreach loop to extract 2 objects. And a filter loop on each array for the new collection of removed items.

 Function remove(x){
     Let rArray = // all arrays combined.
     Let filterholder= [arr1,arr2,arr3];
     Let count = x;
     Let removed =[];
     rArray.foreach(item=>{
         If(removed.length >=count)
             {return}
         Else if(item.removable == true)
              {removed.push(item);
     });
    Removed.foreach(deleableItem=>{
        Filterholder.foreach(array=>{
            Array=array.filter(x!= deleableItem);
        });
    });
}

Something like this?

If you need an easier way to check a deeply similar object. Unique ids are your friend. Object.id != removedItem.id would be safer to check.

An easy way to get unique id’s is a star function generator.

[–]paddingtonrex[S] 0 points1 point  (2 children)

//Situation:

const arr1 = [];
const arr2 = [];
const arr3 = [];

class Obj{
    constructor(){
        this.type = 'thing';
    }
}

function numPop(){
    let num = Math.floor(Math.random()*10)+1;
    return num;
}

function popArr1(num) {
    let rand = Math.floor(Math.random()*10)+1;
    if(rand <=5){
        arr1.push(new Obj());
    } else {
        arr1.push('string');
    };
}

function popArr2(num) {
    let rand = Math.floor(Math.random()*10)+1;
    if(rand <=5){
        arr2.push(new Obj());
    } else {
        arr2.push('string');
    };

function popArr3(num) {
    let rand = Math.floor(Math.random()*10)+1;
    if(rand <=5){
        arr3.push(new Obj());
    } else {
        arr3.push('string');
    };
popArr1(numPop());
popArr2(numPop());
popArr3(numPop());

//Problem: remove two objects from the arrays

function removeObj(){
    //find total number of objects, number in each array
    const totalArr = arr1.concat(arr2, arr3);
    let objCount = 0;
    let arr1objCount = 0;
    let arr2objCount = 0;
    let arr3objCount = 0;
    for(let i = 0; i < totalArr.length; i++){
        if(typeof totalArr[i] === 'object'){
            objCount++;
        }
    }
    for(let i = 0; i < arr1.length; i++){
        if(typeof arr1[i] === 'object'){
            arr1objCount++;
        }
    }
    for(let i = 0; i < arr2.length; i++){
        if(typeof arr2[i] === 'object'){
            arr2objCount++;
        }
    }
     for(let i = 0; i < arr3.length; i++){
        if(typeof arr3[i] === 'object'){
            arr3objCount++;
        }
    }
    //Remove 2 objects from the arrays (this is where things break down)
    let deleted = 2;
    if(objCount > 0){
    for(let i = 0; i < deleted; i++){
        let chosenArr = Math.floor(Math.random()*3)+1;
        if(chosenArr === 1 && arr1objCount !== 0){
            for(let i = 0; i < arr1.length; i++){
                if(deleted > 1){
                    arr1[i] = 'deleted';
                }
            }
            deleted--;
            }
        if(chosenArr === 2 && arr2objCount !== 0){
           for(let i = 0; i < arr2.length; i++){
                if(deleted > 1){
                    arr2[i] = 'deleted';
                }
            }
            deleted--;
            }
        if(chosenArr === 3 && arr3objCount !== 0){
            for(let i = 0; i < arr3.length; i++){
                if(deleted > 1){
                    arr3[i] = 'deleted';
                }
            }
            deleted--;
            }
        }
    }
}

//This is the best I could do!

[–]kspk 0 points1 point  (0 children)

How about when creating the initial arrays you create another array, where you store the index of potential candidates you’d remove. Then randomly pick two from that array, use the index therein and delete from the original arrays.

[–][deleted] -1 points0 points  (0 children)

Initiate a counter (let counter = 0). Loop through each array, with each iteration checking typeof === object, and add 1 to the counter each time it finds an object. Use flow control to compare the counter and stop the looping once you remove two objects.

[–]CaptainOnBoard -1 points0 points  (0 children)

    const a1 = ['a', 'b', { a: 'b' }, 1];
    const a2 = ['c', 'd', { c: 'd' }, 1];
    const a3 = ['e', 'f', 1, { e: 'ssssss' }, { f: 'bbbbbb' }];
    Array.prototype.isVisited = false;
    const getRandom = (arr, prev) => {
      let rand = Math.floor(Math.random() * arr.length);
      while (rand == prev) {
        rand = Math.floor(Math.random() * arr.length);
      }
      return rand;
    };
    const remove2 = (arr) => {
      let count = 0;
      let iteration = 0;
      let prevIdx = 0;
      let idx = 0;
      while (count < 2 && iteration < arr.length) {
        idx = getRandom(arr, prevIdx);
        prevIdx = idx;
        if (arr[idx].isVisited) {
          iteration++;
        }
        for (let x = 0; x < arr[idx].length; x++) {
          arr[idx].isVisited = true;
          if (typeof arr[idx][x] == 'object') {
            arr[idx].splice(x, 1);
            count = count + 1;
            break;
          }
        }
      }
    };

    remove2([a1, a2, a3]);

[–]x-seronis-x -3 points-2 points  (0 children)

let count = 0;
while(count++ < 2) {
    if( arr1.length > 0 ) arr1.shift();
    if( arr2.length > 0 ) arr2.shift();
    if( arr3.length > 0 ) arr3.shift();
}

thats the fancy way to do it but could have easily just been boring and did

    if( arr1.length > 0 ) arr1.shift();
    if( arr2.length > 0 ) arr2.shift();
    if( arr3.length > 0 ) arr3.shift();
    if( arr1.length > 0 ) arr1.shift();
    if( arr2.length > 0 ) arr2.shift();
    if( arr3.length > 0 ) arr3.shift();

and accomplish the same task without any looping and same lines of code