I Need to find multiple elements in a nested array and return it as an array.
I use the reduce function to find the elements, but it returns only one record.
Nested array:
{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Федулов Владислав Владиславович",
"phoneNumber": "+7 (927) 999 9999",
"email": "qweeqwe#mail.ru",
"id": 24,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Ширяев Феликс Богуславович",
"phoneNumber": "+7 (123) 456 7810",
"email": "test#test.ru",
"id": 47,
"parent": 24,
"cardType": 0,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 58,
"parent": 47,
"cardType": 0,
"childrens": null
}
]
}
]
},
{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Игнатьева Женевьева Павловна",
"phoneNumber": "+7 (777) 777 7777",
"email": "igp#sks.ru",
"id": 3,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Меретин Викентий Васильевич",
"phoneNumber": "+7 (917) 193 5222",
"email": "keshman#gmail.com",
"id": 2,
"parent": 3,
"cardType": 1,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Климаков Алексей Александрович",
"phoneNumber": "+7 (903) 888 8888",
"email": "krenog#gmail.com",
"id": 20,
"parent": 2,
"cardType": 1,
"childrens": null
}
]
}
]
}
and the reduce function:
var array = store.managersTree.treeNodes;
var items = [];
const findItemNested = (arr, searchString, nestingKey) => (
arr.reduce((a, item) => {
if (a) return a;
if (item.text.indexOf(searchString)!==-1 ||
item.phoneNumber.indexOf(searchString)!==-1 ||
item.email.indexOf(searchString)!==-1) return item;
if (item[nestingKey]) return findItemNested(item[nestingKey],
searchString, nestingKey)
}, [])
);
const element = findItemNested(array, searchString, "childrens");
I am trying to find a record matching at least one criteria, expecting that reduce returns multiple records, but this returns only one record, despite there were multiple records found.
Any help would be appreciated.
UPD: searchString could be string like phoneNumber, text or email
You could reduce the array recursively.
Destructure the object in reduce parameter to get nestingKey and other properties separately
Create an array of filterKeys which have the keys which you want to search for searchString.
Use some to check if any of the fields in the object have a value which includes the searchString.
If nestingKey exists, you can push the nested items to the accumulator array.
const input=[{"type":"group","level":0,"expand":"-closed","selected":false,"text":"Федулов Владислав Владиславович","phoneNumber":"+7 (927) 999 9999","email":"qweeqwe#mail.ru","id":24,"parent":null,"cardType":0,"childrens":[{"type":"group","level":1,"expand":"-closed","selected":false,"text":"Ширяев Феликс Богуславович","phoneNumber":"+7 (123) 456 7810","email":"test#test.ru","id":47,"parent":24,"cardType":0,"childrens":[{"type":"manager","level":2,"expand":"-empty","selected":false,"text":"Колесова Анастасия Олеговна","phoneNumber":"+7 (900) 000 0001","email":"eprosvirina#baccasoft.ru","id":58,"parent":47,"cardType":0,"childrens":null}]}]},{"type":"group","level":0,"expand":"-closed","selected":false,"text":"Игнатьева Женевьева Павловна","phoneNumber":"+7 (777) 777 7777","email":"igp#sks.ru","id":3,"parent":null,"cardType":0,"childrens":[{"type":"group","level":1,"expand":"-closed","selected":false,"text":"Меретин Викентий Васильевич","phoneNumber":"+7 (917) 193 5222","email":"keshman#gmail.com","id":2,"parent":3,"cardType":1,"childrens":[{"type":"manager","level":2,"expand":"-empty","selected":false,"text":"Климаков Алексей Александрович","phoneNumber":"+7 (903) 888 8888","email":"krenog#gmail.com","id":20,"parent":2,"cardType":1,"childrens":null}]}]}],
filterKeys = ["text", "phoneNumber", "email"];
function findItemNested(array, searchString, nestingKey) {
return array.reduce((acc, { [nestingKey]: nested, ...o }) => {
if (filterKeys.some(k => o[k] && o[k].includes(searchString)))
acc.push(o)
if (nested)
acc.push(...findItemNested(nested, searchString, nestingKey))
return acc;
}, [])
}
console.log(findItemNested(input, "keshman", "childrens"))
console.log(findItemNested(input, "#gmail.com", "childrens"))
Always Array.reduce returns a single value after processing each element in the array. If you want to return matching records in array then you can use, Array.filter,
[1,2,3,4,5].filter((element) => {
return (element === 2 || element ===4);
});
For this above code, the filtered array will be,
[2,4]
And before filter you should either flat the array or should traverse each and every element to filter the array instead of using Array.filter()
Without reduce also its possible,
var s = [{
"type": "group",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Федулов Владислав Владиславович",
"phoneNumber": "+7 (927) 999 9999",
"email": "qweeqwe#mail.ru",
"id": 24,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Ширяев Феликс Богуславович",
"phoneNumber": "+7 (123) 456 7810",
"email": "test#test.ru",
"id": 47,
"parent": 24,
"cardType": 0,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 58,
"parent": 47,
"cardType": 0,
"childrens": null
},
{
"type": "group",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 534,
"parent": 47,
"cardType": 0,
"childrens": null
},
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Колесова Анастасия Олеговна",
"phoneNumber": "+7 (900) 000 0001",
"email": "eprosvirina#baccasoft.ru",
"id": 523,
"parent": 47,
"cardType": 0,
"childrens": null
}
]
}
]
},
{
"type": "manager",
"level": 0,
"expand": "-closed",
"selected": false,
"text": "Игнатьева Женевьева Павловна",
"phoneNumber": "+7 (777) 777 7777",
"email": "igp#sks.ru",
"id": 3,
"parent": null,
"cardType": 0,
"childrens": [
{
"type": "group",
"level": 1,
"expand": "-closed",
"selected": false,
"text": "Меретин Викентий Васильевич",
"phoneNumber": "+7 (917) 193 5222",
"email": "keshman#gmail.com",
"id": 2,
"parent": 3,
"cardType": 1,
"childrens": [
{
"type": "manager",
"level": 2,
"expand": "-empty",
"selected": false,
"text": "Климаков Алексей Александрович",
"phoneNumber": "+7 (903) 888 8888",
"email": "krenog#gmail.com",
"id": 20,
"parent": 2,
"cardType": 1,
"childrens": null
}
]
}
]
}];
function filterRequiredElement(arr, searchString, nestingKey) {
arr.forEach((item, index) => {
if (item.type !== searchString || item.phoneNumber !== searchString || item.email !== searchString) {
arr.splice(index, 1);
}
});
for(let item of arr) {
if (item[nestingKey] !== null) {
filterRequiredElement(item[nestingKey], searchString, nestingKey);
}
}
}
filterRequiredElement(s ,'Климаков Алексей Александрович', 'childrens');
console.log(s);
Related
I have an array of object as shown below:
[{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Okta Verify TOTP",
"provider": "OKTA",
"type": "token:software:totp",
"status": 0,
"id": "osthgek5jmWTckcka0h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
I want to get the distinct object as array based on the **provider**
I tried
[...new Set(item.filter(factor => factor.status == MultiFactorAuthenticationEnrolmentStatus.Enrolled).map(factor => factor.provider))];
This returns string of array such as ["GOOGLE", "OKTA","CUSTOM"]
My requirement is to get the Array of Object such as
[{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
Reference - How to get distinct values from an array of objects in JavaScript?
In the case that you have a preference for taking the first occurrence, you can first map the data into an object based on provider being the key and itself as the value. Once that is done, you can then extract the all of the values with Object#values.
const data = [{
"name": "Okta Verify Push",
"provider": "OKTA",
"type": "push",
"status": 0,
"id": "opfhgfgaidhyBw2H90h7"
}, {
"name": "Okta Verify TOTP",
"provider": "OKTA",
"type": "token:software:totp",
"status": 0,
"id": "osthgek5jmWTckcka0h7"
}, {
"name": "Unknown",
"provider": "CUSTOM",
"type": "claims_provider",
"status": 1,
"id": "clpn4wdtqtH6geILD0h7"
}, {
"name": "Google Authenticator",
"provider": "GOOGLE",
"type": "token:software:totp",
"status": 1,
"id": null
}]
const values = Object.values(
data.reduce((a, b) => {
if (!a[b.provider]) a[b.provider] = b
return a
}, {})
)
console.log(values)
I have JSONArray like given below
[
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
]
Now I want to get the last object from this array who have value "isActive": true using the most effective and optimized way.
{
"id": 3,
"firstName": "efg",
"isActive": true
}
UPDATE :
let getLast = items.filter(item => item.isActive === true);
I tried using the solution provided by #brk. But is there any way to get object using without creating a new array.
First filter with condition isActive === true. This will create a new array , then use array.length-1 to get the last element
let data = [{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
]
let getLast = data.filter(item => item.isActive === true);
console.log(getLast[getLast.length - 1])
var jsonObject = [
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
];
var lastActiveElement = jsonObject.filter( obj => obj.isActive ).pop();
console.log(lastActiveElement);
If this is what you are looking for
var x=[
{
"id": 1,
"firstName": "abc",
"isActive": true
},
{
"id": 2,
"firstName": "cde",
"isActive": false
},
{
"id": 3,
"firstName": "efg",
"isActive": true
},
{
"id": 4,
"firstName": "ghi",
"isActive": false
}
];
//Try the following line
x.filter(x=>x.isActive).slice(-1).pop()
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.
I have a json object that contains multiple parent items (denoted by the parent property being null) which has nested child objects.
{
"aggregateId": null,
"aggregateList": {
"name": "My Grocery Store",
"children": [
{
"name": "Vegetables",
"documentType": "Produce",
"parent": null,
"count": 2,
"children": [{
"name": "Carrots",
"documentType": "Produce",
"parent": null,
"count": 1447,
"children": null
},
{
"name": "Lettuce",
"documentType": "Produce",
"parent": null,
"count": 311,
"children": null
}]
},
{
"name": "Canned Goods",
"documentType": "Cans",
"parent": null,
"count": 583,
"children": null
}
]
}
}
What I am trying to do is when a new object which is a child is returned insert it into the existing array. So for example the object Celery is returned
{
"name": "Celery",
"documentType": "Produce",
"parent": null,
"count": 100,
"children": null
}
Iterate through the tree, locate its parent (produce) and insert it into that array.
{
"aggregateId": null,
"aggregateList": {
"name": "My Grocery Store",
"children": [
{
"name": "Vegetables",
"documentType": "Produce",
"parent": null,
"count": 2,
"children": [{
"name": "Carrots",
"documentType": "Produce",
"parent": null,
"count": 1447,
"children": null
},
{
"name": "Lettuce",
"documentType": "Produce",
"parent": null,
"count": 311,
"children": null
},
{
"name": "Celery",
"documentType": "Produce",
"parent": null,
"count": 100,
"children": null
}]
},
{
"name": "Canned Goods",
"documentType": "Cans",
"parent": null,
"count": 583,
"children": null
}
]
}
}
So far I've tried to iterate through the collection using a for statement but this doesn't seem right and I am not getting results returned. The part that doesn't seem fight is in specifying the location in the tree.
Is there a way via javascript to take the object/array and look at each property to locate the matching value and then insert the new object into that array?
Update:
Sorry I didn't include enough code.
The for loop is wrapped in a function
function nestAssociation(node, oldCollection, newAggregates)
{
var parent = node.documentType; //the parent value of the collection to be inserted
var currentCollection = oldCollection; //previous collection
var newCollection = newAggregates; //the object to be inserted (Celery)
for (var i = 0, iLen = arrayCount; i < iLen; i++) {
if ($scope.myList.children[0].children[i].documentType == node.documentType) {
console.log('true');
var myNewList = $scope.myList.children.concat(newCollection.children)
$scope.myList = myNewList;
console.log(myNewList)
}
}
I have following JSON data received form REST api.
[
{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
},
{
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}
]
I'm trying to display it using directive ng-repeat. Using track by object.id.
It is used like this:
<tr ng-repeat="chapter in chapters | filter: search track by chapter.id">
But the problem is that the ngRepeat:Dupes error still appear. I have checked the data contained in JSON, but there is no duplicite id in it. Do you know why the ngRepeat:Dupes error persists?
Based on the data given, no duplicate error is thrown, below is jsfiddle.
<div ng-controller="MainCtrl">
<input type="text" ng-model="search">
<div ng-repeat="chapter in chapters | filter: search track by chapter.id">{{chapter.id}}</div>
</div>
controller
$scope.chapters = [{
"names": {
"en": "test123"
},
"children": [],
"id": "68d87e8c-42f5-4f11-b25a-b30624246c3b",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test123",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}, {
"names": {
"en": "test456"
},
"children": [],
"id": "d175e6d1-874e-4909-afb2-790c0a940c3f",
"version": 1,
"code": "0",
"order": 0,
"country": "HR",
"name": "test456",
"parent": null,
"selected": false,
"hasQuestions": false,
"level": 1,
"state": "original",
"hasChildChapters": false
}]
http://jsfiddle.net/75sdsuz2/2/