I have the following array object.
let reports = [
{
cc: 'BEL',
cn: 'Belgium',
entities: [
{
entityName: 'Company A',
address: 'some address'
},
{
entityName: 'Company B',
address: 'some address'
}
]
},
{
cc: 'LUX',
cn: 'Luxembourg',
entities: [
{
entityName: 'Company C',
address: 'some address'
}
]
}
];
I want to go through reports and create a new array with only the value of entityName how can I accomplish this?
The new array should look like ['Company A', 'Company B', 'Company C']. How can I accomplish this?
I tried doing: reports.map(e => e.entities.map(r => r.entityName)). However this returns:
[['Company A', 'Company B'], ['Company C']]
You can use .flat() on your result.
let reports = [
{
cc: 'BEL',
cn: 'Belgium',
entities: [
{
entityName: 'Company A',
address: 'some address'
},
{
entityName: 'Company B',
address: 'some address'
}
]
},
{
cc: 'LUX',
cn: 'Luxembourg',
entities: [
{
entityName: 'Company C',
address: 'some address'
}
]
}
];
const res = reports.map(e => e.entities.map(r => r.entityName)).flat();
console.log(res);
I'd do it like this where I first map entities, then flat them and finally return entityNames as a single array result:
let reports = [{
cc: 'BEL',
cn: 'Belgium',
entities: [{
entityName: 'Company A',
address: 'some address'
},
{
entityName: 'Company B',
address: 'some address'
}
]
},
{
cc: 'LUX',
cn: 'Luxembourg',
entities: [{
entityName: 'Company C',
address: 'some address'
}]
}
];
const flattenedEntities = reports
.map(({entities}) => entities)
.flat()
.map(({entityName}) => entityName);
console.log(flattenedEntities);
You can get simply by using for loop.
var reports = [
{
cc: 'BEL',
cn: 'Belgium',
entities: [
{
entityName: 'Company A',
address: 'some address'
},
{
entityName: 'Company B',
address: 'some address'
}
]
},
{
cc: 'LUX',
cn: 'Luxembourg',
entities: [
{
entityName: 'Company C',
address: 'some address'
}
]
}
];
var yourarray = [];
for (var j = 0; j < reports.length; j++) {
var reportentity = reports[j].entities;
for (var i = 0; i < reportentity.length; i++) {
yourarray.push(reportentity[i].entityName);
}
}
console.log(yourarray)
Related
I'm trying to group the objects for the users that have the same location and type value shown in the example below. What would be the best way to approach this?
const diabetics = [
{
type: 'type 1',
location: 'Boston'
email: 'person1#gmail.com'
},
{
type: 'type 2',
location: 'New York'
email: 'person2#gmail.com'
},
{
type: 'type 1',
location: 'Boston'
email: 'person3#gmail.com'
},
{
type: 'type 1',
location: 'Maine'
email: 'person4#gmail.com'
},
]
// expected output
const diabetics = [
{
type: 'type 1',
location: 'Boston'
email: [
'person1#gmail.com',
'person3#gmail.com'
]
},
{
type: 'type 2',
location: 'New York'
email: 'person2#gmail.com'
},
{
type: 'type 1',
location: 'Maine'
email: 'person4#gmail.com'
},
]
You could also get the result using Array.reduce(), creating groups using a key composed of type and location.
Once we have a map keyed on type and location we can use Array.values() to return the desired result:
const diabetics = [ { type: 'type 1', location: 'Boston', email: 'person1#gmail.com' }, { type: 'type 2', location: 'New York', email: 'person2#gmail.com' }, { type: 'type 1', location: 'Boston', email: 'person3#gmail.com' }, { type: 'type 1', location: 'Maine', email: 'person4#gmail.com' }, ]
const result = Object.values(diabetics.reduce ((acc, { type, location, email }) => {
// Create a grouping key, in this case using type and location...
const key = [type, location].join("-");
acc[key] ||= { type, location, email: [] };
acc[key].email.push(email);
return acc;
}, {}))
console.log('Result:', result);
Here's the data:
userRoom = ['rm1'];
data = [{
name: 'building 1',
building: [{
room: 'rm1',
name: 'Room 1'
},{
room: 'rm2',
name: 'Room 2'
}]
},{
name: 'building 2',
building: [{
room: 'rm3',
name: 'Room 3'
}]
},{
name: 'building 3',
building: [{
room: 'rm1',
name: 'Room 1'
}]
}]
What I'm trying to do here is to display the data which it has a building room1 and filter it.
expected output will be like this:
[{
name: 'building 1',
building: [{
room: 'rm1',
name: 'Room 1'
},{
room: 'rm2',
name: 'Room 2'
}]
},{
name: 'building 3',
building: [{
room: 'rm1',
name: 'Room 1'
}]
}]
What I did is
data.map(x => x['building'].filter(y=> userRoom.includes(y.room));
but it doesn't work.
As I mentioned in my comment, use filter > some > includes for the cleanest solution:
userRoom = ['rm1'];
data = [{
name: 'building 1',
building: [
{ room: 'rm1', name: 'Room 1' },
{ room: 'rm2', name: 'Room 2' }
]
}, {
name: 'building 2',
building: [
{ room: 'rm3', name: 'Room 3' }
]
}, {
name: 'building 3',
building: [
{ room: 'rm1', name: 'Room 1' }
]
}];
let filtered = data.filter(i =>
i.building.some(j =>
userRoom.includes(j.room)));
console.log(filtered);
To elaborate on why this works:
You want to filter the data array, as you want so select which of the entries in there you do and don't want to have returned.
To determine which building passes the test required for the filter, some of the rooms in that building need to match a rule...
This rule is that the room's identifier has to be included in the userRoom array, hence the use of includes.
This cant be done with Array.map, because, Array.map always returns a node against each node of the input array. Try making use of any other looping logic.
Array.filter with Array.some will do the trick
const userRoom = ['rm1'];
const data = [{"name":"building 1","building":[{"room":"rm1","name":"Room 1"},{"room":"rm2","name":"Room 2"}]},{"name":"building 2","building":[{"room":"rm3","name":"Room 3"}]},{"name":"building 3","building":[{"room":"rm1","name":"Room 1"}]}];
const output = data.filter((curr) => curr['building'].some(y=> userRoom.includes(y.room)));
console.log(output);
What you have to do is filter the data array with some condition.
Your condition is true, so use that with Array.some. Filter the data with this condition.
Using some, filter and includes
const data = [{"name":"building 1","building":[{"room":"rm1","name":"Room 1"},{"room":"rm2","name":"Room 2"}]},{"name":"building 2","building":[{"room":"rm3","name":"Room 3"}]},{"name":"building 3","building":[{"room":"rm1","name":"Room 1"}]}];
const anyUserRoomInBuilding =
(building, userRoom) => building.some(({room}) => userRoom.includes(room));
const getBuildingsByUserRoom =
(data, userRoom) => data.filter(({building}) => anyUserRoomInBuilding(building, userRoom))
console.log(getBuildingsByUserRoom(data, ['rm1']));
// console.log(getBuildingsByUserRoom(data, ['rm3', 'rm1']));
.as-console-wrapper { max-height: 100% !important; top: 0 }
let filtered = data.filter(i => {
return i.building.filter(j => {
return userRoom.includes(j.room);
}).length > 0;
});
userRoom = ['rm1'];
data = [{
name: 'building 1',
building: [{
room: 'rm1',
name: 'Room 1'
}, {
room: 'rm2',
name: 'Room 2'
}]
}, {
name: 'building 2',
building: [{
room: 'rm3',
name: 'Room 3'
}]
}, {
name: 'building 3',
building: [{
room: 'rm1',
name: 'Room 1'
}]
}]
let filtered = data.filter(i => {
return i.building.filter(j => {
return userRoom.includes(j.room);
}).length > 0;
});
console.log(filtered);
I'm currently working on an inventorying app and I am trying to display the boxID and the amount of items inside of that box on a baseCard component that I have made.
The computed property that I made boxCards needs to spit the data out in this format
[{title: '', amount: null}] so it can be pushed onto each baseCard element.
Presently my computed property is giving me the title, but I cannot figure out how to get the amount of items inside of each box.
boxesInLocation returns this array: ["", "Box 1", "Box 4", "Box 4"]
Which is great, but now I need to count how many times each box shows up in that area and then push it to the reshapedItems function in the amount: spot.
Is this just a simple reduce method that I need to use? Because I've only be able to actually produce a number when calculating the length of the array.
Also, just reducing the array won't spit out the number to each individual instance of reshapedItem
Any ideas on what I can do here?
Cheers!
App.Vue Data:
data(){
userData: {
items: [
{
itemName: 'test book',
category: 'Books',
box: 'Box 3',
location: 'Kitchen',
},
{
itemName: 'test book 2',
category: 'Books',
box: 'Box 3',
location: 'Kitchen',
},
{
itemName: 'test book 3',
category: 'Books',
box: '',
location: 'Basement',
},
{
itemName: 'test record',
category: 'Records',
box: 'Box 1',
location: 'Basement',
},
{
itemName: 'test record 2',
category: 'Records',
box: 'Box 4',
location: 'Basement',
},
{
itemName: 'test furniture',
category: 'Furniture',
box: 'Box 2',
location: 'Garage',
},
{
itemName: 'test movie 1',
category: 'Movies',
box: 'Box 2',
location: 'Garage',
},
{
itemName: 'test movie 2',
category: 'Movies',
box: 'Box 2',
location: 'Garage',
},
{
itemName: 'test movie 3',
category: 'Movies',
box: 'Box 2',
location: 'Garage',
},
{
itemName: 'test Comicbook',
category: 'Movies',
box: 'Box 4',
location: 'Basement',
},
],
boxes: [
{ name: 'Box 1', location: 'Basement' },
{ name: 'Box 2', location: 'Garage' },
{ name: 'Box 3', location: 'Kitchen' },
{ name: 'Box 4', location: 'Basement' },
],
}
Page Component
data() {
return {
items: this.userData.items,
boxes: this.userData.boxes,
}
},
computed: {
boxCards() {
const filteredItems = this.items.filter((item) => item.location === 'Basement')
const itemsInBoxes = filteredItems.map((filteredItem) => {
return filteredItem.box
})
const filteredBoxes = this.boxes.filter((box) => box.location === 'Basement')
const reshapedBoxes = filteredBoxes.map((filteredBox) => {
return { boxID: `${filteredBox.name}`, amount: 100 }
})
return reshapedBoxes
},
You can count the items in the this.items array that have the same box name as the box in question:
return this.boxes
.filter((box) => box.location === 'Basement')
.map((box) => ({
boxId: `${box.name}`,
amount: this.items.filter(item => item.box === box.name).length
}));
You need to save the number of occurrences of each box in a Map, then, get the amount of each one in the second loop you're doing:
const userData = {
items: [
{ itemName: 'test book', category: 'Books', box: 'Box 3', location: 'Kitchen' },
{ itemName: 'test book 2', category: 'Books', box: 'Box 3', location: 'Kitchen' },
{ itemName: 'test book 3', category: 'Books', box: '', location: 'Basement' },
{ itemName: 'test record', category: 'Records', box: 'Box 1', location: 'Basement' },
{ itemName: 'test record 2', category: 'Records', box: 'Box 4', location: 'Basement' },
{ itemName: 'test furniture', category: 'Furniture', box: 'Box 2', location: 'Garage' },
{ itemName: 'test movie 1', category: 'Movies', box: 'Box 2', location: 'Garage' },
{ itemName: 'test movie 2', category: 'Movies', box: 'Box 2', location: 'Garage' },
{ itemName: 'test movie 3', category: 'Movies', box: 'Box 2', location: 'Garage' },
{ itemName: 'test Comicbook', category: 'Movies', box: 'Box 4', location: 'Basement' },
],
boxes: [
{ name: 'Box 1', location: 'Basement' },
{ name: 'Box 2', location: 'Garage' },
{ name: 'Box 3', location: 'Kitchen' },
{ name: 'Box 4', location: 'Basement' },
]
};
function boxCards() {
const boxesCount = userData.items
.reduce((quantity, {box}) =>
quantity.set(box, 1 + (quantity.get(box) || 0))
, new Map);
return userData.boxes
.filter(box =>
box.location === 'Basement'
)
.map(filteredBox =>
({ boxID: filteredBox.name, amount: boxesCount.get(filteredBox.name) })
);
}
console.log( boxCards() );
Say I have two arrays of objects.
const arr1 = [
{
id: '5f6d8fe8f13352002abe77c5',
subject: 'ray#example.com',
location: 'Location 2',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6dacdb49b4bf002a94c73f',
subject: 'ray#example.com',
location: 'Location 3',
timestamp: '2020-08-30T18:00:23.000Z',
},
];
const arr2 = [
{
id: '5f6d8fc3f13352002abe77c1',
subject: 'john.doe#example.com',
location: '101 Dirk Place',
timestamp: '2020-08-31T18:00:23.000Z',
},
{
id: '5f6d8fd9f13352002abe77c3',
subject: 'jane.doe#example.com',
location: 'Location 1',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6d8fe8f13352002abe77c5',
subject: 'ray#example.com',
location: 'Location 2',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6d99c4f45312002af7e343',
subject: 'john#example.com',
location: 'Location 2',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6d99e41edf47002acd3292',
subject: 'johno#example.com',
location: 'Location 3',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6dacdb49b4bf002a94c73f',
subject: 'ray#example.com',
location: 'Location 3',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6d9e9794a701002a6f9924',
subject: 'Marcia14#hotmail.com',
location: 'Bedfordshire kwhlm5 Awesome Concrete Shoes Co',
timestamp: '2020-08-01T23:05:00.000Z',
},
]
How do I get all items in arr2 that have the same location as one of the items in arr1 (i.e. have either 'Location 2' or 'Location 3'), but have a different subject (i.e. does not return items in arr2 that have a subject property of 'ray#example.com'). Preferably through the use of Javascript array functions (map, forEach, filter etc.)
The returned array should be
const arr3 = [
{
id: '5f6d99c4f45312002af7e343',
subject: 'john#example.com',
location: 'Location 2',
timestamp: '2020-08-30T18:00:23.000Z',
},
{
id: '5f6d99e41edf47002acd3292',
subject: 'johno#example.com',
location: 'Location 3',
timestamp: '2020-08-30T18:00:23.000Z',
},
]
Thanks!
I would extract from the first array the properties you are interested in (i.e. location and subject) using the Array.prototype.map() method, and then, filter the second array using the Array.prototype.filter() method with the extracted properties (included or not(!) included) using the Array.prototype.includes() method.
const arr1 = [{"id":"5f6d8fe8f13352002abe77c5","subject":"ray#example.com","location":"Location 2","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6dacdb49b4bf002a94c73f","subject":"ray#example.com","location":"Location 3","timestamp":"2020-08-30T18:00:23.000Z"}];
const arr2 = [{"id":"5f6d8fc3f13352002abe77c1","subject":"john.doe#example.com","location":"101 Dirk Place","timestamp":"2020-08-31T18:00:23.000Z"},{"id":"5f6d8fd9f13352002abe77c3","subject":"jane.doe#example.com","location":"Location 1","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6d8fe8f13352002abe77c5","subject":"ray#example.com","location":"Location 2","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6d99c4f45312002af7e343","subject":"john#example.com","location":"Location 2","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6d99e41edf47002acd3292","subject":"johno#example.com","location":"Location 3","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6dacdb49b4bf002a94c73f","subject":"ray#example.com","location":"Location 3","timestamp":"2020-08-30T18:00:23.000Z"},{"id":"5f6d9e9794a701002a6f9924","subject":"Marcia14#hotmail.com","location":"Bedfordshire kwhlm5 Awesome Concrete Shoes Co","timestamp":"2020-08-01T23:05:00.000Z"}];
const locations = arr1.map(item => item.location);
const subjects = arr1.map(item => item.subject);
const filteredArray = arr2.filter(
item => locations.includes(item.location) && !subjects.includes(item.subject)
);
console.log(filteredArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have an array that contains a number of objects which are almost identical. I want to merge these objects into one, while keeping the data that is not the same between them.
Here's my data:
[
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'}
]
},
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 2, name: 'Cat 2'}
]
}
]
I want the final result to be:
[
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'},
{id: 2, name: 'Cat 2'}
]
}
]
Any help would be appreciated!
var a1 = [
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'}
]
},
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 2, name: 'Cat 2'}
]
}
];
var a2 = [];
_.forEach(a1, function(item){
if(a2.length === 0){
a2.push(item);
}else{
_.forIn(item, function(value, key){
if(!a2[0].hasOwnProperty(key)){
a2[0][key] = value;
}else{
if(typeof value === "object" && value.length > 0){
_.forEach(value, function(v){
console.log("Pushing Item into Categories")
a2[0][key].push(v);
})
}
}
})
}
})
console.log(a2)
This is not the most elegant solution, however it gets the job done, and will combine any length array of "a1" into an array with length of 1 Object, and combine any nested arrays within the items it iterates over.
So, it could work on the following array as well... just for sake of example:
var a1 = [
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 1, name: 'Cat 1'}
],
franks: [
{"blaH":"blah"}
]
},
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 2, name: 'Cat 2'}
] ,
franks: [
{"blaH":"blah1"}
]
} ,
{ id: 1,
title: 'The title',
description: 'The description',
categories: [
{id: 2, name: 'Cat 2'}
] ,
franks: [
{"blaH":"blah2"},
{"blaH":"blah3"}
]
}
];