I'm asserting for elements in array1 does exist in array2 or not, below are the array samples,
var array1 = [
{
"name": "appe",
"loc": "war",
"order": 5,
"neck": "NO",
"end": false
},
{
"name": "con",
"loc": "conve",
"order": 15,
"neck": "NO",
"end": true
}]
var array2 = [{"neck":"NO"
"end":true,
"loc":"conve",
"name":"con",
"order":15
}]
code I tried -
const lodash = require("lodash");
for(var i = 0; i < array2.length; i++ )
{
tests['response json contain Data'] = lodash._.has(points, array2[i]);
//pm.expect(array2[i]).to.be.oneOf(array1);
}
error I get -
response json contain Data | AssertionError: expected false to be truthy
EDIT
after trying another attempt2 -
pm.expect(array2[i]).to.be.oneOf(array1);
error -
AssertionError | expected { Object (name, loc, ...) } to be one of [ Array(20) ]
attempt3 -
pm.expect(array1).to.deep.equal(array2);
error -
AssertionError | expected [ Array(20) ] to deeply equal [ Array(18) ]
what I'm doing wrong?. what I want is if any one element in array2 is not in array1 it should fail.
Thanks
Chai assertion Library is included by Postman by default in its application. So you need to use the to.deep.equal. It will compare nested arrays as well as nested objects.
SOLUTION:
pm.test("verify two objects", function () {
var array1 = [{
"name": "appe",
"loc": "war",
"order": 5,
"neck": "NO",
"end": false
},
{
"name": "con",
"loc": "conve",
"order": 15,
"neck": "NO",
"end": true
}];
var array2 = [{
"neck": "NO",
"end": true,
"loc": "conve",
"name": "con",
"order": 15
},
{
"neck": "NOo",
"end": true,
"loc": "conve",
"name": "con",
"order": 15
}];
pm.expect(array1).to.deep.equal(array2); // Failed
pm.expect(array1).to.deep.equal(array1); // Passed
});
var array1 = [{
"name": "appe",
"loc": "war",
"order": 5,
"neck": "NO",
"end": false
},
{
"name": "con",
"loc": "conve",
"order": 15,
"neck": "NO",
"end": true
}
]
var array2 = [{
"neck": "NO",
"end": true,
"loc": "conve",
"name": "con",
"order": 15
}]
array2.forEach( item => {
if ( !array1.includes(item)){
throw 'doesn\'t include'
}
})
what I want is if any one element in array2 is not in array1 it should fail
var array1 = [{
"name": "appe",
"loc": "war",
"order": 5,
"neck": "NO",
"end": false
},
{
"name": "con",
"loc": "conve",
"order": 15,
"neck": "NO",
"end": true
}
]
var array2 = [{
"neck": "NO",
"end": true,
"loc": "conve",
"name": "con",
"order": 15
}, {
"neck": "NOo",
"end": true,
"loc": "conve",
"name": "con",
"order": 15
}];
// Finds at least one object on array2 which is not in array1.
// The function some finds at least one according to the
// result of findIndex which is using a handler who executes
// the function every.
// The function every, basically, compares every key-value
// between array2 and array1.
let result = array2.some(o => array1.findIndex(ao => Object.entries(o).every(([key, value]) => ao[key] === value)) === -1);
console.log(result); // Returns true because one element is not in array1
Related
I have an Object which is having some properties like this:
obj1={
"id": 2,
"description": "",
"operationIds": [
{
"id": 1,
"name": "Standard"
}
],
"ratingIds": [
{
"id": 1,
"name": "name1",
"description": "",
},
{
"id": 4,
"name": "name4",
"description": "",
},
{
"id": 8,
"name": "name8",
"description": "",
},
],
}
I want covert the array of objects (operationIds and ratingIds) inside the object to array of properties, I'm receiving this object and I want to apply the change on it and supply another method so it should look like this:
obj1={
"id": 2,
"description": "",
"operationIds": [
1
],
"ratingIds": [
1,
4,
8
],
"timestamp": "AAAAAAAGJ6c=",
"estimatedUtilReconciliationApplies": true
}
I was able to do it but in a verry ugly way, is there a more simple and clear way to accomplish this ?
let x = {...obj} as any;
let ar1 = x.operationIds;
const arr1= ar1.map(function (obj) {
return obj.id;
});
let ar2 = x.ratingIds;
const arr2= ar2.map(function (obj) {
return obj.id;
});
x.operatingEnvironmentIds = arr1;
x.thrustRatingIds = arr2;
You can use spread operator and map
let obj1={
"id": 2,
"description": "",
"operationIds": [
{
"id": 1,
"name": "Standard"
}
],
"ratingIds": [
{
"id": 1,
"name": "name1",
"description": "",
},
{
"id": 4,
"name": "name4",
"description": "",
},
{
"id": 8,
"name": "name8",
"description": "",
},
],
}
console.log({
...obj1,
operationIds:obj1.operationIds.map(elem => elem.id),
ratingIds:obj1.ratingIds.map(elem => elem.id),
})
And as a function
let obj1={
"id": 2,
"description": "",
"operationIds": [
{
"id": 1,
"name": "Standard"
}
],
"ratingIds": [
{
"id": 1,
"name": "name1",
"description": "",
},
{
"id": 4,
"name": "name4",
"description": "",
},
{
"id": 8,
"name": "name8",
"description": "",
},
],
}
let transform = (obj) => {
return({
...obj,
operationIds:obj.operationIds.map(elem => elem.id),
ratingIds:obj.ratingIds.map(elem => elem.id),
})
}
let transformed = transform(obj1)
console.log(transformed)
We loop the array and use the Object.assign() method to convert an array of objects to a single object. This merges each object into a single resultant object.
The Object.assign() method also merges the properties of one or more objects into a single object.
My code for sorting episodes in ascending order using episodeNo is
const episodes = await Episode.find({anime: animeId}).sort({episodeNo: 1});
I think it should sort like 1, 2, 3, 4 but it's sorting like 1, 10, 11, 2, 3.
the JSON response is
"id": "607dc50dc21368000414b378",
"episodeNo": 1,
"subtitle": false
},
{
"id": "607dc50dc21368000414b381",
"episodeNo": 10,
"subtitle": false
},
{
"id": "607dc50dc21368000414b382",
"episodeNo": 11,
"subtitle": false
},
{
"id": "607dc50dc21368000414b383",
"episodeNo": 12,
"subtitle": false
},
{
"id": "607dc50dc21368000414b379",
"episodeNo": 2,
"subtitle": false
}
I have an object that looks like the following {key: id numbers}
var obj = {
"c4ecb": {id: [3]},
"a4269": {id: [34,36]},
"d76fa": {id: [54,55,60,61]},
"58cb5": {id: [67]}
}
How do I loop each above id in the following array, and return the label?
var response =
{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}
Finally, what I want to do is look up the key and return a string of id values.
For example, input c4ecb and output strawberry. Input a4269 and output lettuce, radish. Input d76fa and output "spaghetti, rigatoni, lasagna, fettuccine"
I think to join the multiple labels output into one string I could use something like
array.data.vegetables.map(vegetables => vegetables.value).join(', ')].toString();
So in the end I want to have something like
var fruits = [some code that outputs "strawberry"];
var vegetables = [some code that outputs "lettuce, radish"];
var pasta = [some code that outputs "spaghetti, rigatoni, lasagna, fettuccine"];
What I've tried so far:
The following loop will return the id only if there is one id to be called for: e.g. only in case one where {id: 3} but returns null in cases like {id: 34,36} (because it's looking for '34,36' in id, which doesn't exist - I need to look for each one individually.
response.data.forEach(({key, options}) => {
if (obj[key]) {
options.forEach(({id, label}) => {
if (id == obj[key].id) obj[key].label = label;
});
}
});
console.log(obj)
Filter the response object to focus on the category that matches the id.
Map over the options array and select the items which appear in obj[id].
Finally convert the filtered results to a string.
See filteredLabelsAsString() function below for implementation.
var obj = {
"c4ecb": {"id": [3]},
"a4269": {"id": [34,36]},
"d76fa": {"id": [54,55,60,61]},
"58cb5": {"id": [67]}
}
var response =
[{
"success": true,
"data": [
{
"key": "c4ecb",
"name": "fruits",
"options": [
{
"label": "strawberry",
"id": 3
},
{
"label": "apple",
"id": 4
},
{
"label": "pineapple",
"id": 5
},
{
"label": "Other",
"id": 31
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "a4269",
"name": "vegetables",
"options": [
{
"label": "lettuce",
"id": 34
},
{
"label": "cucumber",
"id": 35
},
{
"label": "radish",
"id": 36
}
],
}
]
},
{
"success": true,
"data": [
{
"key": "d76fa",
"name": "pasta",
"options": [
{
"label": "spaghetti",
"id": 54
},
{
"label": "rigatoni",
"id": 55
},
{
"label": "linguine",
"id": 56
},
{
"label": "lasagna",
"id": 60
},
{
"label": "fettuccine",
"id": 61
}
],
}
]
}];
function filteredLabelsAsString(obj_key, obj, content=response) {
// sanity check: obj must contain obj_key
if (Object.keys(obj).includes(obj_key)) {
return content.filter((item) => {
// filter content using value of obj_key
return item.data[0].key == obj_key;
}).map((item) => {
// item : { success: true, data: [] }
// map over options array
return item.data[0].options.map((opt) => {
// option : {id, label}
// return the label if the id is in the obj object's list
if (obj[item.data[0].key].id.includes(opt.id))
return opt.label;
}).filter((label) => {
// filter out empty items
return label !== undefined;
});
}).join(",");
}
// if obj does not contain obj_key return empty string
return "";
}
console.log("fruits: " + filteredLabelsAsString("c4ecb", obj));
console.log("vegetables: " + filteredLabelsAsString("a4269", obj));
console.log("pasta: " + filteredLabelsAsString("d76fa", obj));
I have an array of results. I want to filter the results to an array whose categorys have at least one object with a cat_id of 1. How can I do that?
"results": [
{
"product_id": 1,
"title": "booking",
"draft": false,
"publish": true,
"category": [
{
"cat_id": 1,
"cat_name": "web",
"product": "booking"
}
],
},
{
"product_id": 2,
"title": "reading",
"draft": false,
"publish": true,
"category": [
{
"cat_id": 6,
"cat_name": "android",
"product": "asdasd"
}
],
},
{
"product_id": 3,
"title": "reading",
"draft": false,
"publish": true,
"category": [],
},
]
My attempt:
for (let i = 0; i < data.results.length; i++) {
this.webCatProducts = data.results[i].category.filter(d => d.cat_id == 1)
console.log(this.webCatProducts)
}
Use .some inside the .filter to check to see if any of the category objects have a matchingcat_id:
const results=[{"product_id":1,"title":"booking","draft":!1,"publish":!0,"category":[{"cat_id":1,"cat_name":"web","product":"booking"}],},{"product_id":2,"title":"reading","draft":!1,"publish":!0,"category":[{"cat_id":6,"cat_name":"android","product":"asdasd"}],},{"product_id":3,"title":"reading","draft":!1,"publish":!0,"category":[],},];
const filtered = results.filter(
({ category }) => category.some(({ cat_id }) => cat_id === 1)
);
console.log(filtered);
You should do filter on product level results: [] and predicate matched with expected category’s id with nested find some like this; .filter(product => product.category.some(cat => cat.cat_id === 1)
Make sure that category is always an array otherwise you have to check more.
I have this JSON:
{
"id": 10,
"name": "Color test2",
"seed_id": 1,
"season_id": 2,
"description": null,
"work_schedule_type_id": 2,
"color": "#00A946",
"active": false,
"starred": true,
"phases": [{
"id": 2,
"name": "Phase2",
"work_schedule_type_id": 2,
"phase_color": "#343434",
"phase_order_of_operations": 1,
"description": "TEST PHASE TYPE2",
"isExpanded": false,
"$$hashKey": "object:1387"
}]
}
I need add a property tasks for each phase in phases. How I can do this?
angular.forEach(obj.phases, function(item) { item.tasks = your_val } );
This approach adds the tasks from an Array.
var obj = {
"id": 10,
"name": "Color test2",
"seed_id": 1,
"season_id": 2,
"description": null,
"work_schedule_type_id": 2,
"color": "#00A946",
"active": false,
"starred": true,
"phases": [{
"id": 2,
"name": "Phase2",
"work_schedule_type_id": 2,
"phase_color": "#343434",
"phase_order_of_operations": 1,
"description": "TEST PHASE TYPE2",
"isExpanded": false,
"$$hashKey": "object:1387"
}]
}
var tasks = ["Task1", "Task2"];
obj.phases.forEach(p => p.tasks = tasks);
console.log(obj.phases);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Assuming your JSON object is named json, the code would look something like this:
for (phase of json.phases) {
phase.task = "somevalue";
}