So I have these two questions considering the JavaScript language:
Is there any way to append to an array without using the push() function or any other built in functions in the language?
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:
Is there any way to append to an array without using the push() function or any other built in functions in the language?
Is there any way to merge two arrays together without using the concat() function or any other built in functions in the language?
- 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
7 Answers
Reset to default 8For 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:
To add the item to the array without
push
call:arr[arr.length] = value;
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>