I want to combine two arrays (ranking and matches) that has common properties:
var ranking = [{
def: "0.58",
league: "Scottish Premiership",
name: "Celtic",
off: "3.33",
grank: "3",
tform: "96.33",
},
{
def: "2.52",
league: "Scottish Premiership",
name: "Dundee",
off: "1.28",
grank: "302",
tform: "27.51",
}]
var matches = [{
date: "2010-04-22",
league: "Scottish Premiership",
home: "0.0676",
away: "0.8",
draw: "0.1324",
goals1: "3",
goals2: "1",
tform1: "96.33",
tform2: "27.51",
team1: "Celtic",
team2: "Dundee",}]
Expected output looks like this:
[{
date: "2010-04-22",
league: "Scottish Premiership",
home: "0.0676",
away: "0.8",
draw: "0.1324",
goals1: "3",
goals2: "1",
tform1: "96.33",
tform2: "27.51",
def1: "0.58",
def2: "2.52",
off1: "3.33",
off2: "1.28",
grank1: "3",
grank2: "302",
team1: "Celtic",
team2: "Dundee",}]
To merge the arrays, I used Lodash _.merge function
var result = _.merge(ranking, matches);
The output it returned did merge some objects and omitted homogeneous objects.
Please I need some help and insight in achieving this task. I wouldn't mind any javascript (client-side) solution.
You need to merge by the given data and add the information of the two teams.
const
keys = ['def', 'off', 'grank'],
getRanking = (team, suffix) => Object
.fromEntries(keys.map(k => [k + suffix, team[k]])),
ranking = [{ def: "0.58", league: "Scottish Premiership", name: "Celtic", off: "3.33", grank: "3", tform: "96.33" }, { def: "2.52", league: "Scottish Premiership", name: "Dundee", off: "1.28", grank: "302", tform: "27.51" }],
matches = [{ date: "2010-04-22", league: "Scottish Premiership", home: "0.0676", away: "0.8", draw: "0.1324", goals1: "3", goals2: "1", tform1: "96.33", tform2: "27.51", team1: "Celtic", team2: "Dundee" }],
teams = Object.fromEntries(ranking.map(o => [o.name, o])),
result = matches.map(o => ({
...o,
...getRanking(teams[o.team1], 1),
...getRanking(teams[o.team2], 2)
}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
I'm trying to combine the "role" parameter for "Project" objects with the same "id" and "title" parameters in the myProjects array below:
struct Project: Identifiable {
var id: String
var title: String
var role: String
}
var myProjects = [Project(id: "1", title: "Sunset", role: "2nd AD"),
Project(id: "2", title: "Lights", role: "Mix Tech"),
Project(id: "2", title: "Lights", role: "Sound Mixer"),
Project(id: "3", title: "Beach", role: "Producer")]
var updatedProjects: [Project] = []
// The goal is to update myProjects to show the following:
updatedProjects = [Project(id: "1", title: "Sunset", role: "2nd AD"),
Project(id: "2", title: "Lights", role: "Mix Tech & Sound Mixer"),
Project(id: "3", title: "Beach", role: "Producer"]
This is what I have attempted so far, the result is giving me duplicate combinations of the roles parameter for each project in the myProjects array.
var dupProjects = myProjects
for myProject in myProjects {
for dupProject in dupProjects {
if myProject.id == dupProject.id {
let combinedRoles = "\(myProject.role) & \(dupProject.role)"
updatedProjects.append(Project(id: myProject.id,
title: myProject.title,
role: combinedRoles))
}
}
}
print(updatedProjects)
// [__lldb_expr_48.Project(id: "1", title: "Sunset", role: "2nd AD & 2nd AD"),
__lldb_expr_48.Project(id: "2", title: "Lights", role: "Mix Tech & Mix Tech"),
__lldb_expr_48.Project(id: "2", title: "Lights", role: "Mix Tech & Sound Mixer"),
__lldb_expr_48.Project(id: "2", title: "Lights", role: "Sound Mixer & Mix Tech"),
__lldb_expr_48.Project(id: "2", title: "Lights", role: "Sound Mixer & Sound Mixer"),
__lldb_expr_48.Project(id: "3", title: "Beach", role: "Producer & Producer")]
You can use a dictionary to group them by id, combine the roles, then convert the group back to a single Project
let combined = Dictionary(grouping: myProjects) { element in
return element.id
}.compactMapValues { projects -> Project? in
var first = projects.first
first?.role = projects.map { $0.role }.joined(separator: " & ")
return first
}.values.map { $0 }
my data :
const DATA = [
{
id: "1",
from: "Canada",
name: "person1",
},
{
id: "2",
from: "Canada",
name: "person2",
},
{
id: "3",
from: "France",
name: "person3",
}];
i need to filter this flatList and only display people from Canada
I think you are looking for something like:
const result = DATA.filter(element=> element.from === "Canada")
Then you can use your result to display it in the format that you want.
considering the following data
let dataset =
[
{
"Category": "Auto",
"Parent": "Automotive",
"name": "Auto - Multi Purpose Vehicles",
"High": "10",
"Low": "15",
"Medium": "5"
},
{
"Category": "Auto",
"Parent": "Automotive",
"name": "Auto - Off road",
"High": "1",
"Low": "2",
"Medium": "3"
},
{
"Category": "Auto",
"Parent": "Ecology & Environment",
"name": "Vegetarian",
"High": "5",
"Low": "5",
"Medium": "10"
},
{
"Category": "Home",
"Parent": "Ecology & Environment",
"name": "Weather",
"High": "10",
"Low": "15",
"Medium": "5"
}]
I want to group data in the following way
[{
name: "Auto",
data: [{
name: "Automotive",
data: [{
name: "Auto - Multi Purpose Vehicles",
value: 30
High: 10,
Low: 15,
Medium: 5
},
{
name: "Auto - Off road",
value: 6
High: 1,
Low: 2,
Medium: 3
}]
}, {
name: 'Ecology & Environment',
data: [{
name: "Vegetarian",
value: 20
High: 5,
Low: 5,
Medium: 10
}]
}],
name: "Home",
data: [{
name: "Ecology & Environment",
data: [{
name: "Weather",
value: 30
High: 10,
Low: 15,
Medium: 5
}]
}]
}]
What I've tried so far never land me to the desired output.
here is where I'm stacked.
let cat = []
dataset.forEach((d) => {
const { Category, Parent, name, High, Medium, Low, value } = d;
let category = cat.find((item) => item.Category === Category);
let parent = cat.find((item) => item.data.Parent === Parent);
if (!category) {
cat.push({
Category,
data: []
})
} else if (!parent ) {
cat.forEach(i => {
i.data.push({
Parent,
High,
Medium,
Low
})
})
}
})
I've tried several ways with different results but I'm not able to figure out how to handle it
I'm not an expert in javascript. I'm trying to learn by doing but sometimes i get stack
Many thanks for any help
P.
In order to get the answer you want, follow this code.
step 1:
fire get the unique category from the dataset
step 2:
after getting a unique category, look for the particular category into a dataset, and when a category is found push it into the array
follow below code snippet
let dataset = [
{
Category: "Auto",
Parent: "Automotive",
name: "Auto - Multi Purpose Vehicles",
High: "10",
Low: "15",
Medium: "5",
},
{
Category: "Auto",
Parent: "Automotive",
name: "Auto - Off road",
High: "1",
Low: "2",
Medium: "3",
},
{
Category: "Auto",
Parent: "Ecology & Environment",
name: "Vegetarian",
High: "5",
Low: "5",
Medium: "10",
},
{
Category: "Home",
Parent: "Ecology & Environment",
name: "Weather",
High: "10",
Low: "15",
Medium: "5",
},
];
/* map function help us to get non unique
categoty like => [ 'Auto', 'Auto', 'Auto', 'Home' ]
`New Set()` object gave us unique array then
we create new array using spread oprator */
let uniqueCategory = [...new Set(dataset.map((data) => data.Category))];
let uniqueParent = [...new Set(dataset.map((data) => data.Parent))];
// create new array for storing final output
let cat = [];
// loop over unique category
uniqueCategory.forEach((Category) => {
// create new object for perticular category
let obj = {
name: Category,
data: [],
};
uniqueParent.forEach((parent) => {
let isDataEmpty = true;
let parentObj = {
name: parent,
data: [],
};
// loop over dataset
dataset.map((data) => {
/* if we fount category same as unique category we have
to push that data into particular category object */
if (data.Category === Category && data.Parent === parent) {
// delete category ket/value from object
isDataEmpty = false;
delete data.Category;
parentObj.data.push(data);
}
});
if (!isDataEmpty) obj.data.push(parentObj);
});
cat.push(obj);
});
console.log(cat);
I made the following filter in hopes that I would be combining the results from all 3 $and arrays but it is only matching one of those blocks.
How can I combine the results of what would be returned from each $and array if conditions are met. Hopefully that's clear. I don't know what to call the $and array.
const filter = {
$or: [
{
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
name: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
description: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
tags: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
},
],
};
await Content.paginate(filter, options, (err, result) => {
if (err) {
res.status(500).send(err);
} else {
res.json(result);
}
});
EDIT: Below is an example of two entries that would be found in the database. The way it should work is it should use category, subCategory, and contentType to filter out the entries in the database so that what I have now are only the entries which have the same category, subCategory, and contentType as specified in req.query, I'll call this the firstFilterResult. From there, I am trying to search within firstFilterResult to see if I have entries that have name, tag, or description matches. So basically catgeory, subCategory and contentType are just used to narrow down the results so that I can find matches for name, tag, and description. My code above doesn't do exactly this but this is the idea behind it and I thought that what I have would do similar, but I guess I'm wrong.
contents: [
{
tags: [
'food',
'drinks',
'card',
'account'
],
_id: '1d13ff7m6db4d5417cd608f4',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'In here I will begin to talk about...',
content_id: '5dbcb998ad4144390c244093',
contentType: 'quiz',
date: '2019-06-03T04:00:00.000Z',
category: 'food',
image: 'https://IMAGE.PNG',
__v: 0
},
{
tags: [
'computer',
'laptop'
],
_id: '7d1b940b1c9d44000025db8c',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'This is another description',
content_id: '5f1b963d1c9d44000055db8d',
contentType: 'tool',
date: '2019-06-03T04:00:00.000Z',
category: 'money',
image: 'https://IMAGE.PNG',
__v: 0
}
]
I finally got it to work with this
const catFilter =
req.query.category !== "" ? { category: req.query.category } : {};
const subCatFilter =
req.query.subCategory !== "" ? { tags: req.query.subCategory } : {};
const typeFilter =
req.query.contentType !== ""
? { contentType: req.query.contentType }
: {};
const filter = {
$and: [
{
$or: [
{
name: {
$regex: req.query.searchTerm,
$options: "i",
},
},
{
description: {
$regex: req.query.searchTerm,
$options: "i",
},
},
{
tags: {
$regex: req.query.searchTerm,
$options: "i",
},
},
],
},
catFilter,
subCatFilter,
typeFilter,
],
};
Since each element of the $or contains the same 3 checks with a single one that varies, these can be separated out, and the $or is then only needed if a search term is specified.
Passing options:"i" makes the entire regex match case insensitive, so it is not necessary to surround the search string with (?i) and (?-i)
The following should build the filter that you are attempting, without using empty objects:
// base query that checks the common fields
var filter = {
category: req.query.category,
tags: req.query.subCategory,
contentType: req.query.contentType
};
// if a search term is provided, add in the additional critera
if (req.query.searchTerm !== "") {
var regex = {
$regex: req.query.searchTerm,
options:"i"
};
filter['$or'] = [
{ name: regex },
{ description: regex },
{ tags: regex }
]
}
If this doesn't obtain the results you're after, please edit the question and add in some sample documents so we can see the problem.
I have a sorted array
let things = [
Thing(activity: "1", name: "value1"),
Thing(activity: "1", name: "value2"),
Thing(activity: "1", name: "value3"),
Thing(activity: "2", name: "value4"),
Thing(activity: "2", name: "value5"),
Thing(activity: "3", name: "value6"),
Thing(activity: "3", name: "value7"),
Thing(activity: "1", name: "value8"),
Thing(activity: "1", name: "value9"),
Thing(activity: "1", name: "value10")
]
I would like to produce array of arrays splitted when the activity value changes similar to the following
[[Thing(activity: "1", name: "value1"),
Thing(activity: "1", name: "value2"),
Thing(activity: "1", name: "value3")],
[Thing(activity: "2", name: "value4"),
Thing(activity: "2", name: "value5")],
[Thing(activity: "3", name: "value6"),
Thing(activity: "3", name: "value7")],
[Thing(activity: "1", name: "value8"),
Thing(activity: "1", name: "value9"),
Thing(activity: "1", name: "value10")]]
A generalized solution would be:
extension Sequence {
func grouped<T: Equatable>(by block: (Element) throws -> T) rethrows -> [[Element]] {
return try reduce(into: []) { result, element in
if let lastElement = result.last?.last, try block(lastElement) == block(element) {
result[result.index(before: result.endIndex)].append(element)
} else {
result.append([element])
}
}
}
}
Then you can do:
let results = things.grouped { $0.activity }
A less elegant (but slightly more efficient) solution would be:
extension Sequence {
func grouped<T: Equatable>(by block: (Element) throws -> T) rethrows -> [[Element]] {
var results: [[Element]] = []
var lastValue: T?
var index = results.endIndex
for element in self {
let value = try block(element)
if let lastValue = lastValue, lastValue == value {
results[index].append(element)
} else {
results.append([element])
index = results.index(before: results.endIndex)
lastValue = value
}
}
return results
}
}
As already mentioned by #matt in comments you can use collection method reduce(into:) to group your elements by checking if the activity of the last element of the last array is equal to the current element activity, if so just append a new element to the last array, otherwise append a new array with a single element to the outer array:
struct Thing {
let activity, name: String
}
let things: [Thing] = [
.init(activity: "1", name: "value1"),
.init(activity: "1", name: "value2"),
.init(activity: "1", name: "value3"),
.init(activity: "2", name: "value4"),
.init(activity: "2", name: "value5"),
.init(activity: "3", name: "value6"),
.init(activity: "3", name: "value7"),
.init(activity: "1", name: "value8"),
.init(activity: "1", name: "value9"),
.init(activity: "1", name: "value10")]
let grouped: [[Thing]] = things.reduce(into: []) {
$0.last?.last?.activity == $1.activity ?
$0[$0.index(before: $0.endIndex)].append($1) :
$0.append([$1])
}
print(grouped) // "[[__lldb_expr_1.Thing(activity: "1", name: "value1"), __lldb_expr_1.Thing(activity: "1", name: "value2"), __lldb_expr_1.Thing(activity: "1", name: "value3")], [__lldb_expr_1.Thing(activity: "2", name: "value4"), __lldb_expr_1.Thing(activity: "2", name: "value5")], [__lldb_expr_1.Thing(activity: "3", name: "value6"), __lldb_expr_1.Thing(activity: "3", name: "value7")], [__lldb_expr_1.Thing(activity: "1", name: "value8"), __lldb_expr_1.Thing(activity: "1", name: "value9"), __lldb_expr_1.Thing(activity: "1", name: "value10")]]\n"