Javascript (dynamic) paste into an array and then shift all elements under +1

Really did not find a solution for Javascript. What I need; I want to insert an element into an array, but not overwrite this element. Rather, a "dynamic" insert. So the Insert element, then shift all the elements below it by +1 index.

For example:

I have an array "14S" "16S" "19S".
I know want to insert "15S".
The resulting array: "14S" "15S" "16S" "19S"

What I tried:

  fullName = "15S"
  low = 5;
  cardsS[low] = fullName;
  for (var i = low; i < cardsS.length; i++) {
      cardsS[i + 1] = cardsS[i];
  }
+4
source share
8 answers

If you know the position you want to insert an element:

Use the splice method . It is cheap and works the way you want. You can also insert multiple elements at once:

var strings = ["14S", "16S", "19S"];
strings.splice(1,0,"15S");

Result

"14S" "15S" "16S" "19S"

, , .

, :

push/sort, ( )

var strings = ["14S", "16S", "19S"];
strings.push("15S");
strings.sort(function(a, b){
    if (a is less than b by some ordering criterion)
        return -1;
    if (a is greater than b by the ordering criterion)
        return 1;
    // a must be equal to b
    return 0;
});
+11

Array.splice :

var arr = ["14S","16S","19S"];
arr.splice(1,0,"15S");
//         ^position after which to insert
//            ^number of elements to delete (none here)
//              ^value to insert ("15S" here)
// => arr is now ["14S","15S","16S","19S"]

, Array.indexOf :

var arr = ["14S","16S","19S"];
arr.splice((arr.indexOf('14S')>-1 && arr.indexOf(after)+1 || 0),0,"15S");
//         ^use indexOf result if applicable or just insert 
//          (so, if no position, this turns into unshift ;)

:

function arrayInsertAfter(array, after, value){
  after = array.indexOf(after)>-1 && array.indexOf('14S')+1 || 0;
  array.splice(after, 0, value);
  return array;
}
// usage
var arr = arrayInsertAfter(["14S","16S","19S"],"14S","15S");
// => ["14S","15S","16S","19S"]

MDN Array.splice

+4

push(), sort() :

var yourArray = ['14S', '16S', '19S'];

yourArray.push('15S');
yourArray.sort();
+3

Array.splice.

1.

arr.splice(1, 0, '155');

Fiddle

+2

, splice .

var arr = [];
 arr[0] = "14S";
 arr[1] = "16S";
 arr[2] = "19S";

 arr.splice(2, 0, "15S");
 console.log(arr.join());

The resulting array: 14S, 16S, 15S, 19S
+2

, [5], "fullName". , 4 , 0 3. , 4 .

, [i + 1], 6, .

, :

  • , (, )
  • + 1
  • , > < . > , , , .
  • , .

, Splice, javascript.

, !

+1

:

fullName ="15S"
cardsS = ["14S", "16S", "19S"];
for (var k in cardsS)
{
    if(parseInt(fullName) < parseInt(cardsS[k])){
        cardsS.splice(k,0,fullName)
        break;
    }
}

:)

, :

fullName ="15S"
cardsS = ["14S", "16S", "19S"];
for (var k = 0; cardsS.length > k; k++)
{
    if(parseInt(fullName) < parseInt(cardsS[k])){
       cardsS.splice(k,0,fullName)
        break;
    }

}

0

push + sort.

Splice / . :

var strings = ["14S", "16S", "19S"];
strings.splice(1,0,"15S");

"15S" 1. " , :"

push + sort. , . "15S" , () .

, . (, , ... sort()).

KooiInc . , . 15S 14S, 14S, . , 15S - , "", , .

, , , Push + sort, .

0
source

All Articles