Lionel R
Lionel R

Reputation: 66

Push value in empty index in array

i want to push à value in array , but not at the end, not at the start, at the first empty index

Show the code exemple:

var test=new Array();
test[0]="Lionel";
test[2]="Jhon";
test.push("Cloé");

result:

[ 'Lionel', <1 empty item>, 'Jhon', 'Cloé' ]

And i need to have Cloé just after Lionel Thanks

Edit this is different to insert in specific index, because i can't know the number of empty index, this is slot system . And i just wanna know if we have native solution ?.

Upvotes: 1

Views: 3870

Answers (6)

Nina Scholz
Nina Scholz

Reputation: 386560

You could search for a sparse index and return this index for inserting a value.

function getIndex(array) {
    var last = 0;
    array.some(function (_, i) {
        return last < i || !++last;
    });
    return last;
}

var test = ["Lionel", , "Jhon"];

test[getIndex(test)] = "Cloé";

console.log(test);

Upvotes: 2

Lionel R
Lionel R

Reputation: 66

Ok, we don't have native solution, this is my solution with code, my final objective is slot systeme :

 FindValidSocket(Type){
    let count=0;
    while (count!==this.Max[Type]){
        if(typeof(this.traitement[Type][count])==="undefined"){
            return count;
        }else{
            count++;
        }
    }
    return false;
}

Thanks all for your response

Upvotes: 0

user8579087
user8579087

Reputation:

Solution 1

If you know which index you want to apply the value to, use test[x] = "Cloe".

Solution 2

If you want to apply the value to the first empty item, use a for loop that goes through every item in the array until it finds one that's empty.

test = ["Lionel", "", "Jhon", ""];

for (var i = 0; i < test.length; i++) {
  if (!test[i]) {
    test[i] = "Cloe";
    break;
  }
}

console.log(test)

EDIT:

If you only want to use native functions. Use splice and indexOf.

test = ["Lionel", "", "Jhon", ""];

test.splice(test.indexOf(""), 1, "Cloe")

console.log(test)

test.splice(index, 1, item) inserts item into test at the specified index. indexOf("") search for an array with no value in it and return its position.

Upvotes: 0

marvel308
marvel308

Reputation: 10458

you can do it in the following way

var test=new Array();
test[0]="Lionel";
test[2]="Jhon";

let x = test.length;
Object.keys(test).forEach(function(element, idx){
    if(element != idx){
        x = (x==test.length ? idx: x);
    }
})
test[x] = "Cloe"
console.log(test);

Upvotes: 0

RaV
RaV

Reputation: 1048

function insertIntoFirstEmpty(val, arr) {
   for(var i = 0;i<arr.length;i++) {
      if(typeof arr[i] === 'undefined')
      {
         arr[i] = val;
         return;
      }
   }
   arr.push(val);
}

Upvotes: -1

Jamiec
Jamiec

Reputation: 136094

You could write a simple function to look for a gap with undefined as the value and place the new value if if found - otherwise just push to the end of the array

function insertFirstGap(array, value){
    for(var i = 0;i<array.length;i++){
        if(array[i] === undefined){
            array[i] = value; 
            return; 
        }
    }
    array.push(value);

}

Live example below:

var test=new Array();
test[0]="Lionel";
test[2]="Jhon";

insertFirstGap(test,"Cloé");

console.log(test, test.length);

function insertFirstGap(array, value){
    for(var i = 0;i<array.length;i++){
        if(array[i] === undefined){
            array[i] = value;  
            return;
        }
    }
    array.push(value);
     
}

Upvotes: 1

Related Questions