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)
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:
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);
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 2 years ago.
I've got an array of sessions defined something like this:
const sessions = [{id: 1, eventYear: "2019"},{id: 2, eventYear: "2018"},{id: 3, eventYear: "2018"}];
and I want to create a new array of objects with just the eventYear "2018"
I tried mapping over the array and if session.eventYear === eventYear return the session record, otherwise return nothing, but got a list of objects 3 long (not 2) with one undefined record. I wanted just 2 records returned.
I have the following working, but it feels uncomfortably non functional and long. I'd appreciate a suggestion for how to make this more concise in modern JavaScript
const sessionsForEventYear = [];
sessions.map(function(session) {
if (session.eventYear === eventYear) {
sessionsForEventYear.push(session);
}
});
Use Array.prototype.filter instead:
const sessions = [{id: 1, eventYear: "2019"},{id: 2, eventYear: "2018"},{id: 3, eventYear: "2018"}];
const sessionsForEventYear = sessions.filter(({ eventYear }) => eventYear === '2018');
console.log(sessionsForEventYear);
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));
This question already has answers here:
How to append something to an array?
(30 answers)
How to extend an existing JavaScript array with another array, without creating a new array
(20 answers)
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 9 years ago.
I created the following array:
$scope.testAccounts[0] = { id: 99, name: "Select Account" };
I tried
$scope.testAccounts.push(result.data);
where results.data looks something like this:
[{ id: 1, name: "x" },{ id: 2, name: "y" }]
However this does not seem to work as it tries to add an array as the second element. What I need is to have the contents of the array result.data appended to the array $scope.testAccounts
Please note that all the examples I have seen so far seem to not work if the array is an array of objects. This is what I have. Thanks
You're looking for Array.concat
> foo = [1,2,3]
[1, 2, 3]
> foo.concat([4,5,6])
[1, 2, 3, 4, 5, 6]