How to create an array of object with 2 arrays .js [duplicate] - javascript

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)

Related

Convert an object into array javascript [duplicate]

This question already has answers here:
Converting JavaScript object with numeric keys into array
(17 answers)
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
(21 answers)
Closed 10 months ago.
I have an object with these key-value pairs. This object comes from an API which I am calling :
APIName(obj).subscribe((res:any) => {
console.log(Object.values(res.data));
})
data = {
0 : 1,
1: 3,
2: 7,
3: 10
....so on
}
simply put it is an object of numbers and my desired output is (a simple array) :
data = [1,3,7,10]
I've tried Object.value and Object.key it still converts it into an object. Any help?
First of all if your data is not sorted by key. Then sort it. and iterate through object and push to array.
var data = {
1: 1,
0: 3,
2: 7,
3: 10
};
let sortedKeys = Object.keys(data).sort();
let arrayOfData = [];
// iterate method
sortedKeys.forEach((key) => {
arrayOfData.push(data[key]);
});
console.log(arrayOfData);

Adding a property to an array of objects [duplicate]

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)

ReactJs Combine Key and Value [duplicate]

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);

How to do split array of object in javascript? [duplicate]

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));

myArray.push(["dog", 3]); should push dog and 3 to the end but does not, what am I missing here? [duplicate]

This question already has answers here:
JavaScript: How to join / combine two arrays to concatenate into one array?
(3 answers)
Closed 7 years ago.
var ourArray = ["Stimpson", "J", ["cat"]];
ourArray.pop(); // ourArray now equals ["Stimpson", "J"]
ourArray.push(["happy", "joy"]); // ourArray now equals ["Stimpson", "J", ["happy", "joy"]]
var myArray = ["John", 23, ["cat", 2]];
myArray.pop();
// Only change code below this line.
*myArray.push(["dog", 3]);*
// Only change code above this line.
(function(z){return 'myArray = ' + JSON.stringify(z);})(myArray);
Array.push will push anything onto the array, what you are doing is pushing another array into the array, you will need to push "dog" and 3 without the array.
Array.push can take multiple arguments. So just do myArray.push("dog", 3);

Categories