Reverse array at n level only [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for a good way to reverse multi-dimensional array in javascript but at a specific level (n) only, like:
[
[[2,1], [4,2], [6,3], [3,4]],
[[5,1], [1,2], [3,3], [4,4]],
[[4,1], [7,2], [1,3], [2,4], ['monkey', [3,4,5,6,7]]]
]
into this (with n = 2):
[
[[1,2], [2,4], [3,6], [4,3]],
[[1,5], [2,1], [3,3], [4,4]],
[[1,4], [2,7], [3,1], [4,2], [[3,4,5,6,7], 'monkey']]
]
For the time being, I've coded this short prototype which meets my need:
Array.prototype.reverseAtLevel = function(level){
if(level < 0)
return;
if(level == 0)
this.reverse();
this.forEach(function(element, index, array){
if(element instanceof Array)
element.reverseAtLevel(level-1);
});
return;
};
Using it like this:
aMultiDim.reverseAtLevel(2)
Is this the right approach to take?
EDIT:
I don't want to have the "direct" .reverse() behaviour which will output my first example as:
[
[[4,1], [7,2], [1,3], [2,4], ['monkey', [3,4,5,6,7]]],
[[5,1], [1,2], [3,3], [4,4]],
[[2,1], [4,2], [6,3], [3,4]]
]

Related

Split an array into two Parts [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can we split or divide an array into two new arrays?
SingleARR = [7,5,6,4,3,2,4,5,4,2,8,8];
one array should have values that don't repeat
and the other has values that repeat. Moreover, both new arrays should have different elements from each other.
First, count the frequencies. Then filter it by the frequency if it is one then that does not repeat and push it into one array. Then again filter it by the frequency, if it is greater than 1 then it repeats and pushes
let a = [7, 5, 6, 4, 3, 2, 4, 5, 4, 2, 8, 8];
let ret = a.reduce((p, c) => {
if (!p[c]) p[c] = 1;
else p[c] += 1;
return p;
}, {});
let x = [];
let y = [];
console.log(ret);
for (prop in ret) if (ret[prop] === 1) x.push(+prop);
for (prop in ret) if (ret[prop] > 1) y.push(+prop);
console.log(x);
console.log(y);
it into another array.

javascript, merge two arrays by index [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Here are two arrays:
const a = [1, null, 2, null]
const b = [null, null, 3, 4, null]
Note: The length of a and b is not fixed.
I want elements of b replace elements of a by index if the element is not null.
expect value:
[1, null, 3, 4]
const result = b.map((el, i) => el === null ? a[i] : el);
result.push(...a.slice(b.length));
Just map to a new array.
You can use map() function
var x=b.length+1;
b.concat(a);
var c=b.map(function(el, index) {
if(a[index]!=null && b[index]==null){
return b[index]=a[index];
else{
return b[index]
}
console.log(c.slice(0,x));

Fastest way to delete and return last n items from an array JavaScript [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If I have an array in JavaScript, how can I remove the last n items from it and return it as a new array?
I have two options, splice or to iterate pops/pushes. Which is better?
Thanks
You should splice it:
array.splice(array.length-n, array.length);
As such:
function removeLast(arr, n){
arr.splice(arr.length-n, arr.length);
return arr;
}
arr = JSON.parse(prompt("Enter the array"));
n = parseInt(prompt("Enter the number of elements you want to remove"), 10);
alert(removeLast(arr, n));
> removeLast([1, 2, 3, 4, 5], 3)
[1, 2]
> removeLast([1, 2, 3, 4, 5], 2)
[1, 2, 3]
> removeLast([1, 2, 3, 4, 5], 1)
[1, 2, 3, 4]
Just for fun I whipped up a quick jsperf test to compare the two methods
http://jsperf.com/splicevspop
Splice is demonstrably faster

how use javascript array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how use javascript
reverse array
i wanted it is("861", "860","859","858" ............. )
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
var splitarr = arr.split(",");
for (var i = 0; i < arr.length; i++){
console.log(splitarr);
}
it is java arrays?
Simply: reverse()
The reverse() method reverses an array in place. The first array
element becomes the last and the last becomes the first.
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
arr.reverse();
console.log(arr);
DEMO
Array has a reverse function on it's prototype.
var arr = [
"803", "812", "828", "846", "851",
"852", "853", "857", "858", "859", "860", "861"
];
arr.reverse()
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
You can simply call the reverse() function of javascript
arr.reverse();

two javascript arrays and display result with count [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi I have two javascripts arrays and I want to display result in below way (specified in desired result table):
array1 = ( '2013/01/02','2013/01/03','2013/01/02','2013/01/02' );
array2 = ( 'a' ,'b', 'c', 'a' );
I need result in below format but in HTML page:
2013/01/02 2013/01/03
a 2 0
b 0 1
c 1 0
hints: array1 1st value link with array2 1st value, array1 2nd value link with array2 2nd value ...
How many 2013/01/02 and a ? if we compare two arrays ? count is 2 but should display in matrix
To count unique values use:
array1 = [ '2013/01/02','2013/01/03','2013/01/02','2013/01/02' ];
array2 = [ 'a' ,'b', 'c', 'a' ];
var counts = {};
for (var i = 0; i < array1.length; i++) {
if (!counts[array1[i]])
counts[array1[i]] = {};
if (counts[array1[i]][array2[i]])
counts[array1[i]][array2[i]] += 1;
else
counts[array1[i]][array2[i]] = 1;
}
DEMO.
Using two foreach loops you get your desired table (JQuery.each DEMO).

Categories

Resources