how to modify properties of an nested object? - javascript

I have the following nested object and I need to leave the "alias" property blank and the "group" property set to true for all "entries" and "exits". I also need to delete the whole "parameters" object.
Would there be a way to do it all in one function? I've tried to apply the delete Object method but it doesn't work as it's an indexed object.
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "do",
"group": false
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "re",
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3,
"group": false
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": "do"
"group": false
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": "re"
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3
"alias": "mi"
"group": false
}
},
"order": 2
}
}
}
the desired nested object would be the following
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "",
"group": true
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "",
"group": true
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": ""
"group": true
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": ""
"group": true
}
},
"order": 2
}
}
}
what I've tried is the following, managing to delete the "parameters" object but I can't access the "label" property of each "entry" and "exit
const nedtedObjectsValues = Object.values(nestedObjects);
for (object of nedtedObjectsValues) {
delete object.properties.parameters;
}
if anyone can give me an idea of how to approach this function.
Thank you in advance.

In JavaScript, to reference numeric object properties, you need to use the square brackets syntax:
object.1 // bad
object[1] // good
You can delete numeric property like this:
delete object[1];

Related

Can't loop array after grouping with .reduce

I've got the following meetings object :
[
{
"id": 19,
"duration": 1.75,
"Employee": {
"name": "Jeanne",
}
},
{
"id": 20,
"duration": 1.00,
"Employee": {
"name": "Louis",
}
},
{
"id": 21,
"duration": 1.00,
"Employee": {
"name": "Jeanne",
}
}
]
I want to group it by Employee.name. Using reduce() here is what I come up with :
meetings.reduce(function (r, a) {
r[a.Employee.name] = r[a.Employee.name] || [];
r[a.Employee.name].push(a);
return r;
}
The resulting object is the following :
{
"Jeanne": [
{
"id": 19,
"duration": 1.75,
"Employee": {
"name": "Jeanne"
}
},
{
"id": 21,
"duration": 1.00,
"Employee": {
"name": "Jeanne"
}
}
],
"Louis": [
{
"id": 20,
"duration": 1.00,
"Employee": {
"name": "Louis"
}
}
]
}
If I try to map() or forEach() i cannot get the value of the element :
Array.from(thisMeeting).forEach(element => console.log(element));
return `undefined`;
Array.from-ming an Object will result in an empty array.
You'll have to iterate over the objects keys with Object.entries(thisMeeting).forEach… and grab the values inside that.

Walk through objects nested at different levels and delete selected properties

There are two types of objects. The first one is pretty simple:
{
"status": "200",
"dump": {
"id": "213ad4c0",
"product": {
"productName": "Bicycle"
},
"components": {
"steering": {
"id": "HB2",
"description": "Handlebar",
"quantity": 1,
"spare_part": false,
"material": "steel"
},
"wheel": {
"id": "WH8",
"description": "Wheel",
"quantity": 2,
"spare_part": true,
"material": "steel"
}
}
}
}
I wanted to delete spare_part property from it and it could've been done with the following:
Object.entries(myResponse.dump.components).forEach(([key, value]) => {
delete value.spare_part;
});
Things get complicated when an object is composed of nested objects like:
{
"status": "200",
"dump": {
"id": "8e8cd4ee",
"product": {
"productName": "Car"
},
"components": {
"suspension": {
"id": "SU_02",
"description": "Suspension",
"quantity": 1,
"spare_part": false,
"material": "mixed",
"subcomponents": {
"S_FRONTAL": {
"id": "SU_02_F",
"description": "Suspension Front",
"quantity": 1,
"spare_part": false,
"material": "mixed",
"subcomponents": {
"DAMPER_L": {
"id": "SU_D_L_12",
"description": "Damper Front Left",
"quantity": 1,
"spare_part": true,
"material": "mixed"
},
"DAMPER_R": {
"id": "SU_D_R_12",
"description": "Damper Front Right",
"quantity": 1,
"spare_part": true,
"material": "mixed"
}
}
}
}
}
}
}
}
How can I gracefully walk through all levels of nesting and delete the spare_part property?
By gracefully I mean no manual key chaining in Object.entries() arguments :-)
You can use recursion, like this:
const removeSpareParts = (components) => {
Object.values(components).forEach((component) => {
delete component.spare_part;
if (component.subcomponents) {
removeSpareParts(component.subcomponents);
}
});
};
removeSpareParts(myResponse.dump.components);
This will go through each given layer-1 component, delete its spare part, and recursively do the same for all its subcomponents.

Postman - How to count occurrences of a specific object in a JSON response

I am new to JSON and Postman. I believe I'm trying to do something very simple.
I have created a GET request which will get a JSON response like the one below.
In the example below I want to get the count of All "IsArchived" attributes in the response;
The number of those attributes will vary from response to response.
How can I do it? Thanks in advance
{
"Id": 1328,
"Name": "AAA Test",
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"FieldGroups": [
{
"Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
"Name": "General",
"FieldDefinitions": [
{
"Id": 1,
"DisplayName": "Product Name",
"IsArchived": false
},
{
"Id": 2,
"DisplayName": "Short Description",
"IsArchived": false
},
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
},
]
},
{
"Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
"Name": "Somethingelse",
"FieldDefinitions": [
{
"Id": 123,
"DisplayName": "Attribution",
"IsArchived": false
},
{
"Id": 1584,
"DisplayName": "FC1",
"IsArchived": false
},
{
"Id": 623,
"DisplayName": "Sizes",
"IsArchived": false,
"Owner": {
"Id": 208,
"Name": "The Boss"
},
"Unit": "",
"Options": [
{
"Id": 1,
"Value": "XS"
},
{
"Id": 2,
"Value": "S"
},
{
"Id": 3,
"Value": "M"
}
]
}
]
}
],
"IsArchived": false
"Version": 1
}
It is a rather specific solution but I hope it helps. The description is added as comments:
// Convert the response body to a JSON object
var jsonData = pm.response.json()
// Create a count variable which will be increased by 1 everytime IsArchived occurs
var count = 0;
function countIsArchived() {
// Loop through the FieldGroupsArray
_.each(jsonData.FieldGroups, (fieldGroupsArray) => {
// Loop through the FieldDefinitionsArray
_.each(fieldGroupsArray.FieldDefinitions, (fieldDefinitionsArray) => {
// Check if IsArchived exists
if(fieldDefinitionsArray.IsArchived) {
// Increase count by 1
count++;
}
});
});
// IF you want it:
// Check if IsArchived exists on the top level of the JSON response and increase count
if(jsonData.IsArchived) {
count++;
}
// IF you want it:
// Create a Postman environment variable and assign the value of count to it
pm.environment.set("count", count);
}
Additional info:
The , after the following object is not needed. It invalidates the JSON:
{
"Id": 33,
"DisplayName": "Long Description",
"IsArchived": false
}, <--

Fuzzy search deep array only on certain properties

I have a JSON dataset which could be very large when it returns, with the following structure for each object:
{
"ctr": 57,
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}
Now I want to search all objects in the array and return only objects which include a string of some sort, but I only want to search certain properties.
I basically have another array which contains the keys I want to search, e.g.
const iteratees = ['ctr', 'property.value', 'status.stage']
I have lodash available within the project, but I have no idea where to start.
Any ideas?
You could use filter(), some() and reduce() to do this.
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if(a) return a[b];
}, o);
if(res) {
if((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
var arr = [{
"ctr": 'lorem',
"averageECPC": 23,
"cost": 2732.54,
"margin": 66,
"profit": 2495.9,
"property": {
"value": "Izzby",
"uri": "/Terrago/2"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 2573.13,
"compare": 0
},
"children": [{
"ctr": 79,
"averageECPC": 54,
"cost": 3554.78,
"margin": 88,
"profit": 3145.81,
"property": {
"value": "Comvex",
"uri": "/Octocore/4"
},
"status": {
"content": "<p>Some Content</p>",
"stage": 1
},
"alerts": {
"status": 2
},
"revenue": {
"value": 1247.92,
"compare": 0
}
}]
}, {
name: 'lorem',
ctr: 12,
property: {
value: 1
},
status: {
stage: 1
}
}, {
name: 'ipsum'
}]
const iteratees = ['ctr', 'property.value', 'status.stage'];
var searchFor = 'lo';
var result = arr.filter(function(o) {
return iteratees.some(function(e) {
var res = e.split('.').reduce(function(a, b) {
if (a) return a[b];
}, o);
if (res) {
if ((res).toString().indexOf(searchFor) != -1) return true;
}
})
})
console.log(result)

Filter an array of objects / convert array into another

could you please help me to convert data in format like :
"tanks": [
{
"id": "1",
"name": {
"id": 1,
"tor": "000"
},
"type": {
"id": 1,
"system": "CV-001"
}
}
]
into
"tanks":[
{
"type": 1,
"name": 1
}
]
As you can see, type.id in the first array is the same as just type in the second. It is like I have to iterate through the array(as I have not only one Object in it) and left only needed fields in Objects, but I am stuck.
Hope it is a little informative for you.
You can do this with a simple Array.map()
var obj = {
tanks : [
{
"id": "1",
"name": {
"id": 1,
"tor": "000"
},
"type": {
"id": 1,
"system": "CV-001"
}
},
{
"id": "2",
"name": {
"id": 2,
"tor": "200"
},
"type": {
"id": 2,
"system": "CV-002"
}
}
]
};
obj.tanks = obj.tanks.map(function(item) {
return {
name : item.name.id,
type : item.type.id
};
});
console.log(obj);
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Categories

Resources