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

javascript - How to add an element to an array without any built in functions - Stack Overflow

programmeradmin0浏览0评论

So I have these two questions considering the JavaScript language:

  1. Is there any way to append to an array without using the push() function or any other built in functions in the language?

  2. Is there any way to merge two arrays together without using the concat() function or any other built in functions in the language?

So I have these two questions considering the JavaScript language:

  1. Is there any way to append to an array without using the push() function or any other built in functions in the language?

  2. Is there any way to merge two arrays together without using the concat() function or any other built in functions in the language?

Share Improve this question edited Sep 2, 2014 at 14:39 Eiiki asked Sep 2, 2014 at 14:35 EiikiEiiki 3201 gold badge3 silver badges10 bronze badges 11
  • 3 1. What have you tried? 2. Why would you want to? – CodingIntrigue Commented Sep 2, 2014 at 14:37
  • 1 "I need to make an apple pie without using apples". Good luck with that. – Marc B Commented Sep 2, 2014 at 14:38
  • 1 JavaScript has an append() function? – j08691 Commented Sep 2, 2014 at 14:38
  • 3 The point of a challenge is to challenge yourself. It's not much of a challenge if you ask someone else for the answer. – cookie monster Commented Sep 2, 2014 at 14:43
  • 1 I just +1 this question because I don't see the point to -1 – Marecky Commented Nov 8, 2017 at 13:10
 |  Show 6 more comments

7 Answers 7

Reset to default 8

For the first part you can always use the length property of the array, to add the next element:

a = ['a', 'b', 'c', 'd'];
a[a.length] = 'e'; 

// a is now ["a", "b", "c", "d", "e"]

To do the latter, merge the arrays, without a function you can just loop thru the arrays, should pick the largest one to loop on. But yeah, as the comments state. There's usually not a good reason to do so.

Here are the alternatives for you:

  1. To add the item to the array without push call:

    arr[arr.length] = value;
    
  2. To concatenate one array to another without concat call:

    for (var i = 0; i < arr2.length; arr1[arr1.length] = arr2[i++]);
    

Not sure if this is what you are looking for or why but arr[arr.length] = 1; is the answer both of your questions.

var myArr = [];
myArr[myArr.length] = 1;
myArr[myArr.length] = 2;
myArr[myArr.length] = 3;

var myArr1 = [...]; // has items;
var myArr2 = [...]; // has items;
var mergedArr = [];

for(var i = 0; i < myArr1.length){
    mergedArr[mergedArr.length] = myArr1[i];
}

for(var i = 0; i < myArr2.length){
    mergedArr[mergedArr.length] = myArr2[i];
}

Yes, you can do so without using .push, concat, or .length. ES6 allows the use of the spread syntax (...):

var arr = [1, 2];  // arr is 1, 2
var arr = [...arr, 3];  // arr is now 1, 2, 3

let arr1 = [1, 2]; 
let arr2 = [3, 4];
arr = [...arr1, ...arr2]; // arr is now 1, 2, 3, 4
function pop(arr) {
  let finalVar = arr[arr.length - 1];
  arr.length = arr.length - 1;

  return finalVar;
}

I hope this can solve your problem:

const addItem = (arr, ...arguments) => {
  for (let i = 0; i < arguments.length; i++) {
    arr[arr.length] = arguments[i];
  }
  return arr;
};

let myArr = [];

console.log(addItem(myArr, 'Chaewon')); // ['Chaewon'])
console.log(addItem(myArr, 'Liz')); // ['Chaewon', 'Liz']);
<html lang="en">
<head>
<script>
    let data = [20, 80, 79, 21, 40];
    let newEl = 26;
    let position = 2;
    console.log(data);

 for(let i = data.length-1; i >= 0; i--){
    console.log(data[i]);
    if (i >= position) {
        data[i + 1]= data[i];
        if (i === position) {
            data[i]= newEl;
        }

    }
 }
 console.log(data)
</script>
</head>
<body>
    
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论