Search for related properties in the same object. Javascript - javascript

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.

Related

Delete array object in java script

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.

How to find the farthest object in a sub array of a array with empty objects?

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);

Need to add parentId to child in multi-dimensional Array of JSON data

I have a multidimensional array of JSON data with 'n' number of nested children. My task is to add UniqueId to parent JSON and that uniqueId should be added as parentId to the child. Can you please help in javascript. Thanks
Note:
The number of child its recursive, and there can be any number of children. For this purpose, we can have a deep level of three
Input :
[{
"text": 1527978678434,
"value": 1527978678434,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"children": [{
"text": 474194771845,
"value": 474194771845,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"children": []
},
]
}]
The Required Output:
[{
"text": 1527978678434,
"value": 1527978678434,
"parentId": 0,
"uniqueId": 1,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"parentId": 1,
"uniqueId": 2,
"children": [{
"text": 474194771845,
"value": 474194771845,
"parentId": 2,
"uniqueId": 3,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"parentId": 2,
"uniqueId": 4,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"parentId": 1,
"uniqueId": 5,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"parentId": 1,
"uniqueId": 6,
"children": []
},
{
"text": 111221786011,
"value": 111221786011,
"parentId": 1,
"uniqueId": 7,
"children": []
},
{
"text": 641372005975,
"value": 641372005975,
"parentId": 1,
"uniqueId": 8,
"children": [{
"text": 23082640100,
"value": 23082640100,
"parentId": 8,
"uniqueId": 9,
"children": []
}]
}
]
}]
Consider data is your array.
Just do the DFS and alter the data on the fly. + Having a global to hold the increased ID.
let lastUniqueId = 0;
function addIds(children, parentId) {
(children || []).forEach(r => {
r.parentId = parentId;
r.uniqueId = ++lastUniqueId;
addIds(r.children, r.uniqueId);
});
}
addIds(data, lastUniqueId);
This seems something you can do using a recursive function while keeping track of a shared uniqueId counter, assuming the nesting doesn't go incredibly deep:
const data = [{
"text": 1527978678434,
"value": 1527978678434,
"children": [{
"text": 1292232152442,
"value": 1292232152442,
"children": [{
"text": 474194771845,
"value": 474194771845,
"children": []
},
{
"text": 468086178830,
"value": 468086178830,
"children": []
}
]
},
{
"text": 1067869237589,
"value": 1067869237589,
"children": []
},
{
"text": 1166591731429,
"value": 1166591731429,
"children": []
},
]
}];
function convert(array) {
let uniqueId = 0;
function mapData(data, parentId) {
const myId = ++uniqueId;
const result = {
...data, // copy all data
uniqueId, parentId, // add our new fields
// and handle the children recursively
children: data.children.map(c => mapData(c, myId)),
};
// Don't mind this, just tricking JS in displaying the children array at the bottom during console.log
const children = result.children; delete result.children; result.children = children;
return result;
}
return array.map(d => mapData(d, 0));
}
const result = convert(data);
console.log(result);

Filter Array of user objects by array of roles in Javascript

I have an array of user objects.
I want to filter them based on the array of user roles.
filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const users = [{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 2,
"email": "user2#test.com",
"name": "User2",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 3,
"email": "user3#test.com",
"name": "User3",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
}]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 5,
"email": "user5#test.com",
"name": "User5",
"roles": [{
"id": 5,
"code": "ROLE_LAWYER",
"name": "Lawyer"
}]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']
const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))
console.log(filteredUsers)
Expected result
[{
"id": 1,
"email": "user1#test.com",
"name": "User1",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 4,
"email": "user4#test.com",
"name": "User4",
"roles": [{
"id": 1,
"code": "ROLE_ADMINISTRATOR",
"name": "Administrator"
},
{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}
]
}, {
"id": 6,
"email": "user6#test.com",
"name": "User6",
"roles": [{
"id": 2,
"code": "ROLE_SELLER",
"name": "Seller"
}]
},
{
"id": 7,
"email": "user7#test.com",
"name": "User7",
"roles": [{
"id": 9,
"code": "ROLE_SLA",
"name": "sla"
}]
},
{
"id": 8,
"email": "user8#test.com",
"name": "User8",
"roles": [{
"id": 8,
"code": "ROLE_BANK",
"name": "Bank"
}]
},
{
"id": 9,
"email": "user9#test.com",
"name": "User9",
"roles": [{
"id": 7,
"code": "ROLE_CPF",
"name": "CPF"
}]
}
]
I am trying to image what result you want to receive....
You want to filter of users by array with possible roles, another worlds if user has one of roles of filter array you want to pass his to 'filteredUsers' array?
filter.includes(role.id) - I guess it is wrong, may be you want to
filter by role.code?
Array.find() doesn't support Explorer
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.includes() - doesn't support Explorer too.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
I think the reason why that was happening was that you are trying to match the role id with the 'filter' array which contains the role code. From what i observed from the expected answer, you are looking for user who has any one of their role fits the filter. So do the following will filter by role code and with the expected result
const filteredUsers = users.filter((user) => {
return user.roles.map(role=>filter.includes(role.code)).includes(true)
})
this line of code basically map the filter to every role object to every user, if their role code is included in the filter it will add a true to the array(return array of map()) and for the filter function if the map() return array contains true then true(so basically a || for the whole array)

Using angularjs forEach loops

I am getting this type of json in my $scope of angularjs:
$scope.someStuff = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [
{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
}
I want to use angularjs forEach loop and i want to get only category name,
My expected output:
[{"name":"software"}, {"name":"hardware"}, {"name":"waterwash"}]
You can use Array.map()
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
$scope.someStuff.categoryservices.map((x) => { return { name: x.category.name}})
var obj = {
"id": 2,
"service": "bike",
"min": "22",
"per": "100",
"tax": "1",
"categoryservices": [{
"id": 32,
"category": {
"id": 1,
"name": "software"
}
},
{
"id": 33,
"category": {
"id": 2,
"name": "hardware"
}
},
{
"id": 34,
"category": {
"id": 3,
"name": "waterwash"
}
}
]
};
console.log(obj.categoryservices.map((x) => {
return {
name: x.category.name
}
}))
You can use map method by passing a callback function as parameter.
const someStuff = { "id": 2, "service": "bike", "min": "22", "per": "100", "tax": "1", "categoryservices": [ { "id": 32, "category": { "id": 1, "name": "software" } }, { "id": 33, "category": { "id": 2, "name": "hardware" } }, { "id": 34, "category": { "id": 3, "name": "waterwash" } } ] }
let array = someStuff.categoryservices.map(function({category}){
return {'name' : category.name}
});
console.log(array);

Categories

Resources