This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
[{"e":"01-25-01-006"},{"e":"01-02-05-001"}], I have this.
[ 01-25-01-006,01-02-05-001 ], I need this format of array
You can use map to get the desired outcome:
let data = [{
"e": "01-25-01-006"
}, {
"e": "01-02-05-001"
}];
console.log(data.map(entry => entry.e));
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:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of string to array of object using es6 or lodash
(4 answers)
Closed 1 year ago.
I have an array of objects as shown below
Array(2)
0: "104635ac-4b9a-3f7a-d4f8-e8badca83d58"
1: "547d57b7-ddc7-a481-95e4-ccfe2a6404a8"
I want to add two more properties one named productId and one name quantity to each element of this array of Objects.
The final outcome should be as follows.
{
"productId": "104635ac-4b9a-3f7a-d4f8-e8badca83d58",
"quantity": 1,
},
{
"productId": "547d57b7-ddc7-a481-95e4-ccfe2a6404a8",
"quantity": 1,
}
Can someone please let me know how to achieve this?
You can use Array.map to map through the array and construct an object:
const arr = ["104635ac-4b9a-3f7a-d4f8-e8badca83d58","547d57b7-ddc7-a481-95e4-ccfe2a6404a8"]
const result = arr.map(e => ({quantity: 1, productId: e}))
console.log(result)
This question already has answers here:
JavaScript - why Array.prototype.fill actually fills a "pointer" of object when filling anything like 'new Object()'
(4 answers)
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 1 year ago.
Why does table[0][0] = 2 not only assign that value to the first item in the first array?
let table = new Array(3).fill(Array(3));
table[0][0] = 2;
console.log(table);
output: [ [ 2, , ], [ 2, , ], [ 2, , ] ]
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);
This question already has answers here:
Condensing a sparse array in Javascript?
(6 answers)
Convert Array to Object
(46 answers)
Closed 4 years ago.
I have a problem with my script Javascript. I try to create a multidimensional array with dates in the key.
I would like to have something like
"1" : [ "2018-01-01", "2018-02-02" ]
"11" : [ "2018-01-01", "2018-02-02" ]
But I get something like
1 : [ "2018-01-01", "2018-02-02" ]
2 : ""
3 : ""
..
11 : [ "2018-01-01", "2018-02-02" ]
I'm doing
var array = [];
array[no].push(date);
Thanks for the future help,
MYT.
You should use an Object rather than an Array.
var object = {};
object[no] = object[no] || [];
object[no].push(date);