Check whether an array contains some data - laravel

I have 2 variables, users and friend inside a component
Variable users is an array. Each iteration contains id, name and email
Variable friend have same structure in addition to pivot.
In my components file (.vue file), I need to check whether users contains friend without using loops so that I can use it inside the v-if condition.
Note: email and id are unique values.
How can I check it? Single line code will be very helpful.
Eg:
users have 2 iterations
id: 1
name: First
email: first#example.com
id: 2
name: Second
email: second#example.com
friend is like this
id: 2
name: Second
email: second#example.com
pivot: {...}

Use _.find() lodash function
_.find(users, {id: friend.id});
The first parameter is the array which you want to check and the second is the condition to satisfy. Remember this will return the specific iteration of the array instead of true or false but you can use it as a condition. If it returns null then it will take it as false, if there is something then it will be taken as true.

Try using v-if:
<div v-if=":user.friend"></div>

Related

GraphQL: Validating array elements are in list

I have the following argument in my Ruby GQL:
argument :countryCode, [String], required: false, validates: {inclusion: {in: COUNTRY_CODES}}, prepare: :strip
What I want this to achieve is to allow an array of Strings to be used, and each value in the array to be one of COUNTRY_CODES
However, this returns the exception "is not included in the list". What is wrong here?
Better way for this case create enum with countries, but in this case client must send enum hardcoded country code, other way use validator like dry-validation of similar

Cypress - counting number of elements in an array that contain a specific string

Attempting to confirm that of all the schema in the head of a page exactly 3 of them should have a specific string within them. These schemas have no tags or sub classes to differentiate themselves from each other, only the text within them. I can confirm that the text exists within any of the schema:
cy.get('head > script[type="application/ld+json"]').should('contain', '"#type":"Product"')
But what I need is to confirm that that string exists 3 times, something like this:
cy.get('head > script[type="application/ld+json"]').contains('"#type":"Product"').should('have.length', 3)
And I can't seem to find a way to get this to work since .filter, .find, .contains, etc don't filter down the way I need them to. Any suggestions? At this point it seems like I either need to import a custom library or get someone to add ids to these specific schema. Thanks!
The first thing to note is that .contains() always yields a single result, even when many element match.
It's not very explicit in the docs, but this is what it says
Yields
.contains() yields the new DOM element it found.
If you run
cy.get('head > script[type="application/ld+json"]')
.contains('"#type":"Product"')
.then(console.log) // logs an object with length: 1
and open up the object logged in devtools you'll see length: 1, but if you remove the .contains('"#type":"Product"') the log will show a higher length.
You can avoid this by using the jQuery :contains() selector
cy.get('script[type="application/ld+json"]:contains("#type\": \"Product")')
.then(console.log) // logs an object with length: 3
.should('have.length', 3);
Note the inner parts of the search string have escape chars (\) for quote marks that are part of the search string.
If you want to avoid escape chars, use a bit of javascript inside a .then() to filter
cy.get('script[type="application/ld+json"]')
.then($els => $els.filter((index, el) => el.innerText.includes('"#type": "Product"')) )
.then(console.log) // logs an object with length: 3
.should('have.length', 3);

Assign value to a variable using || operator with if condition in Ruby

I am trying to write logic for a search query. There are many different conditions with different parameters. One parameter sent from form is code. So there are code values in two different tables: competitions and responses. What I need is to check the params[:code] value first in competitions table and if it does not exist then check in responses table. If it does not exist in either table then it should return nil. I am trying to write it in a single if statement. The code I tried is below:
competitions = Competition.includes(:event, :responses)
if params[:code].present?
competitions = (competitions.where(code: params[:code])) ||
(competitions.joins(:responses).where(responses: { code: params[:code] }))
The above code checks only the value of competitions.where(code: params[:code]). If that value is [], then it is not evaluating the second condition. What changes should I do to make the above code work as per the requirements mentioned above?
competitions.where(code: params[:code]) returns a Relation object which is always truthy.
Luckily enough, it implements #presence method, returning either the value if it’s not blank, or nil. So, this should work:
competitions.where(code: params[:code]).presence || ...

MS Flow: How to achieve something like `_.find()` (lodash/JS)

How can I use MS Flow to select an individual object, by value for a specified property, from an array?
Example array:
[
{
item_id: '1234'
},
{
item_id: '4567'
}
]
In the example above, I may only want to work with the first object and the rest of its available properties.
Happy to use the Workflow Definition Language and/or any of the Data Operations actions.
I solved this by using the "Data operations - Filter" action.
Ignore the error in red - it is an array.
My left-hand expression for "item_id" is:
item()?['item_id']
And then I statically enter the item ID I wish to access in the right-hand input.
DocumentNo Item will then be an array itself with only 0 or 1 elements and can be used like so:
body('DocumentNo_Item')?[0]?['label']

jasmine - how to check if an argument exists?

good day.
im testing to see function got all her args.
i know what value two of her args must have,
but for the third arg, i just want to test if it exists.
expect(myFunction).toHaveBeenCalledWithMatcher({
a: 1,
b: 2,
c: dont know its val but want it to exist
});
thanks in advance
You can also use jasmine.any. In case you expect a number it could be:
expect(myFunction).toHaveBeenCalledWith({
a: 1,
b: 2,
c: jasmine.any(Number)
});
It is also possible jasmine.any(Function) and so on. From Jasmine doc:
jasmine.any takes a constructor or “class” name as an expected value. It returns true if the constructor matches the constructor of the actual value.
Try
expect(myFunction.mostRecentCall.args[2]).toBeDefined();
and leave out the argument in the toHaveBeenCalledWith test.

Resources