joining arrays with concat and using a loop [duplicate] - javascript

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
flatten an array without using .flat();
(16 answers)
Closed 9 months ago.
I run into the following problem, the following array: [[1,2,3],[4,5,6],[7,8,9]] I must go through it and get the following result [1, 2,3,4,5,6,7,8,9]. Currently I get the same result. Here my code:
let main= [[1,2,3],[4,5,6],[7,8,9]]
let result= [].concat(
main.map((e)=>{
return e
})
)
console.log(result)

Related

Trying to concat an array of multiple array into one array [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
How to flatten nested array in javascript? [duplicate]
(27 answers)
Closed 2 years ago.
I try to concat Array of multiple Arrays like this
`let soldNumber = [
[1,2],
[5,7],
[35,67],
...
]`
to recieve this let soldNumber = [1,2,5,7,35,67,...]
soldNumber.flat()
Array.prototype.flat()
let soldNumber = [[1,2], [5,7], [35,67]];
let newSoldNumber = soldNumber.flat();

Flatten array with arrays [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 3 years ago.
Say I have an array with arrays inside it. I would like to flatten it and get a single array with all the values.
let arrWithArrs = [[1,2,3], [4,5,6]];
let array = arrWithArrs.map(arr => ...arr);
This obviously doesn't work but I would like to know how to make it work.
The wanted outcome would be
array = [1,2,3,4,5,6];
You can perform a reduce on the array to combine each of the arrays into a single array like so:
arrWithArrs.reduce((result, lst) => [...result, ...lst], []);

JavaScript hash of Arrays [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 4 years ago.
I have such type of array
How to access Viena
There could be a lot of cities
[{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"},{..},{..}],{"Paris":[{..}]} ]
You can use Array.filter for this.
var data = [{"Viena":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]},{"Paris":[{"date":"2018-11-10","time":"17:45","price":599,"to_city":"Viena","wday":"saturday"}]}];
var result = data.filter(function(value){return Object.keys(value).indexOf("Viena") != -1;});
console.log(result[0]);

How to iterate array of objects [duplicate]

This question already has answers here:
Object.length undefined in javascript [duplicate]
(3 answers)
JavaScript object literal length === undefined?
(5 answers)
Closed 5 years ago.
Here is my jsfiddle
Here is my array looks like
var arr = [];
arr['alpha'] = {'a':'1','b':'1'};
arr['beta'] = {'a':'2','b':'4'};
console.log(arr)
When i take console.log(arr.length) It says 0. So i can't able to for loop this one
How can i iterate this array ?
Note :
I don't want to use jquery, i prefer only javascript

How to combine 2 array to an Object? [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
Closed 8 years ago.
I have 2 arrays which are the same length like this:
$scope.access_name = ['group1','group2','group3','group4']
access_value=[1,2,1,3]
How can I combine to have result like this:
object_access = {'group1:1','group2:2','group3:1','group4:3'}
thankyou very much
var object_access={};
for(int i=0;i<access_value.length;i++)
{
object_access[$scope.access_name[i]]=access_value[i];
}

Categories