Lodash findWhere with Object as value of key - lodash

I have a list of objects and i am trying to use underscore findwhere function to find an object in the list with a key and another objects as its value. I have tried doing this but get an undefined value. So my question is underscore findwhere available to find objects with a key that has a value of another object, rather than a string or number. Sample code.
var a = [
{channel: {aa: "1", bb: "2"}, id: 233332},
{channel: {aa: "3", bb: "4"}, id: 822211}
]
var b = {channel: {aa: "1", bb: "2"}, id: 233332}
_.findWhere(a, b) should return {channel: {aa: "1", bb: "2"}, id: 233332} which it does for this array of objects with two keys but with a more populated object i.e one that has more keys and values it doesn't seem to work, are there any gotchas with findWhere or any things i didn't consider?

_.findWhere is deprecated in lodash v4.0.0, so it's better to use _.find with _.matches.
lodash _.matches
var a = [
{channel: {aa: "3", bb: "4"}, id: 822211},
{channel: {aa: "1", bb: "2"}, id: 233332}
];
var b = {channel: {aa: "1", bb: "2"}, id: 233332, desc:"Hello"};
_.find(a, function(n){
if(_.matches(n)(b)){
return n;
}
});
jsFiddle with lodash v3.1.0
Now if you want other way around
var a = [
{channel: {aa: "3", bb: "4"}, id: 822211, desc:"Hello"},
{channel: {aa: "1", bb: "2"}, id: 233332, desc:"Hello"}
];
var b = {channel: {aa: "1", bb: "2"}, id: 233332};
_.find(a, function(n){
if(_.matches(b)(n)){
return n;
}
});
jsFiddle with lodash v3.1.0

The .findWhere method matches all properties from object b. If you add extra properties, the match will fail. What you can do however is the following:
_.findWhere(a, _.pick(b, ['channel', 'id']));
The _.pick will create a new object, with only those key/value pairs that are also available in your array a.

Related

Filtering out objects from multiple arrays in a JSONB column

I have a JSON structure with two arrays saved in a JSONB column. A bit simplified it looks like this
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
Schema:
CREATE TABLE campaign
(
id TEXT PRIMARY KEY,
data JSONB
);
Since each row (data column) can be fairly large, I'm trying to filter out matching item objects and group objects from the items and groups arrays.
My current query is this
SELECT * FROM campaign
WHERE
(data -> 'items' #> '[{"productId": "123"}]') OR
(data -> 'groups' #> '[{"groupId": "B"}]')
which returns rows containing either the matching group or the matching item. However, depending on the row, the data column can be a fairly large JSON object (there may be hundreds of objects in items and tens in groups and I've omitted several keys/properties for brevity in this example) which is affecting query performance (I've added GIN indexes on the items and groups arrays, so missing indices is not why it's slow).
How can I filter out the items and groups arrays to only contain matching elements?
Given this matching row
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}
I'd like the result to be something like this (the matching item/group could be in different columns from the rest of the data column - doesn't have to be returned in a single JSON object with two arrays like this, but I would prefer it if doesn't affect performance or lead to a really hairy query):
{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
}
],
"groups": [
{
"groupId": "B"
"discount": "20",
"discountId": "3"
}
]
}
What I've managed to do so far is unwrap and match an object in the items array using this query, which removes the 'items' array from the data column and filters out the matching item object to a separate column, but I'm struggling to join this with matches in the groups array.
SELECT data - 'items', o.obj
FROM campaign c
CROSS JOIN LATERAL jsonb_array_elements(c.data #> '{items}') o(obj)
WHERE o.obj ->> 'productId' = '124'
How can I filter both arrays in one query?
Bonus question: For the groups array I also want to return the object with the lowest discount value if possible. Or else the result would need to be an array of matching group objects instead of a single matching group.
Related questions: How to filter jsonb array elements and How to join jsonb array elements in Postgres?
If your postgres version is 12 or more, you can use the jsonpath language and functions. The query below returns the expected result with the subset of items and groups which match the given criteria. Then you can adapt this query within a sql function so that the search criteria is an input parameter.
SELECT jsonb_set(jsonb_set( data
, '{items}'
, jsonb_path_query_array(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")'))
, '{groups}'
, jsonb_path_query_array(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")'))
FROM (SELECT
'{
"prop1": "abc",
"prop2": "xyz",
"items": [
{
"itemId": "123",
"price": "10.00"
},
{
"itemId": "124",
"price": "9.00"
},
{
"itemId": "125",
"price": "8.00"
}
],
"groups": [
{
"groupId": "A",
"discount": "20",
"discountId": "1"
},
{
"groupId": "B",
"discount": "30",
"discountId": "2"
},
{
"groupId": "B",
"discount": "20",
"discountId": "3"
},
{
"groupId": "C",
"discount": "40",
"discountId": "4"
}
]
}' :: jsonb) AS d(data)
WHERE jsonb_path_exists(data, '$.items[*] ? (#.itemId == "123" && #.price == "10.00")')
AND jsonb_path_exists(data, '$.groups[*] ? (#.groupId == "B" && #.discount == "20" && #.discountId == "3")')

find object in nested array with lodash

I have json data similar to this:
{
"Sections": [
{
"Categories": [
{
"Name": "Book",
"Id": 1,
"Options": [
{
"Name": "AAAA",
"OptionId": 111
},
"Selected": 0
},
{
"Name": "Car",
"Id": 2,
"Options": [
{
"Name": "BBB",
"OptionId": 222
},
"Selected": 0
},
],
"SectionName": "Main"
},
... more sections like the one above
]
}
Given this data, I want to find a category inside a section based on its (Category) Id, and set its selected option, I tried this, but couldn't get it to work....Note Category Id will be unique in the whole data set.
_.find(model.Sections, { Categories: [ { Id: catId } ]});
According to your data model, it looks like you're trying to find an element that is inside a matrix: Sections can have multiple Categories and a Category can have multiple types (car, book...).
I'm afraid there isn't a function in lodash that allows a deep find, you'll have to implement it the 'traditional' way (a couple of fors).
I provide this solution that is a bit more 'functional flavoured' than the traditional nested fors. It also takes advantage of the fact that when you explicitly return false inside a forEach, the loop finishes. Thus, once an element with the provided id is found, the loop is ended and the element returned (if it's not found, undefined is returned instead).
Hope it helps.
const findCategoryById = (sections, id) => {
var category;
_.forEach(sections, (section) => {
category = _.find(section.Categories, ['Id', id]);
return _.isUndefined(category);
});
return category;
};
const ex = {
"Sections": [{
"Categories": [{
"Name": "Book",
"Id": 1,
"Options": [{
"Name": "AAAA",
"OptionId": 111
}],
"Selected": 0
},
{
"Name": "Car",
"Id": 2,
"Options": [{
"Name": "BBB",
"OptionId": 222
}],
"Selected": 0
}
],
"SectionName": "Main"
}]
};
console.log(findCategoryById(ex.Sections, 2));
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.5/lodash.min.js"></script>

How to iterate and render my fetch result in react native JSX

{
"status": true,
"live_score_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"live_score_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"results_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12",
"matches": [
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "5",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:45:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "15",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
},
{
"cVenueCode": "TTAB",
"cTableName": "Table 1",
"cEventType": "Junior Boys",
"cMatchNo": "4",
"cRound": "First Round",
"nScheduledDate": "2016-12-11",
"nScheduledTime": "11:30:00",
"teamname1": "MOTHER SCHOOL",
"teamname2": "HARI SHEWA SCHOOL",
"nVenueID": "1",
"nTableID": "1",
"nTeamID1": "3",
"nTeamID2": "4",
"nTournamentID": "1",
"nFixtureDetailsID": "14",
"nEventTypeID": "5",
"image": "http://example.com/tt.png"
}
]
}
],
"results_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24",
"matches": []
}
],
"fixture_point_domestic": [
{
"nTournamentID": "1",
"cTournamentName": "sample tournament.",
"cTournamentType": "D",
"dStartDate": "2016-12-10",
"dEndDate": "2016-12-12"
}
],
"fixture_point_international": [
{
"nTournamentID": "2",
"cTournamentName": "International Tournament Sample",
"cTournamentType": "I",
"dStartDate": "2016-12-22",
"dEndDate": "2016-12-24"
}
]
}
This is the fetch result.I want to render first a heading like domestic Tournaments.Then i want to loop for al tornaments in domestic.Then all matches in each tournament.How i can do this?Anyone to help.thanks in advance :)
can you please help me to figure out how to iterate all
First of all you should decide on how the data should be displayed on the screen. Should they be clickable or not, scrolled or not, what part of screen they should take etc. And depending on the visual design of your future application you can make a choice what exact react-native visual component will represent the data on the screen by the best way.
For example, if it is applicable by visual design of your application you can take a look at the react-native ListView or ScrollView components to render endless list of identical complex data.
If iterating through that entire JSON object is your intent, then an easy approach is a for...in loop.
For example:
var obj = {a: 1, b: 2, c: {a: 1, b: 2}};
function walk(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var val = obj[key];
console.log(val);
walk(val);
}
}
}
walk(obj);
Taken from: iterating through json object javascript
Object.keys(data).map((key) => { ... })
This will use the object properties (live_score_domestic, live_score_international, etc) to iterate through the top most data. Then you can use data[key] to get throught its contents.

Inserting and removing values within a collection array

I'm new to working with MongoDb using Express. I currently have a collection that has an array within an object. The array is meant to hold an unlimited number of values.
My question is when I add a new item to that array in the collection, do I always have to pass all the values in the object?
For example, with the following collection. Say I wanted to add a new contact.
{
"owner": "Tom Smith",
"age": "29",
"contacts": [
{
"firstname": "Fred",
"lastname": "Anderson",
"age": "22"
},
{
"firstname": "Linda",
"lastname": "Smith",
"age": "32"
},
{
"firstname": "Tom",
"lastname": "James",
"age": "42"
},
{
"firstname": "Cal",
"lastname": "Hallaway",
"age": "57"
}
],
"city": "New York"
}
Do I need to explicitly declare all my values in the object I pass to the end point?
Example:
obj.owner = 'Tom Smith';
obj.age = '29';
obj.contacts.firstname = 'Fred';
obj.contacts.lastname = 'Anderson';
obj.contacts.age = '22';
... etc.
and then add my new contact and push the full object to the endpoint to update?
Is there a way that I can just add a new contact without pushing all the data that already exists in the collection?
To add a new data in a nested attribute array:
Model.findOneAndUpdate({
_id: 'THE ID OF YOUR GYUS'
}, {
$push: {
contacts: {
firstname: 'TOTO',
lastname: 'TITI',
age: 42,
},
},
});

lodash casting, mapping and picking

I have an array of objects that I am simplifying as this:
var expenses = [
{
arm: "0",
cat_id: "1",
crop: {
crop: "corn",
id: 1
},
crop_id: "1",
dist: "164.97",
expense: "Fertilizer",
id: "1",
loan_id: "1"
},
{
arm: "20",
cat_id: "8",
crop: {
crop: "corn",
id: 1
},
crop_id: "1",
dist: "0",
expense: "Labor",
id: "8",
loan_id: "1"
}
];
I am trying to end up with this:
var expenses = [{
arm: 0,
cat_id: 1,
crop: "corn",
crop_id: 1,
dist: 164.97,
expense: "Fertilizer",
id: 1,
loan_id: 1
},{
arm: 20,
cat_id: 6,
crop: "corn",
crop_id: 1,
dist: 0,
expense: "Labor",
id: 1,
loan_id: 1
}];
I can get certain pieces in that direction but can't pull it all together without error. I can't find out how to cast the values to float or put crop INSIDE of stub because casted returns all nulls. I currently have this:
flattened = _.map(expenses, function(item){
var crop = item.crop.crop;
var stub = _.pick(item, [
'id',
'loan_id',
'cat_id',
'expense',
'crop_id',
'arm',
'dist'
]);
var casted = _.map(stub, function(i){
i.crop = crop;
return i;
});
return stub;
});
Any help is appreciated.
Problem 1: I can't find out how to cast the values to float
This should be easily fixed by using parseFloat.
e.g.
item.dist = parseFloat(item.dist);
Problem 2: put crop INSIDE of stub because casted returns all nulls
Since you're already using lodash, might as well get used to their chaining feature (lazy evaluation).
DEMO
var flattened = _.map(expenses, function(item) {
item.dist = parseFloat(item.dist);
return _(item)
.omit('crop')
.assign(_.omit(item.crop, 'id'))
.value();
});
The solution above maps the entire expenses array, converting item.dist to a floating point value and then flattening the values from the item.crop object towards the item object with the exception of the item.crop.id value.
Note: In regards to your solution above, using _.map in an object
results to an array.
My attempt for my own learning purposes based on #ryeballar code.
var flattened = _.map(expenses, function(item) {
return _(item)
.set('crop', item.crop.crop) // set item.crop
.set('dist', parseFloat(item.dist)) // set dist as (float)item.dist
.value();
});