简体   繁体   中英

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 ? .

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); 

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); } 

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); 

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM