How to test if value is present in Set of objects with Ramda - ramda.js

I'm trying to figure out how to see if a specific value, in this case "/db/User/10" exists in a set of objects that is returned from an API.
The Data returned from the API from console.log(test)
Set {
Object {
"UserData": "/db/UserData/509c404f-ffa1-49d3-a161-b9eb5b2ebb14",
"UserDataPrivate": "/db/UserDataPrivate/26b7d879-4403-4135-ab44-b23e5244606b",
"UserStatus": "/db/UserStatus/cb50eae0-e254-4d8c-915a-674fd314fff3",
"createdAt": "2017-10-20T21:03:26.575Z",
"id": "/db/User/68",
"inactive": null,
"updatedAt": "2017-10-20T23:34:12.179Z",
"username": "15031111234",
"version": 4,
},
Object {
"UserData": "/db/UserData/a3dded63-4e0b-4841-b201-4c96dad1844d",
"UserDataPrivate": "/db/UserDataPrivate/6037a718-e33f-4945-b2d0-ca4c11fa4f13",
"UserStatus": null,
"createdAt": "2017-09-21T18:26:31.089Z",
"id": "/db/User/10",
"inactive": false,
"updatedAt": "2017-10-18T20:10:06.681Z",
"username": "15038304313",
"version": 19,
},
}
var searchbyID = propEq('id', '/db/User/10');
result = filter(searchbyID, test);
console.log(result)
I'm thinking this should return true, but instead, it returns an empty array.
Is this the right approach with Ramda?

You need to convert the Set to an Array.
var searchbyID = propEq('id', '/db/User/10');
result = filter(searchbyID, Array.from(test));
console.log(result)
It won't return true, but objects that match. You could be wanting find instead.

Related

Transform JSON: select one row from array of json objects

I can't get a specific row from this JSON array.
So I want to get the object where filed 'type' is equal to 'No-Data'
Are there exist any functions in SQL to take the row or some expressions?
"metadata": { "value": "JABC" },
"force": false
"users": [
{ "id": "111", "comment": "aaa", type: "Data" },
{ "id": "222", "comment": "bbb" , type:"No-Data"},
{ "id": "333", "comment": "ccc", type:"Data" }
]
You can use a JSON path query:
select jsonb_path_query_first(the_column, '$.users[*] ? (#.type == "No-Data")')
from the_table
This assumes that the column is defined as jsonb (which it should be). If it's not you have to cast it: the_column::jsonb
Online example

Get the value from the response based on a condition and store it to a variable

I would like to get the value from the response based on a condition and store it to a variable.
In the below JSON, I would like to store the value when the name matches to something I prefer. Is there a way to achieve this using Karate API?
{
"results": [
{
"name": "Sample1",
"email": "sample1#text.com",
"id": "U-123"
},
{
"name": "Sample2",
"email": "sample2#text.com",
"id": "U-456"
},
{
"name": "Sample3",
"email": "sample3#text.com",
"id": "U-789"
}
]
}
So after reading the comment, my interpretation is to "find" the id where name is Sample2. Easy, just use a filter() operation, refer the docs: https://github.com/karatelabs/karate#jsonpath-filters
Instead of using a filter, I'm using the JS Array find() method as a very concise example below:
* def response =
"""
{ "results": [
{ "name": "Sample1", "email": "sample1#text.com", "id": "U-123" },
{ "name": "Sample2", "email": "sample2#text.com", "id": "U-456" },
{ "name": "Sample3", "email": "sample3#text.com", "id": "U-789" }
]
}
"""
* def id = response.results.find(x => x.name == 'Sample2').id
* match id == 'U-456'
Take some time to understand how it works. Talk to someone who knows JS if needed.

Karate For loop to get ids based on pattern and then use a delete feature

I have response from an API call that gives me a list of devices each with an id. Some of these devices are test devices with the id starting with the prefix 'Test' Example Test319244.
I wish to only retrieve those ids with the prefix 'Test', may be in an array and be able to pass them to another feature file which takes the device ID as the parameter to delete it. Basically I want to delete all the testdevices.
Here is the sample response that contains all the device IDs
{
"items": [
{
"deviceId": "004401784033074000",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319246",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319245",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319244",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "command-service",
"deviceType": "service",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
},
{
"deviceId": "kafka-connect-all",
"deviceType": "kafka-connect",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
}
],
"metadata": {
"pagination": {
"limit": 50,
"offset": 0,
"previousOffset": 0,
"nextOffset": 0,
"totalCount": 15
},
"sortedBy": [
{
"field": "deviceId",
"order": "ASC"
}
]
}
}
Here in the above example I only want to delete the devices with ids - Test319244,Test319245 and Test319246
How can I get an array of ids based on the pattern(Testxxxxxx) and pass that on to another feature file
I need help to define an array of ids like:
* def ids = extract the ids based on the pattern
# pass the ids to the delete feature which would send the id one at a time and delete the device.
* def delete = call(delete.feature) ids
This is how the delete scenario feature file looks:
Scenario: Delete Device
# device_registry_url defined in karate-config.js
Given url device_registry_url
And path '/device/'+DeviceID
And header Authorization = authheader
And request ''
When method delete
Then status 200
Would this be the right approach or could we do it in a better way? If so, can someone kindly help in how to do it please?
Just use karate.filter() and then you know what to do:
* def fun = function(x){ return x.deviceId.startsWith('Test') }
* def filtered = karate.filter(response.items, fun)
* call read('delete.feature') filtered

Unable to loop through data from SQLite call in React Native, Expo

I'm currently making a call to an SQLite local database in my react native
Expo app like so:
db.transaction(tx => {
tx.executeSql('select * from dr_report_templates', [], (_, { rows }) => {
const templateData = JSON.stringify(rows);
this.setState({ options: templateData, isLoading: false }, () => console.log(this.state))
});
},
error => {
alert(error);
},
() => console.log('Loaded template settings')
);
I'm returning the data and making it a JSON string with: JSON.stringify
Data appears like so:
Object {
"isLoading": false,
"options": "{\"_array\":[{\"id\":30,\"name\":\"SFR General\",\"description\":\"SFR1\"},{\"id\":31,\"name\":\"SFR Extended\",\"description\":\"SFR2\"},{\"id\":7790,\"name\":\"test_new_template\",\"description\":\"test_new_template\"},{\"id\":7792,\"name\":\"apart_1\",\"description\":\"apart_1\"},{\"id\":7793,\"name\":\"SFR\",\"description\":\"Single Family\"},{\"id\":7798,\"name\":\"Condo\",\"description\":\"Condo \"},{\"id\":7799,\"name\":\"Duplex\",\"description\":\"Duplex\"},{\"id\":7800,\"name\":\"Triplex \",\"description\":\"3\"},{\"id\":7801,\"name\":\"Apartments\",\"description\":\"Apartment complex\"},{\"id\":7802,\"name\":\"Commercial retail store \",\"description\":\"Storefront \"},{\"id\":7803,\"name\":\"5-10 unit\",\"description\":\"5\"},{\"id\":7804,\"name\":\"Commercial Industrial \",\"description\":\"Industrial \"},{\"id\":7805,\"name\":\"Industrial Property\",\"description\":\"RE\"}],\"length\":13}",
"selected": "",
}
Attempting to get values for just the first array element like so:
this.state.options[0]
does not work. I'm obviously doing something wrong in the way that I'm doing this but can't figure out what. Any ideas?
EDIT: I had also ran the query with out JSON.Stringify. The data returned like so with this "t" in front of it. I've never hard this before and I couldn't loop through it so that's why I did a JSON.stringify.
t {
"_array": Array [
Object {
"description": "SFR1",
"id": 30,
"name": "SFR General",
},
Object {
"description": "SFR2",
"id": 31,
"name": "SFR Extended",
},
Object {
"description": "test_new_template",
"id": 7790,
"name": "test_new_template",
},
Object {
"description": "apart_1",
"id": 7792,
"name": "apart_1",
},
Object {
"description": "Single Family",
"id": 7793,
"name": "SFR",
},
Object {
"description": "Condo ",
"id": 7798,
"name": "Condo",
},
Object {
"description": "Duplex",
"id": 7799,
"name": "Duplex",
},
Object {
"description": "3",
"id": 7800,
"name": "Triplex ",
},
Object {
"description": "Apartment complex",
"id": 7801,
"name": "Apartments",
},
Object {
"description": "Storefront ",
"id": 7802,
"name": "Commercial retail store ",
},
Object {
"description": "5",
"id": 7803,
"name": "5-10 unit",
},
Object {
"description": "Industrial ",
"id": 7804,
"name": "Commercial Industrial ",
},
Object {
"description": "RE",
"id": 7805,
"name": "Industrial Property",
},
],
"length": 13,
}
this.setState({ options: templateData._array, isLoading: false });
or change how you destructure in 3rd parameter of executeSql to:
(_, { rows: { _array } }) => {
const templateData = JSON.stringify(_array);
}
Why you're conveting it with JSON.stringify()? You can iterate over array or access it with array's key name.
NOTE: JSON.stringify() does not convert it to JSON. It converts to JSON string
The JSON.stringify() method converts a JavaScript object or value to a
JSON string, optionally replacing values if a replacer function is
specified or optionally including only the specified properties if a
replacer array is specified.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
You're actually converting your database response to string.
So Change
const templateData = JSON.stringify(rows);
to
const templateData = rows;
and use this array where you want.

How to make a Object.map?

I had an array of data. 7 items for which I used data.map. I loaded this array on firease and now I can't call it like this . Because this is not the Array is already in the Objects.
Question.
How do I do data.map for Objects. Moreover, I need to transfer data. Specifically: id, name , info , latlng. Inside is the ImageCard that should be in the data.map.
Example object:
Object {
"0": Object {
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
},
You can extract keys from Object using Object.keys which will return an array of all keys and then map this array.
like this,
const obj={
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
}
let keys = Object.keys(obj);
keys.map(item=>{
//..... do your stuff from object like,
let y=obj[item]
// or whatever
}
Multiple ways to do that
one mentioned by Jaydeep Galani
another approach would be to use Proxy
const obj={
"id": 0,
"image": "/images/Stargate.jpg",
"info": "Stargate is a 1994 science fiction adventure film released through
Metro-Goldwyn-Mayer (MGM) and Carolco Pictures..",
"latlng": Object {
"latitude": 53.6937,
"longitude": -336.1968,
},
"name": "Stargate",
"year": "1994",
}
const newObj = new Proxy(obj,{
get: (target, prop)=>{
let newArr = [];
if(prop === 'map'){
// then first convert target into array
Object.keys(target).foreach(item => {
newArr.push({item:target[item]})
})
// then apply map function to that and return the result
return newArr.map((item)=>{
//your code goes here like
return<div>{item.info}</div>
})
}
}
})