This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
convert array of objects into object of objects properties from array
(4 answers)
Closed 12 months ago.
I have a parsed json array:
parsedJson = JSON.parse(comments);
const reformattedArray = parsedJson.map(({username, rating}) => ({[username]: rating}));
[{…}, {…}, {…}, {…}, {…}]
[{First username: 5}, {Second username: 4}, {Third username: 3}, {Fourth username: 2}, {Fifth username: 1}]
but how is it possible to look my array like this:
{…,…,…,…,…}
{First username: 5, Second username: 4, Third username: 3, Fourth username: 2, Fifth username: 1}
Is it possible to make this array to look like this?
You can easily achieve this with Array.reduce:
const data = [{FirstUsername: 5}, {SecondUsername: 4}, {ThirdUsername: 3}, {FourthUsername: 2}, {FifthUsername: 1}]
const oneObject = data.reduce((total, current) => {
const keyName = Object.keys(current)[0]
total[keyName] = current[keyName]
return total
}, {})
console.log(oneObject)
Related
This question already has answers here:
Filter array of objects based on another array in javascript
(10 answers)
Closed 11 days ago.
I need to get filtrated array that have products only with ids from first array. I tried something like this:
let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
second.filter((es, i) => es.id.includes(first))
you will need to reverse the include condition so it should be first.includes(es.id) instead of es.id.includes(first)
let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
let result=second.filter(es => first.includes(es.id))
console.log(result);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
please I have 2 arrays in the following format
const images = ["image1", "image2", "image3", "image4"]
const items = [{quantity: 1}, {quantity: 2}, {quantity: 4}, {quantity: 1}]
I need the final output to be like below
const newArray = [
{
image: "image1",
quantity: 1
},
{
image: "image2",
quantity: 2
},
{
image: "image3",
quantity: 4
},
{
image: "image4",
quantity: 1
},
]
You can use Object.assign() to append a new data to the target object.
Working demo: https://replit.com/#kallefrombosnia/data-merge#index.js
const images = ["image1", "image2", "image3", "image4"]
const items = [{quantity: 1}, {quantity: 2}, {quantity: 4}, {quantity: 1}]
// Parse trough images
images.forEach((image, index) =>{
// Assign new item into target object
Object.assign(items[index], {image});
});
console.log(items)
/**
[
{ quantity: 1, image: 'image1' },
{ quantity: 2, image: 'image2' },
{ quantity: 4, image: 'image3' },
{ quantity: 1, image: 'image4' }
]
*/
You can make use of Array#map:
const images = ["image1", "image2", "image3", "image4"];
const items = [{quantity: 1}, {quantity: 2}, {quantity: 4}, {quantity: 1}];
items.map((e, i) => e.image = images[i]);
console.log(items);
I'm struggling with array manipulation when they are arrays of different types. My problem is rather simple, but I would still appreciate any assistance.
I have the following two classes:
export interface Group {
gId: number;
gName: string;
}
export interface UserGroup {
uId: number;
gId: number;
}
I have two arrays:
finalUserGroupsToUpdate: UserGroup[];
pickListUserAssociatedGroups: Group[];
The array of Group is populated and looks like this in the console:
(3) [{…}, {…}, {…}]
0: {gId: 1, gName: "Manager"}
1: {gId: 2, gName: "Nurse"}
2: {gId: 3, gName: "Audit"}
I also have a uId that is accessible through the active route in Angular (for this example it can just be a local variable):
currentUserID = 1;
What I want to be able to do is push each gId of the array of Group into the array of UserGroup while appending currentUserId as the uId.
The final result should look something like:
(3) [{…}, {…}, {…}]
0: {gId: 1, uId: 1}
1: {gId: 2, uId: 1}
2: {gId: 3, uId: 1}
You can use Array#map.
const arr = [{gId: 1, gName: "Manager"},
{gId: 2, gName: "Nurse"},
{gId: 3, gName: "Audit"}];
let currentUserID = 1;
const res = arr.map(({gId})=>({gId, uId: currentUserID}));
console.log(res);
const finalUserGroupsToUpdate: UserGroup[];
const pickListUserAssociatedGroups: Group[] =[{gId: 1, gName: "Manager"},
{gId: 2, gName: "Nurse"},
{gId: 3, gName: "Audit"}];
const currentUserID = 1;
pickListUserAssociatedGroups.forEach(({gId}) => {
finalUserGroupsToUpdate.push({gId, uId: currentUserID})
});
console.log(finalUserGroupsToUpdate);
Say, you have your groups in arrayOfGroups and you want to assign each group id gId to current user id uId and hold them in array arrayOfUserGroups.
currentUserID = 1;
arrayOfUserGroups = [];
arrayOfGroups.forEach(g => {
arrayOfUserGroups.push({gId: g.gId, uId: currentUserID});
});
This question already has answers here:
Extract certain properties from all objects in array
(5 answers)
Closed 4 months ago.
Let's say I create object like this:
updates.push({
id: this.ids[i],
point: point,
value: value
});
Later on I want to use JSON.stringify on updates object, however I need only
point and value like:
updates[{point: 1, value: 12}, {point: 2, value: 24}]
What's the best ES6 solution for that?
I looked at some examples with delete, but that's not what I actually need, as I do not want to delete ids.
Try the following :
JSON.stringify(updates.map(({point,value})=>({point,value})));
let updates = [{id : 1, point : 1, value: 2},{id : 1, point : 1, value: 2}];
console.log(JSON.stringify(updates.map(({point,value})=>({point,value}))));
If updates is an array. Then you might want something like this:
const newArrayWithoutId = updates.map(({ point, value }) => {
return {
point,
value,
}
}
Just ({id, ...rest}) => ({...rest}) is too short for an answer, so how about this?
const withoutId = ({id, ...rest}) => ({...rest})
const vals = [
{id: 'a', point: 1, value: 'foo'},
{id: 'b', point: 2, value: 'bar'},
{id: 'c', point: 3, value: 'baz', meaning: 42}
]
const reduced = vals.map(withoutId)
console.log(reduced)
This question already has answers here:
filtering an array of objects using an array without nested loops js
(1 answer)
Filter array of objects based on another array in javascript
(10 answers)
Closed 4 years ago.
I have an array of Objects:
[
{id: 1, thing_id: 2},
{id: 1, thing_id: 3},
{id: 1, thing_id: 4}
]
and I want to filter that using an array of thing_ids:
[2,3]
I did look at filtering an array of objects using an array without nested loops js but it doesn't seem to work.
Clearly I need to use .filter somehow?
Mick
You can make use of Array.prototype.filter() and Array.prototype.includes() like the following way:
var arr1 = [{ id:1, thing_id: 2},
{ id: 1, thing_id: 3},
{ id: 1, thing_id: 4}];
var arr2 = [2,3]
var res = arr1.filter(i => arr2.includes(i.thing_id))
console.log(res);