turn into 2d array index [duplicate] - javascript

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 7 years ago.
I have a array with 3*3 dimension(2d) like this:
a[3][3]=[[1,2,3],[4,5,6],[7,8,9]];
I want formulate this array to access in single array(1d). how I can do this?
like:
b[9]=[0,1,2,3,4,5,6,7,8,9];

you want to convert a 2d array to a flat array. How about:
var a = [[1,2,3],[4,5,6],[7,8,9]];
var merged = [];
merged = merged.concat.apply(merged, a);
see https://stackoverflow.com/a/10865042/1432801

var flatArray = [];
for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
flatArray.push(a[i][j]);
}
}

Related

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

Weird behaviour in Array.fill [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

Add multiple options in array with javascript [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
I have this var
var allwords = {};
And i need push more options in this array like:
allwords.push = {"Ctrl-Space": "autocomplete"};
allwords.push = {"Ctrl-pause": "closewindow"};
And look like this:
allwords = {"Ctrl-Space": "autocomplete", "Ctrl-pause": "closewindow"};
How can't i do?
push is for Array objects. For traditional objects, assign the properties manually like
var allwords = {};
allwords["Ctrl-Space"] = "autocomplete";
allwords["Ctrl-pause"] = "closewindow";
console.log(allwords);

javascript convert array to json object [duplicate]

This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,
Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.
Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);

Is possible to combine two arrays? "array.join(array)"? [duplicate]

This question already has answers here:
zip two arrays Javascript [duplicate]
(3 answers)
Closed 8 years ago.
I have two arrays:
var array1= [[1],[2],[3]];
var array2= [[10],[20],[30]];
is there a way to (have) a third array?
var array3 = [[1],[10],[2],[20],[3],[30]];
Maybe:
var test= array1.join(array2 + "<br>");
I think you are looking for the "concat" function
Join two arrays:
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
The values of the children array will be:
Cecilie,Lone,Emil,Tobias,Linus
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
You can either use CONCAT function like
array1 = array1.concat(array2)
OR, APPLY()
array1.push.apply(array1, array2)

Categories

Resources