This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
merge two arrays (keys and values) into an object [duplicate]
(3 answers)
Closed 2 years ago.
Platform: ReactJS
I am trying to combine the two arrays as a date:value object. Any thoughts?
a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
b = [ 1, 2, 3, 4 ]
I am looking for the following result:
["1/1/2020":1, "1/2/2020":2, "1/3/2020":3, "1/4/2020":4]
Thank you,
You can try this approach.
const a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
const b = [ 1, 2, 3, 4 ]
const c = {};
a.forEach((v, i) => {
c[v] = b[i]
});
console.log(c);
Related
This question already has answers here:
How to create an array of objects from multiple arrays
(2 answers)
how convert array to each array in javascript [closed]
(2 answers)
How to combine two arrays into an array of objects? [duplicate]
(2 answers)
Closed 9 months ago.
I have two arrays
study_id: ["1", "2", "3"],
study_name: ["clinic Study", "study test", "test 2"],
I want to create an array of objects with the array as follows:
"studies": [{
"study_id": "1",
"study_name": "clinic study"
},
{
"study_id": "2",
"study_name": "test study"
}
]
How to achieve this?
thanks
there are a few ways to do this but one ES6 solution is to use map method in first array and map each element to a object which key is element from first array and value is element with same index in the second array.
var a = [1, 2, 3]
var b = ['a', 'b', 'c']
var c = a.map((e, i) => {
return { [e]: b[i] };
});
console.log(c)
This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
How to serialize a Map in javascript?
(3 answers)
Closed last year.
Why does this map print empty? I'd expect it to be populated from the array? I'm probably missing something small.
let numbers = [1, 2, 3];
let numberMap = numbers.reduce((map, obj) => {
return map.set(obj, obj + 1);
}, new Map());
console.log(JSON.stringify(numberMap));
This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Zip arrays in JavaScript?
(5 answers)
Transposing a 2D-array in JavaScript
(25 answers)
Closed 1 year ago.
Hi guys I have this array :
[[2,3,0],[0,4,5]]
and i want traverse this array like:
[[2,0],
[3,4],
[0,5]]
Any recommendations please? I am working with javascript
You can easily achieve this result using the Array.prototype.map
const arr = [
[2, 3, 0],
[0, 4, 5],
];
const result = arr[0].map((val, i) => {
return Array(arr.length)
.fill("")
.map((_, index) => arr[index][i]);
});
console.log(result);
This question already has answers here:
Convert list of Objects when the levels are given in an array
(2 answers)
Populate nested object from array?
(2 answers)
Closed 3 years ago.
What's the best way to create a N levels nested object (where N is the size of the array) for example:
const arr = ['a','b','c','d']
The output object should look like this:
{
a: {
b: {
c: {
d: true
}
}
}
}
You can use array.reduce, it helps you pass an accumulator where you can accumulate your nested obj.
const array = ['a','b','c','d'];
const object = {};
array.reduce((o, s) => {
return o[s] = {};
}, object);
console.log(object);
This question already has answers here:
Sum all integers in a multidimensional array javascript
(9 answers)
Closed 5 years ago.
I'm using a nested array like this:
const data = [
[0],
[2],
[[1], 3]
1
]
Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1).
And is it also possible to count how many arrays are used? This would be 5 arrays
const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);
This uses a recursive approach with reduce.