Ramda remove objects from array based on nested value - ramda.js

I'm trying to remove an object from an array of objects if a certain value exists in nested data.
The data being returned from the API is shaped like this:
Array [
Object {
"id": "/db/Shifts/123",
"applicants": Object {
"applicants": Array [
"/db/User/12",
"/db/User/13",
],
},
Object {
"id": "/db/Shifts/456",
"applicants": Object {
"applicants": Array [
"/db/User/12",
"/db/User/14",
],
},
Object {
"id": "/db/Shifts/789",
"applicants": Object {
"applicants": Array [
"/db/User/13",
"/db/User/14",
],
},
]
Using Ramda, how would I filter out the shifts where User 12 exists in the array of applicants, which would be located at applicants.applicants.
I am not able to flatten the data in this case, the list of applicants for each shift does have to be an array contained in an object.
I tried this:
var hasApplied = pathEq(['applicants', 'applicants'], 'db/User/12');
console.log(filter(hasApplied, shifts));
But I don't think that is quite right because applicants.applicants is an array, I feed like I need to be feeding it one more function to get into the array of applicants but I'm not sure what.

Your use of R.pathEq is resulting in the user ID being compared with each array of IDs for equality, rather than checking whether each array contains the given ID.
You could instead make use of R.pathSatisfies along with R.contains
const hasApplied = R.pathSatisfies(R.contains('/db/User/12'), ['applicants', 'applicants'])

Related

how can i filter a array in Vue3

I am new to Vue3.
I have to filter a array with same item and push results into another array
My array is
let list = [
{"name":"1000","properties":{"item":"1","unit":"DZN"}},
{"name":"2000","properties":{"item":"1","unit":"CTN"}},
{"name":"3000","properties":{"item":"2","unit":"DZN"}},
{"name":"4000","properties":{"item":"3","unit":"CTN"}}
]
I need corresponding name with condition item =1 in another array.
Result array will be similar to [{"name":"1000", "unit":"DZN"}, {"name":"2000", "unit":"CTN"}]
TIA
If I understand correctly you want to create an array of objects with name and unit attributes from records which have item values equal to 1. To do that you have to first filter an array to get only records which have desired value of item and then map those values to create a new array of objects.
Here is an example:
let list = [
{"name":"1000","properties":{"item":"1","unit":"DZN"}},
{"name":"2000","properties":{"item":"1","unit":"CTN"}},
{"name":"3000","properties":{"item":"2","unit":"DZN"}},
{"name":"4000","properties":{"item":"3","unit":"CTN"}}
]
const filteredList = list.filter((e) => e.properties.item === "1").map((e) => { return {name: e.name, unit: e.properties.unit}});
console.log(filteredList);
If this is a Vue3 reactive variable remember to add .value before filter() method to make it work

Lua script access object path inside an array

I'm trying to access the object with property optionId = 'a386ead3-08ca-486e-aeb1-23add87292e7' to set its weight.
my object is like following:
weight": {
"options": [
{
"optionId": "a386ead3-08ca-486e-aeb1-23add87292e7",
"weight": 10
},
{
"optionId": "a386ead3-08ca-486e-aeb1-23add87292e7",
"weight": 20
}
],
"value": 100
}
and i'm using the following function to get its path but with no luck:
local GetFieldOptionWeightPath = function (optionId)
return "$.weight.options[\"optionId\"==\""..optionId.."\"]";
end
You need to compare each optionId like you would programmatically. To do that, you can use a filter script expression:
$.weight.options[?(#.optionId=="a386ead3-08ca-486e-aeb1-23add87292e7")]
Here, $() is the filter and # will point to each of the elements that's getting filtered.
Note that since it's a filter, it may potentially yield multiple results. In fact, in your example case it will yield both entries as they have the same optionId.
In the end your Lua function generating the path can look like:
local GetFieldOptionWeightPath = function (optionId)
return ("$.weight.options[?(#.optionId==%q)]"):format(optionId)
end
This answer assumes JSONPath support which was implemented in RedisJSON v2 (late 2021).

Cannot update document by index in FaunaDB

I'm attempting to update a document using an index in my FaunaDB collection using FQL.
Update(
Match(
Index('users_by_id'),
'user-1'
),
{
data: {
name: 'John'
}
}
)
This query gives me the following error:
Error: [
{
"position": [
"update"
],
"code": "invalid argument",
"description": "Ref expected, Set provided."
}
]
How can I update the document using the index users_by_id?
Match returns a set reference, not a document reference, because there could be zero or more matching documents.
If you are certain that there is a single document that matches, you can use Get. When you call Get with a set reference (instead of a document reference), the first item of the set is retrieved. Since Update requires a document reference, you can then use Select to retrieve the fetched document's reference.
For example:
Update(
Select(
"ref",
Get(Match(Index('users_by_id'), 'user-1'))
),
{
data: {
name: 'John'
}
}
)
If you have more than one match, you should use Paginate to "realize" the set into an array of matching documents, and then Map over the array to perform a bulk update:
Map(
Paginate(
Match(Index('users_by_id'), 'user-1')
),
Lambda(
"ref",
Update(
Var("ref"),
{
data: {
name: "John",
}
}
)
)
)
Note: For this to work, your index has to have an empty values definition, or it must explicitly define the ref field as the one and only value. If your index returns multiple fields, the Lambda function has to be updated to accept the same number of parameters as are defined in your index's values definition.

Possible to use angular-datatables with serverside array sourced data instead of object sourced data

I'm trying to use angular-datatables with serverside processing. However, it seems that angular-datatables expects that the data from the server is in object format (object vs array data described) with column names preceding each table datapoint. I'd like to configure angular-datatables to accept array based data since I can't modify my server side output which only outputs data in array format.
I'm configuring Datatables in my javascript like so:
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withOption('ajax', {
url: 'output/ss_results/' + $routeParams.uuid,
type: 'GET'
})
.withDataProp('data')
.withOption('processing', true)
.withOption('serverSide', true);
My data from the server looks like this in array format:
var data = [
[
"Tiger Nixon",
"System Architect",
"$3,120"
],
[
"Garrett Winters",
"Director",
"$5,300"
]
]
But as far as I can tell, angular-datatables is expecting the data in object format like so:
[
{
"name": "Tiger Nixon",
"position": "System Architect",
"extn": "5421"
},
{
"name": "Garrett Winters",
"position": "Director",
"extn": "8422"
}
]
I tried not defining dtColumns or setting it to an empty array like vm.dtColumns = []; but I get an error message when I do that. When I configure dtColumns with a promise to load the column data via ajax I get datatables error #4 because it can't find the column name preceding my table datapoints in the data retrieved from the server.
Is it possible to configure angular-datatables to accept array based data? I can't find anything on the angular-datatables website that indicates it can be configured this way.
Edit: So I removed the .withDataProp('data') which I think was causing the problem. The table works a little better now but it's still broken. After it loads, I get the message No matching records found. Even though right below it it says Showing 1 to 10 of 60,349 entries
Previous1…456…6035Next Does anyone know why this might be?
If you want to use an array of arrays instead of an array of objects, simply refer to the array indexes instead of the object names :
$scope.dtColumns = [
DTColumnBuilder.newColumn(0).withTitle('Name'),
DTColumnBuilder.newColumn(1).withTitle('Position'),
DTColumnBuilder.newColumn(2).withTitle('Office'),
DTColumnBuilder.newColumn(3).withTitle('Start date'),
DTColumnBuilder.newColumn(4).withTitle('Salary')
]
demo using the famous "Tiger Nixon" array loaded via AJAX -> http://plnkr.co/edit/16UoRqF5hvg2YpvAP8J3?p=preview

Is it possible to turn an array returned by the Mongo GeoNear command (using Ruby/Rails) into a Plucky object?

As a total newbie I have been trying to get the geoNear command working in my rails application and it appear to be working fine. The major annoyance for me is that it is returning an array with strings rather than keys which I can call on to pull out data.
Having dug around, I understand that MongoMapper uses Plucky to turn the the query resultant into a friendly object which can be handled easily but I haven't been able to find out how to transform the result of my geoNear query into a plucky object.
My questions are:
(a) Is it possible to turn this into a plucky object and how do i do that?
(b) If it is not possible how can I most simply and systematically extract each record and each field?
here is the query in my controller
#mult = 3963 * (3.14159265 / 180 ) # Scale to miles on earth
#results = #db.command( {'geoNear' => "places", 'near'=> #search.coordinates , 'distanceMultiplier' => #mult, 'spherical' => true})
Here is the object i'm getting back (with document content removed for simplicity)
{"ns"=>"myapp-development.places", "near"=>"1001110101110101100100110001100010100010000010111010", "results"=>[{"dis"=>0.04356444023196527, "obj"=>{"_id"=>BSON::ObjectId('4ee6a7d210a81f05fe000001'),...}}], "stats"=>{"time"=>0, "btreelocs"=>0, "nscanned"=>1, "objectsLoaded"=>1, "avgDistance"=>0.04356444023196527, "maxDistance"=>0.0006301239824196907}, "ok"=>1.0}
Help is much appreciated!!
Ok so lets say you store the results into a variable called places_near:
places_near = t.command( {'geoNear' => "places", 'near'=> [50,50] , 'distanceMultiplier' => 1, 'spherical' => true})
This command returns an hash that has a key (results) which maps to a list of results for the query. The returned document looks like this:
{
"ns": "test.places",
"near": "1100110000001111110000001111110000001111110000001111",
"results": [
{
"dis": 69.29646421910687,
"obj": {
"_id": ObjectId("4b8bd6b93b83c574d8760280"),
"y": [
1,
1
],
"category": "Coffee"
}
},
{
"dis": 69.29646421910687,
"obj": {
"_id": ObjectId("4b8bd6b03b83c574d876027f"),
"y": [
1,
1
]
}
}
],
"stats": {
"time": 0,
"btreelocs": 1,
"btreelocs": 1,
"nscanned": 2,
"nscanned": 2,
"objectsLoaded": 2,
"objectsLoaded": 2,
"avgDistance": 69.29646421910687
},
"ok": 1
}
To iterate over the responses just iterate as you would over any list in ruby:
places_near['results'].each do |result|
# do stuff with result object
end