I have 2 different JSON responses from 2 different GET methods from postman.
I am trying to write a test to compare them.
the first response is specificYield
[
[
"2020-11-30T00:00:00Z",
50.701604514154944
],
[
"2020-12-31T00:00:00Z",
19.328539610238128
],
[
"2021-01-31T00:00:00Z",
25.183636870929693
]
]
The second response is expectedYield
[
[
"2020-11-30T00:00:00Z",
62.78336503569246
],
[
"2020-12-31T00:00:00Z",
32.97678871921154
],
[
"2021-01-31T00:00:00Z",
40.82916109964013
]
]
I am trying to check that the integer value in the expectedYield array is always greater than the value in the specificYield array
so it will compare the 62.78336503569246 to 50.701604514154944
and then 32.97678871921154 to 19.328539610238128
and so on
The test looks like this so far, but I know im not using objects anymore and am trying to refactor it to just work with arrays.
pm.test("Expected Yield > Specified Yield", function () {
const expectedYield = pm.response.json();
const specificYield = pm.globals.get("specificYield");
Object.values(expectedYield).forEach((record) => {
pm.expect(record[0][1]).to.greaterThan(
Object.values(specificYield).forEach((record) => {
pm.expect(record[0][1]);
})
);
});
});
I don't know PostMan syntax, but the standard JS syntax would be
expectedYield.every((record, i) => record[1] > specificYield[i][1])
The second argument i to the callback function is the index of the element being tested. This is used to compare that record with the corresponding record of the other array.
Then we use every() to check that it's true for the entire array. There's no need to use Object.values() -- that's used to convert the properties of an ordinary object to an array in order to use array methods, but these are already arrays.
Related
I am trying to compare two or multiple objects using difference() function and then use the object(s) key value to push into an array of just url's, but I can't use dot syntax on an object for some reason any hints?
array this.stores
[
{
name: "Google Play"
url: "https://play.google.com"
}
]
array result
[
{
name: "Google Play"
url: "https://play.google.com"
},
{
name: "Steam Store"
url: "https://store.steampowered.com"
}
]
I'm comparing these 2 arrays of objects like this:
const storesDifference = difference(result, this.stores);
// works as it should stores 'Steam Store'
console.log('difference', storesDifference.url);
// I'm trying to return URL key using dot syntax but without any success
managed to solve this problem using filter function. Works as i wanted.
see below:
const array3 = this.stores
.filter((x) => !result.includes(x))
.concat(result.filter((x) => !this.stores.includes(x)));
I am using a Japanese dictionary API, which allows me to put in a term and return a list of nouns, verbs, adverbs, and others. This is a result of the API. The API returns an array with a list of objects. Each object has its own arrays and in one of these arrays, there is the array with the information I need, like this:
{"data":[
{
"attribution": {"jmdict": true, "jmnedict": false},
"senses": [
{
"english_definitions": [
"sleep"
],
"parts_of_speech": [
"Noun",
"Ichidan verb"
]
},
{
"english_definitions": [
"Sleep"
],
"parts_of_speech": [
"Ichidan verb", "intransitive verb"
]
}
]
}
]
}
I want to filter the result to show only those results that are verbs, excluding the other results.
This is my code to do the filter:
verbArray = jsonData.data
let verbs2 = verbArray.filter(e => e.senses[0].parts_of_speech[0].includes('verb'));
//console.table(verbs2);
This code does the filter and gives me only those results that have the word verb in the string. But only if it is in position one of the array. If the term is in position two or three, will not return it
You can use
let verbs2 = verbArray.filter(e => e.senses[0].parts_of_speech.some(pos=>pos.includes('verb')));
if you only want to iterate all parts of speeches. The Array.prototype.some() will check all parts of speech for the term verb.
If you also want to iterate senses, you can use
let verbs2 = verbArray.filter(e => e.senses.some(sense=> sense.parts_of_speech.some(pos=>pos.includes('verb'))));
Hello developers I'm trying to modify an array of objects inside an array of objects before deploying its result to Redux reducer.
The array is obtained through a request to an endpoint, reason why i must to create an instance of writable copy of it , and then proceed on the process
Lest say i have this array:
allProducts= [
{
"product_type": "Bikes",
"product_imgs": [
{
"id": 5,
"url": "Mountain Bike/Screenshot (200)"
},
{
"id": 6,
"url": "Mountain Bike/Screenshot (200)"
}
],
"product_name": "product test 1"
},
{
"product_type": "Bikes",
"product_imgs": [
{
"id": 7,
"url": "City Bike/banderaa"
},
{
"id": 8,
"url": "City Bike/banderaa"
}
],
"product_name": "product test 2"
}
]
I would like to modify the items inside the array product_imgs of each object , but for that , having in mind this array comes from a request , i do create a readable copy an over that i set the logic.
let instance=[...allProducts];
then using a double for each (though i also tried using a doule for loop) i reach till every image inside the array of objects product_imgs of each object :
instance.forEach(array=>array.product_imgs.map(element => {
this.imgDownLoaderFirebase
.ref(element.url)
.getDownloadURL()
.toPromise()
.then((url) => {
console.log(url);
//then in this space once the url of some firebase endpoint is reached and else
//i would like to modify that object inside the array product_imgs which is at the same time
//part of the instance array.
//For that i expose that this new url gotten would be asigned as the new
//value thus
element = { ...element };
element.url=url
console.log(element);
console.log(instance);//Printing the general array in order to check if changes committed
})
})
I want to specify that i use first a foreach and then a map in order to modify the inner array of objects result , but using a double for each doesn't precisely inmprove this situation:
instance.forEach(array=>array.product_imgs.forEach(element => {........
Then checking the logs , the element (item url) inside the array of objects product_imgs of the array of obejcts instance , is modified , but the external array containing the inner modified not
How could i improve this?
Thanks
If your goal is to extract all product_img values from your array, you could try something like the following :
// This line will convert your array of object into an array of array of urls, using a destructuring process
const urls = allProducts.map(({ product_img }) => product_img);
// This line will merge the previous result into a single-level array of urls that you can iterate onto.
const result = [].concat([], ...res);
Edit : I forgot to mention that this process will in fact return an array of objects including your id and url.
I have a group of filters that is an Reactive Forms Object. I’ve taken the property values of the object and pushed it into an array.
// original filters object {claim_number: null, status: "Approved", patient: null, service_date: null}
let filterArr = []
Object.keys(this.filtersForm.value).forEach(filter => {
filterArr.push(this.filtersForm.value[filter])
// filterArr [null, “Approved, null, null]
})
I have a table that is comprised of an array of objects like the following:
"claims":[
{
"billed_amount":141.78,
"claim_number": "6596594-0",
"location":"University Hospital",
"member_id":"A1234567890",
"status":{
"label":"Approved",
"value": "Approved"
}
},
{
"billed_amount":341.70,
"claim_number": "2196524-3",
"location":"Springfield Hospital",
"member_id":"B1234567890",
"status":{
"label":"Pending",
"value":"Pending"
}
},
{
"billed_amount":111.70,
"claim_number": "1233514-5",
"location":"Springfield Hospital",
"member_id":"C1234567890",
"status":{
"label":"Pending",
"value":"Pending"
}
},
{
// ...etc
}
]
I am trying to loop through each row and put the property values in an array, one for each row so I can filter them against filterArr. How can I do that?
My question is similar to this post (From an array of objects, extract value of a property as array
), with the key difference being that I'm trying to create an array per object.
Each object represents a row in a table that I am trying to dynamically filter. So I can't have values from different rows being put into one array.
According to your desired result, I think you can use ES6 functions.
const result = yourTable.map(element => Object.values(element));
Using map() function, you go through all elements, and extract from each object its values.
Unsure what you want to include in your output but the below will loop through an array and return an array to the filter function
const output = claimTable["claims"].map((claim) => {
return claim
}).filter((claim) => {
return claim.billed_amount > 100
})
The above will loop through the claims and 'convert' to an array. The filter will return that claim for all true conditions (in this case, if the billed amount is greater than 100).
This article goes over this and adds a bit more to it.
I have an array of objects in JavaScript. Each object contains property named "myPropArray", which is actually another array. Here is the structure of this array:
let myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
What I need is to take all arrays under this property and to merge them all in one array, without duplicates (in JavaScript).
Here is my implementation (using lodash):
_.map(myBaseArray , 'myPropArray')
Which is actually returning the following result:
[
["s1", "s2"],
["s2", "s3"]
]
But what I want to accomplish is:
["s1", "s2", "s3"]
Also (if possible) I'm trying to avoid for-each loops, since this needs to me optimized as much as possible and I'm wondering if can be done using lodash mappers or other similar function?
There are already some solutions from this stage where I got (as the solution here) but I would like to find a solution tho this problem which will be specific for my "array or objects which contains properties of type Array"
.
Extract the property's value with Array.map(), flatten by spreading into Array.concat(), and use a Set to get unique values. Spread the Set back to an array:
const myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
const result = [...new Set([].concat(...myBaseArray.map((o) => o.myPropArray)))]
console.log(result)
The lodash way would be to use _.flatMap(), and _.uniq():
const myBaseArray = [
{
myPropArray: ["s1", "s2"],
someOtherProp: "randomString1"
},
{
myPropArray: ["s2", "s3"],
someOtherProp: "randomString2"
}
]
const result = _.uniq(_.flatMap(myBaseArray, 'myPropArray'))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>