最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - function to add item to beginning of an array without unshift method - Stack Overflow

programmeradmin1浏览0评论

I am trying to implement a function to add an item to the beginning of an array without using the unshift or splice methods, just array.length. Everything I do so far just overwrites what is already existing at index 0. Any help would be appreciated!

LetsUnshift = function(array) { 
 for (var i = array.length - 1; i >0; i--) {
    array[i +1] = array[i];
 }
 array[0] = item;
};

I am trying to implement a function to add an item to the beginning of an array without using the unshift or splice methods, just array.length. Everything I do so far just overwrites what is already existing at index 0. Any help would be appreciated!

LetsUnshift = function(array) { 
 for (var i = array.length - 1; i >0; i--) {
    array[i +1] = array[i];
 }
 array[0] = item;
};
Share Improve this question asked Nov 1, 2015 at 3:49 Frankie K.Frankie K. 771 silver badge4 bronze badges 1
  • this question is unclear.. what do you want to do? are you performing a book exercise? – webdeb Commented Nov 1, 2015 at 3:56
Add a ment  | 

5 Answers 5

Reset to default 2

Keeping the same reference, you could use ES 6's Array.prototype.copyWithin

var arr = ['b', 'c', 'd'],
    x = 'a';
++arr.length;
arr.copyWithin(1, 0);
arr[0] = x;

arr; // ["a", "b", "c", "d"]

You could reverse twice,

var arr = ['b', 'c', 'd'];
arr.reverse().push('a');
arr.reverse();

arr; // ["a", "b", "c", "d"]

You could re-push the whole thing,

var arr = ['b', 'c', 'd'],
    a2 = arr.slice();
arr.length = 0;
arr.push('a');
Array.prototype.push.apply(arr, a2);

arr; // ["a", "b", "c", "d"]

You could iterate with while, similar to your for

var arr = ['b', 'c', 'd'],
    i = arr.length;
while (i-- > 0)
    arr[i + 1] = arr[i];
arr[0] = 'a';

arr; // ["a", "b", "c", "d"]

If your goal is to create a new reference you could re-write either of those last two, e.g.

var arr = ['b', 'c', 'd'],
    a2 = ['a'];
Array.prototype.push.apply(a2, arr);

a2; // ["a", "b", "c", "d"]

If we're just code-golfing,

var arr = ['b', 'c', 'd'],
    a2 = Array.bind(null, 'a').apply(null, arr);

a2; // ["a", "b", "c", "d"]

This should work:

 var LetsUnshift = function(array, item) { 
      for (var i = array.length - 1; i >=0; i--) {
         array[i +1] = array[i];
      }
      array[0] = item;
 };

The issue with your code is the condition i>0, which prevents the first element from being shifted to the right.

letsUnshift = function(array, itemToAdd) { 
  return array.reduce(function(newArray, value){
    newArray.push(value);
    return newArray;
  }, [itemToAdd]);
};
letsUnshift([2,3,4], 1); //returns [1,2,3,4] and doesn't modify original array

Just called the function instead and start it from arr.length-1 down to >= 0 as suggested in another answer

var item = 15, i, arr = [11, 23, 33, 54, 17, 89];
LetsUnshift(arr);
function LetsUnshift(arr) {
    for (i = arr.length-1; i >= 0; --i) {
        arr[i + 1] = arr[i];
    }
    arr[0] = item;
};
console.log(arr);

//OUTPUT: [15, 11, 23, 33, 54, 17, 89]

const array=[1,2,3]
const target=3

function addElementAtStarting(array, target){
    return [target, ...array]
}
发布评论

评论列表(0)

  1. 暂无评论