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();
Related
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.
Improve this question
I have two arrays:
var a = ["Saab", "Volvo", "BMW"]
var b = ["Banana", "Mango", "Orange"]
I want to insert b array values into random positions of a. Expected output should be:
var c = ["Banana", "Saab", "Volvo", "BMW", "Orange", "Mango"]
Here, b-array values are inserted into a, but the value sequence of a-array is not hampered. How can I do that using Javascript?
"Saab" → "Volvo" → "BMW"
You can repeatedly pop values from b and splice them into a at random positions:
while (b.length) {
a.splice(Math.floor(Math.random() * (a.length + 1)), 0, b.pop());
}
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 3 years ago.
Improve this question
I have this objects from google API, if I console.log(ret) I have this:
{name:x, stars:5},
{name:y, stars:4},
{name:j, stars: 3}
I am getting this result from the following loop:
for (let i = 0; i < fromGoogle.length; i++) {
let ret = fromGoogle[i];
}
I want to create an Array of Objects like:
[{...},{...},{...}]
How do I do it?
Thank you, I am new at JS
If you have, for instance, objects referenced by individual variables:
let a = {name:x, stars:5};
let b = {name:y, stars:4};
let c = {name:j, stars: 3};
you can create an array of them using an array initializer (aka "array literal"):
let array = [a, b, c];
You don't need the individual variables, though, you can do this:
let array = [
{name:x, stars:5},
{name:y, stars:4},
{name:j, stars: 3}
];
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 5 years ago.
Improve this question
Let's say that I have a map with keys : 1,2,3,4,5
and let's say I have an array which contains : [2,3,4]
what I am trying to do is iterate through map and remove keys 2,3,4
some final map will have 1,5 keys and their values
You do not need to check, you can directly delete. Say arr is your map then do
var keysToRemove = [2,3,4];
keysToRemove.forEach( el => delete arr[el] )
Use forEach and delete
var map = {
"1": "1",
"2": "1",
"3": "1",
"4": "1",
"5": "1"
};
var arr = [2,3,4];
arr.forEach( s => delete map[s] );
map is now
{1: "1", 5: "1"}
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]]
]
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).