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());
}
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 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have an arrA filled with words. Each of the words get assignet a Value starting at 1.
arrA = ["mango", "banana", "apple"...]
for (var i = 0; i < arrA.length; ++i)
mango = 1
banana = 2
apple= 3
arrA and i get implemented into a script later on which makes both values crusial for working of the programm. I want to sort arrA alphabeticly without it loosing their assigned i.Like this:
apple = 3
banana = 2
mango = 1
arrA works as a description for the user, and each i is used to define which scripts are going to start.
I´ve combined them with concat() and convertet them to an Array in order to sort them.
var arrInput = arrA[i].split(';', 2);
var strData = arrInput.concat(i) + '';
var a = strData.slice(0, -2);
var arrFruits = a.split();
arrFruits.sort();
var b = strData.slice(-2);
value = b.match(/\d+/)[0];
Alerting arrFruits outputs: mango banana apple
How do I get arrA to sorting without craping up the numbers order?
Use map to create an object with the value and it's i number, and then use sort to sort by the value.
const arrA = ["mango", "banana", "apple"]
const result = arrA.map( (x,i) => ({value:x, i:i+1}))
.sort( (a,b) => a.value.localeCompare(b.value))
console.log(result);
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 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();
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
I have two arrays one is string and other in integer in JavaScript and I want an object in this form
[["a",1],["b",2],["c",3]]
Please help how I create this object
Edit: Practical example of where this is needed...https://www.cssscript.com/creating-funnel-charts-using-svg-and-d3-js-d3-funnel/
Here's one of the many possible solutions :
var a1 = ["a", "b", "c"];
var a2 = [1, 2, 3];
var a3 = a1.map(function(e,i){ return [e,a2[i]] });
This makes [["a",1],["b",2],["c",3]].
See map
If what you want is an object (an "associative array") like {a: 1, b: 2, c: 3}
then it's a little more interesting :
var a1 = ["a", "b", "c"];
var a2 = [1, 2, 3];
var a3 = a1.reduce(function(r,e,i){ r[e]=a2[i]; return r }, {});
See reduce