I have an array of object and it looks like this, I want to remove attributeType object and get a new one.
How can I delete object in array?
{
"id": 2,
"version": 1,
"name": "test",
"attributeInputs": {
"0": {
"id": 4,
"position": 0,
"label": "display",
"attributeType": {
"id": 3,
"label": "Input",
"fieldType": "TEXT"
},
},
"1": {
"id": 5,
"position": 3,
"label": "price",
"attributeType": {
"id": 5,
"label": "Price",
"fieldType": "PRICE"
},
}
}
}
I would like delete AttributeType and it should look like this:
{
"id": 2,
"version": 1,
"name": "test",
"attributeInputs": {
"0": {
"id": 4,
"position": 0,
"label": "display",
},
"1": {
"id": 5,
"position": 3,
"label": "price",
}
}
}
You can use Object.keys() on the attributeInputs field and delete the key attributeType:
const obj = {
"id": 2,
"version": 1,
"name": "test",
"attributeInputs": {
"0": {
"id": 4,
"position": 0,
"label": "display",
"attributeType": {
"id": 3,
"label": "Input",
"fieldType": "TEXT"
},
},
"1": {
"id": 5,
"position": 3,
"label": "price",
"attributeType": {
"id": 5,
"label": "Price",
"fieldType": "PRICE"
},
}
}
}
Object.keys(obj.attributeInputs).forEach(o => delete obj.attributeInputs[o].attributeType )
console.log(obj)
first, let's store the object in a variable called myObject.
now let's work on the variable myObject.
myObject.attributeInputs = Object.keys(myObject.attributeInputs).map(key=>{
const {attributeType,...newObj}=myObject.attributeInputs[key];
return newObj;
});
it's not clean code, but it works.
Related
I'm attempting to write a function to recursively delete nodes based on Parent-child relationships.
function recursiveDelete(parentNode, deleteStack) {
let toDelete = allNodes.filter(function (node) {
return node.Parent !== undefined && node.Parent[0] === parentNode.ID;
})
toDelete.forEach((childNode) => {
deleteStack.push(...recursiveDelete(childNode, toDelete));
});
return deleteStack;
}
When I run this code, it create arrays with multiple copies of each node to be deleted, which seems to correspond to the height of the tree. It still seems to be running in polynomial time, but it isn't ideal.
edit - below is an example of what gets returned when I run the function on allNodes, with the duplicates.
[
{
"ID": "recymaQKcdrGzAqRM",
"Name": "Depth 1",
"Type": "task",
"Duration": 0.1,
"Priority": "6",
"Parent": [
"recQT9BPQtqs7Cg0U"
],
"Predecessors": [
"recKwlRnVhKik3SZF"
],
"depth": 2
},
{
"ID": "recRcn1t2Nkupcb9L",
"Name": "Start",
"Type": "start",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recymaQKcdrGzAqRM"
]
},
{
"ID": "recBmEBC1bo1CcJ8i",
"Name": "Depth 2",
"Type": "task",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recymaQKcdrGzAqRM"
],
"Predecessors": [
"recRcn1t2Nkupcb9L"
]
},
{
"ID": "recRcn1t2Nkupcb9L",
"Name": "Start",
"Type": "start",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recymaQKcdrGzAqRM"
]
},
{
"ID": "recBmEBC1bo1CcJ8i",
"Name": "Depth 2",
"Type": "task",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recymaQKcdrGzAqRM"
],
"Predecessors": [
"recRcn1t2Nkupcb9L"
]
},
{
"ID": "rec7qUgu7kpnQ06s7",
"Name": "Start",
"Type": "start",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recBmEBC1bo1CcJ8i"
]
},
{
"ID": "rec5Lyx3zuAsCPv9W",
"Name": "Depth 3",
"Type": "task",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recBmEBC1bo1CcJ8i"
],
"Predecessors": [
"rec7qUgu7kpnQ06s7"
]
},
{
"ID": "rec7qUgu7kpnQ06s7",
"Name": "Start",
"Type": "start",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recBmEBC1bo1CcJ8i"
]
},
{
"ID": "rec5Lyx3zuAsCPv9W",
"Name": "Depth 3",
"Type": "task",
"Duration": 0.1,
"Priority": "5",
"Parent": [
"recBmEBC1bo1CcJ8i"
],
"Predecessors": [
"rec7qUgu7kpnQ06s7"
]
}
]
My current hack is to just remove duplicates:
[...new Set(recursiveDelete(allNode, [allNode]))]
But I'd like to figure out what I'm doing wrong. Thanks!
edit, I think I figured it out. Instead of passing toDelete to the recursive function, I should've only been passing [childNode], so the correct code looks like this:
function recursiveDelete(parentNode, deleteStack) {
let toDelete = allNodes.filter(function (node) {
return node.Parent !== undefined && node.Parent[0] === parentNode.ID;
});
toDelete.forEach((childNode) => {
deleteStack.push(...recursiveDelete(childNode, [childNode]));
});
return deleteStack;
}
I have two arrays of objects. I want to look at the first one, find typeId then look in the second array for the match (states.typeId == stateTypes.id) then merge those properties into the first array's found match object. If the properties have same key append "stateType" to the property name if not just bring it over. I think an example would best explain it.
Array of objects
"states": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
],
"stateTypes": [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
],
Wanted array
"newArray": [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 1,
"stageType-name": "PENDING",
"stageType-description": "Pending",
"stageType-label": "Pending"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 3,
"stageType-name": "COMPLETED",
"stageType-description": "Completed",
"stageType-label": "Completed"
},
{
"id": 3,
"typeId": 2,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text",
"newIncomingKey": "Text",
"stageType-id": 2,
"stageType-name": "IN_PROGRESS",
"stageType-description": "In Progress",
"stageType-label": "In Progress"
}
],
Something like this:
const state = [
{
"id": 1,
"typeId": 1,
"name": "CREATED",
"description": "Created",
"label": "Created",
"perviousNotMatchKey": "Text"
},
{
"id": 2,
"typeId": 3,
"name": "ASSIGNED",
"description": "Assigned",
"label": "Assigned",
"perviousNotMatchKey": "Text"
},
{
"id": 3,
"typeId": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"perviousNotMatchKey": "Text"
}
];
const stateTypes = [
{
"id": 1,
"name": "PENDING",
"description": "Pending",
"label": "Pending",
"newIncomingKey": "Text"
},
{
"id": 2,
"name": "IN_PROGRESS",
"description": "In Progress",
"label": "In Progress",
"newIncomingKey": "Text"
},
{
"id": 3,
"name": "COMPLETED",
"description": "Completed",
"label": "Completed",
"newIncomingKey": "Text"
}
];
const temp = {};
for (const obj1 of state) {
temp[obj1.id] = { ...obj1 };
}
for (const obj2 of stateTypes) {
const destination = temp[obj2.id];
for (const [key, value] of Object.entries(obj2)) {
if (destination[key]) destination[`stateTypes-${key}`] = value;
else destination[key] = value;
}
}
const newArray = Object.values(temp);
console.log(newArray);
I've got a array of object in which the children got arrays as well:
[{
"id": 0,
"name": "Primary",
"xPoints": [{
"id": 0,
"name": "Untitledtest12",
"type": "custom",
"description": ""
}, {
"id": 1,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 2,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 3,
"name": "asd123",
"description": "",
"type": "custom"
}, {
"id": 4,
"name": "a",
"description": "a",
"type": "custom"
}]
}, {
"id": 1,
"name": "Untitled X Line",
"xPoints": [
{},
{},
{},
{},
{},
{
"id": 0,
"name": "this is the farthest",
"type": "custom",
"description": ""
},
{}
]
}]
what I'm looking for is a way to get the "farthest" of object index on these childrens of the main array
Expected result:
index: 6 from second's "Untitled X Line"
You can use recursion to get the depth of the object.
const obj = [{
"id": 0,
"name": "Primary",
"xPoints": [{
"id": 0,
"name": "Untitledtest12",
"type": "custom",
"description": ""
}, {
"id": 1,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 2,
"name": "asd",
"description": "",
"type": "custom"
}, {
"id": 3,
"name": "asd123",
"description": "",
"type": "custom"
}, {
"id": 4,
"name": "a",
"description": "a",
"type": "custom"
}]
}, {
"id": 1,
"name": "Untitled X Line",
"xPoints": [
{},
{},
{},
{},
{},
{
"id": 0,
"name": "this is the farthest",
"type": "custom",
"description": ""
},
{}
]
}]
function depthOf(object) {
var level = 1;
for(var key in object) {
if (!object.hasOwnProperty(key)) continue;
if(typeof object[key] == 'object'){
var depth = depthOf(object[key]) + 1;
level = Math.max(depth, level);
}
}
return level;
}
var maxLevels = 0;
var maxObj;
obj.forEach(e => maxLevels < depthOf(e) ? maxObj = e : '');
console.log(maxObj);
I am using GraphQl and below is my data
{
"data": {
"food": {
"category": [
{
"id": 4,
"name": "meat"
},
{
"id": 3,
"name": "brink"
},
{
"id": 2,
"name": "vegetable"
},
{
"id": 5,
"name": "bread"
},
{
"id": 1,
"name": "cookie"
},
{
"id": -1,
"name": "candy"
},
]
}
}
}
How can I sort this when I execute the query and the return data will be like below
{
"data": {
"food": {
"category": [
{
"id": -1,
"name": "candy"
},
{
"id": 1,
"name": "cookie"
},
{
"id": 2,
"name": "vegetable"
},
{
"id": 3,
"name": "brink"
},
{
"id": 4,
"name": "meat"
},
{
"id": 5,
"name": "bread"
},
]
}
}
}
I try
{
food {
category(orderBy: id_ASC){
id
name
}
}
}
but show
unknown argument "orderBy" on field "category" of type Food.
Anyone know how to do this?
Thanks.
I need help with matching childrens in the same object based on parent ID property....
JSON:
{
"1": {
"id": 1,
"name": "My Crib",
"type": "Home",
"order": 0,
"parent": null
},
"2": {
"id": 2,
"name": "First floor",
"type": "Floor",
"order": 1,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"3": {
"id": 3,
"name": "Garage",
"type": "Garage",
"order": 2,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"4": {
"id": 4,
"name": "Garden",
"type": "Garden",
"order": 3,
"parent": {
"id": 1,
"url": "http://localhost:8080/rest/areas/1"
}
},
"5": {
"id": 5,
"name": "Entrance hall",
"type": "Entrance",
"order": 1,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"6": {
"id": 6,
"name": "Kitchen",
"type": "Kitchen",
"order": 2,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"7": {
"id": 7,
"name": "Living room",
"type": "LivingRoom",
"order": 3,
"parent": {
"id": 2,
"url": "http://localhost:8080/rest/areas/2"
}
},
"8": {
"id": 8,
"name": "Dog house",
"type": "DogHouse",
"order": 1,
"parent": {
"id": 4,
"url": "http://localhost:8080/rest/areas/4"
}
}
}
const mappedAreas = Object.keys(areas).map(function(key) {
const area = areas[key];
const wrappers = statics["test.area.groups"]["wrappers"];
const leafs = statics["test.area.groups"]["leafs"];
if (wrappers.indexOf(area["type"]) > -1) {
//In the array!
//If not a home
if(area["type"] != "Home"){
console.log("floor:")
console.log(area)
//get wrapper childrens
const children = Object.keys(areas).map(function(key) {
const child = areas[key];
if(child["type"] != "Home"){
if(child["parent"]["id"] == area["id"] && leafs.indexOf(child["type"]) > -1){
console.log(child);
//return <Area key={"area_index_"+key} areaData={area} />
}
}
});
console.log("endfloor/")
//return <Area key={"area_index_"+key} areaData={area} />
}
} else {
//Not in the array
}
});
Is there any way to do this better than having one map inside another map?
Basicly I get all areas in one object, and I want to match them with eachother, looking for ["parent"]["id"] to match with ["id"].
Tell me if you need more description.