I have an array of 5 elements ex. arr = [1,2,3,4,5]; I want to add elements between these elements, and place them in new array arr1 = [1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5,0,0,0];. When i alert arr1 i get the same array as it is, but when i alert arr1.length i get that it`s length is 5, when it is actually 20. Can you help me fix this, or tell me why do i get that result. Here is an example of the code i am using:
function niza(val,times){
var arr = [];
for (var i=0;i<times;i++) {
arr.push(val);
}
return arr;
}
and then this:
var y1=0;
var arr= [];
var a = new Array();
for (var j=0;j<Niza1.length;j++) {
y1 = Niza1[j];
arr = y1 + "," + niza(0,11);
a.push(arr);
}
where Niza1 holds the 5 elements mentioned before in arr, and a holds the elements mentioned in arr1.
I'm not sure to understand the code you wrote, but do you know you can add multiple elements at once with arr.push ?
var array1 = [1, 2, 3, 4, 5];
var array2 = [];
for(var i = 0 ; i < array1.length ; i++) {
array2.push(array1[i], 0, 0, 0);
}
//array2 == [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]
You question is a little hard to follow, but try something like this:
function inject(original, val, times) {
var res = [];
for(var i=0; i < original.length; i++){
res.push(original[i]);
for(var j = 0; j < times; j++){
res.push(val);
}
}
return res;
}
Demonstration
Here's a solution that would work with an arbitrary list and length, and is therefore reusable:
function inject(original, values) {
var result = [];
for (var i = 0 ; i < original.length ; i++) {
result.push(original[i]);
result.push.apply(result, values);
}
return result;
}
console.log(inject([1,2,3], [0,0,0]));
// output: [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]
console.log(inject([1,1,1], [2,6,2,6]));
// output: [1, 2, 6, 2, 6, 1, 2, 6, 2, 6, 1, 2, 6, 2, 6]
It leverages the the native apply (here's an explanation) function to execute the push with an arbitrary list of arguments, defined by the values array.
Related
I have an array containing numbers, in that array there're some numbers that occur many times in consecutive order one after the other, and I want to compress those repeated numbers into a very specific format 'Number*Times' to reduce the size of the array:
input: [0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0]
---------^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^--------
output: [0, 1, 2,'0x3', 3, 2, '0x6', 5, 6, 0]
let array = [0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0];
let string = array.toString();
let string_compressed = string.replace(/(\d+,)(\1)+/g, (x) => {
return "Number*" + x.split(",").length + ",";
});
let array_compressed = string_compressed
.split(",")
.map((x) => (isNaN(Number(x)) ? x : Number(x)));
console.log(array_compressed); //[0, 1, 2, 'Number*4', 3, 2, 'Number*7', 5, 6, 0]
I don't know how to get the number that repeated so I put Number instead!
I used regex to solve it, I know if you think to solve problem with regex, they become two problems!
BUT Guys I'm sure this isn't the efficient way to solve this problem, And there're other ways to solve it!
what do you suggest if you want to solve this problem?
Because your regex to find how many numbers repeat already only matches numbers that are in consecutive order in your array, you can simply just take the first index of the x.split(",") array and return that.
Edit:
Also, as #qrsngky out, your x.split(",").length wasn't actually the right length, because when you split it by comma, there is a null character at the end:
let array = [0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0];
let string = array.toString();
let string_compressed = string.replace(/(\d+,)(\1)+/g, (x) => {
console.log(x.split(","));
return "Number*" + x.split(",").length + ",";
});
let array_compressed = string_compressed
.split(",")
.map((x) => (isNaN(Number(x)) ? x : Number(x)));
console.log(array_compressed);
Sorry for missing that, and props to the comments! I just fixed it by subtracting one from the length.
Edit 2:
For edge cases, we can just add a comma and use slice.
I attached the complete fixed code snippet below:
let array = [0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0, 0, 0];
let string = array.toString() + ",";
let string_compressed = string.replace(/(\d+,)(\1)+/g, (x) => {
return x.split(",")[0] + "*" + (x.split(",").length - 1) + ",";
});
let array_compressed = string_compressed
.slice(0, -1)
.split(",")
.map((x) => (isNaN(Number(x)) ? x : Number(x)));
console.log(array_compressed);
Assume your original array consists of none other than numbers.
Non-regex approach: based on a for loop and counting how many repetitions had been encountered.
let array = [0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0]
let buffer = [];
let currentNum = array[0], count = 1;
for (let i = 1; i < array.length; i++) {
if (array[i] === currentNum) {
++count;
} else {
buffer.push( count === 1 ? currentNum : (currentNum + 'x' + count) );
currentNum = array[i];
count = 1;
}
}
//don't forget the last number
if(currentNum !== undefined) buffer.push( count === 1 ? currentNum : (currentNum + 'x' + count) );
console.log(buffer);
The if(currentNum !== undefined) check is only useful in case it's an empty array.
Another example of how not to do it with string manipulation by coding what you want done:
function packArray(array) {
var packed = [];
for( var i = 0; i < array.length; i=j) {
var entry = array[i];
for(var j = i+1; array[j] === entry && j<array.length; ++j);
packed.push( j > i+1 ? `${entry}x${j-i}` : entry);
}
return packed;
}
console.log( packArray([0, 1, 2, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 5, 6, 0]))
You could substitute let for var except for var j which should remain the same to allow access to j outside the nested for loop.
let linkMatrix = [
[0,0,1,0],
[1,0,0,1],
[1,1,0,1],
[0,1,0,0]
];
let newMatrix = [];
function linkToPage(){
for(let j = 0; j < linkMatrix.length; j--){
newMatrix = linkMatrix.splice(linkMatrix[j], 1);
console.log(linkMatrix + " Here is linkMatrix");
for(let i = 0; i < newMatrix.length; i++){
newMatrix.splice(newMatrix[i]);
console.log(newMatrix + " Here is newMatrix");
}
}
**What i'm trying to do is loop throug the first array but remove the fist array, because i don't need to loop throug that, then loop throug the rest of the arrays, but the only value i need is the value in the removed array's index, so to better understand: if we had an array that wore somthing like this arr = [[0,1],[1,0],[1,1]] then remove [0,1] and because it is arr[0], i would like to loop throug the 0 index of the other arrays so i would get 1 and 1, then go back to the original array remove arr[1] and loop throug arr[0],arr[2] to the 1 index of the arrays so i would get [1,1] **
**Yeah, so the wanted result from my link matrix would be:
[0,0,1,0] = 2
[1,0,0,1] = 2
[1,1,0,1] = 1
[0,1,0,0] = 2
because there is 2 other arrys pointing to the first array, the same for the second and fourth, but there is only one array pointing to the third array
**
You could add the values of the columns.
const
getColSum = matrix => matrix.reduce((r, a) => a.map((v, i) => (r[i] || 0) + v), []);
console.log(...getColSum([[0, 0, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1], [0, 1, 0, 0]]));
console.log(...getColSum([[0, 0, 0], [1, 0, 0], [1, 1, 0]]));
A version without (nearly) array methods.
function getColSum (matrix) {
const result = Array(matrix[0].length).fill(0);
for (const row of matrix) {
for (let i = 0; i < row.length; i++) result[i] += row[i];
}
return result;
}
console.log(...getColSum([[0, 0, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1], [0, 1, 0, 0]]));
console.log(...getColSum([[0, 0, 0], [1, 0, 0], [1, 1, 0]]));
I have two arrays (a and b) where each item has to move into another array (c)
I used the push/slice method to do this but it adds brackets into each element.
Here is the code:
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1];
let c = [];
for (let i = 5; a.length > 0 || b.length > 0; i--) {
c.push(a.splice(i, 1), b.splice(i, 1));
}
console.log(c) // I would like [element1, element2, etc]
Instead, is there another method (to replace c.push(a.splice(i, 1), b.splice(i, 1))) which does not add brackets to each element ?
Thanks!
based on the comment "it'd be nice to keep them in the same order"
using another answers use of .pop .. use .shift to remove from beginning of array instead of end
also, you can use a while loop instead
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1];
let c = [];
while(a.length > 0 || b.length) {
c.push(a.shift(), b.shift());
}
console.log(c)
Of course, the above is fine if the array lengths are the same
But, based on your original code ... this works with uneven length arrays:
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1, 99];
let c = [];
while(a.length > 0 || b.length) {
c.push(...a.splice(0,1), ...b.splice(0, 1));
}
console.log(c)
Notice the splice is always at index 0 - to maintain order
Using your exact answer all I did is access the 0 location in each spliced thing
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1];
let c = [];
for (let i = 5; a.length > 0 || b.length > 0; i--) {
c.push(a.splice(i, 1)[0], b.splice(i, 1)[0]);
}
console.log(c) // I would like [element1, element2, etc]
i already commented it, but is an answer, so here the answer
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1];
let c = [];
for (let i = 5; a.length > 0 || b.length > 0; i--) {
c.push(a.pop(), b.pop());
}
console.log(c);
Simple for loop to go through both a and b at once and add the element, if there is one:
let a = [0, 2, 4, 6, 8, 10, 12, 14];
let b = [1, 3, 5, 7, 9];
let c = [];
for (let i = 0; i < Math.max(a.length, b.length); i++) {
if (i < a.length)
c.push(a[i]);
if (i < b.length)
c.push(b[i]);
}
console.log(c);
This assumes the arrays would be of unequal length. If they are always going to be equal, then the if statements are not needed.
Alternative implementation for arrays of equal length:
let a = [0, 2, 4, 6, 8, 10];
let b = [1, 3, 5, 7, 9, 11];
let c = a.flatMap((x, i) => [x, b[i]]);
console.log(c);
If a and b have to end up empty in the end, this can be just a simple:
a.length = 0;
b.length = 0;
after c is created, instead of continually mutating the two arrays.
The spread syntax is a new addition to the set of operators in JavaScript ES6. It takes in an iterable (e.g an array) and expands it into individual elements.
The spread syntax is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.
So , instead use :
let a = [0, 6, 2, 0, 1, 1];
let b = [1, 3, 9, 0, 0, 1];
let c = [...a,...b]
console.log(c)
let a=[4, 3, 2, 2, 0, 1]
let b=[0, 1, 2, 2, 3, 4]; //My code demo below help to sort a into ascending order.
Output=[4, 5, 2, 3, 1, 0];
// Get the position of element index in a ascending order. For example, 0 is in index 4, 1 is in index 5, 2 is in index 2,2 is in index 3, and 4 is in index 0. Please Provide a demo. Thank you
var Arr = [4, 3, 2, 2, 0, 1];
for (var i = 1; i < Arr.length; i++) {
for (var j = 0; j < i; j++) {
if (Arr[i] < Arr[j]) {
var x = Arr[i];
Arr[i] = Arr[j];
Arr[j] = x;
}
}
}
console.log(Arr);
You could get the indices of the unsorted array and sort the indices by the values of the array.
const
array = [4, 3, 2, 2, 0, 1],
indices = [...array.keys()];
indices.sort((a, b) => array[a] - array[b]);
console.log(...indices);
you can use Object.entries :
let a=[4, 3, 2, 2, 0, 1]
let result = Object.entries(a).sort((a,b) => a[1]-b[1]).map(e => +e[0])
console.log(result)
i guess this is what you want
and also i change some other thing in code to be cleaner and readable
you can ask me anything if you want
function sortWithIndex (self) {
let Arr = [4, 3, 2, 2, 0, 1];
let result = [];
let ArrLength = Arr.length;
for (let i = 0; i < ArrLength; i++) {
number = Arr[i];
indexOfNumber = i;
result.push ('index ' + indexOfNumber + ' is ' + number);
};
return result
};
console.log(sortWithIndex());
Mostafa.T 🐍
I would like to iterate through two arrays subtracting one arrays value from another and adding their specific difference values to an object. So for example I have:
var answer = [];
var boom = [1,2,3,4];
var other = [[1,2,3,4],
[2,3,4,5],
[6,7,8,9];
for(var i=0; i<other.length; i++) {
for(var e=0; e<4; e++){
answer[e] = boom[e] - other[i][e];
}
}
This give me an output of:
Object {0: -5, 1: -5, 2: -5, 3: -5}
Which is boom subtracted from the last array in other what I am looking for and I think I am very close to getting it is:
Object [{0: [ 0, 0, 0, 0]},
{1: [-1,-1,-1,-1]},
{2: [-5,-5,-5,-5]}];
You can see that it will add the results of each iteration of the second for loop to the object answer. How can I accomplish this?
for(var i=0; i<other.length; i++) {
answer[i] = [];
for(var e=0; e<4; e++){
answer[i][e] = boom[e] - other[i][e];
}
}
You need to initialize answer as an object not an as array, also you need to create a new answer array representing each set of values in other
var answer = {};
var boom = [1, 2, 3, 4];
var other = [
[1, 2, 3, 4],
[2, 3, 4, 5],
[6, 7, 8, 9]
];
for (var i = 0; i < other.length; i++) {
var temp = answer[i] = {};
for (var e = 0; e < 4; e++) {
temp[e] = boom[e] - other[i][e];
}
}
Demo: Fiddle