Javascript - Multiple nested filter expressions - javascript

Have this JSON object in JavaScript that comes from an API:
[
{
"id": 1,
"label": "Breakfast",
"subCategories": [
{
"id": 100,
"label": "Cereals, Muesli",
"items": [
{
"productId": "4fdddf1d-8d31-411d-a908-5edd68a775b7",
"label": "Bircher Muesli"
},
{
"productId": "000673e7-47ec-4dce-a940-ad4aacbd7d73",
"label": "Individual Cereals"
},
{
"productId": "0f739661-5531-4734-9dfd-e145b60667cc",
"label": "Organic Porridge Oats"
}
]
},
{
"id": 101,
"label": "Eggs, Omelettes",
"items": [
{
"productId": "6d608133-ab44-4f9d-ab8e-fc6a3f955397",
"label": "Crushed Avocado with Soughdough Toast"
},
{
"productId": "fcfe91ab-e9b1-4dc0-8c57-ffb9646e0658",
"label": "Crushed Avocado with Crispy Bacon"
},
{
"productId": "2a80e48b-76f6-4bda-abf3-ec8dc7bf1419",
"label": "Crushed Avocado with Smoked Salmon"
},
{
"productId": "ae35e949-abf3-4795-a5df-9af4250c2185",
"label": "Egg White Omelette"
}
]
}
]
},
{
"id": 2,
"label": "Light Lunch",
"subCategories": [
{
"id": 103,
"label": "Condiments",
"items": [
{
"productId": "25503a9b-b553-4b56-a152-49e4121cf4ae",
"label": "Butter"
},
{
"productId": "c1dd9761-f170-4e6a-a7d7-5519a4213874",
"label": "Jam"
}
]
},
{
"id": 104,
"label": "Yoghurts",
"items": [
{
"productId": "938fed24-6d4c-e0cd-8303-0fcd42c87be4",
"label": "Fruit Yoghurt",
},
{
"productId": "62137176-0966-4424-9093-51bd7871d31b",
"label": "Greek Yoghurt",
},
{
"productId": "307e59c4-b103-43d4-988c-75ee539d5d75",
"label": "Granola Parfait: Layers of Berries, Fruit Granola, Yoghurt & Honey",
}
]
}
]
}
]
I need to filter this array above with the search query (Eg: Greek) against the items.label property and have it returned the filtered outcome like below:
[
{
"id": 2,
"label": "Light Lunch",
"subCategories": [
{
"id": 104,
"label": "Yoghurts",
"items": [
{
"productId": "62137176-0966-4424-9093-51bd7871d31b",
"label": "Greek Yoghurt",
}
]
}
]
}
]
I've tried various implementation with filter() with nested some() as seen on StackOverflow but did not return the desired result. Currently this works but only the top level category is filtered and the nested subcategory only exist if there's a match for item.
var searchQuery="Greek";
var data=[]; //JSON omitted for brevity.
var result = data.filter(a=>{
return a.subCategories.some(b=> {
return b.items.some(c=> new RegExp(searchQuery,"i").test(c.label));
});
});
Any help would be greatly appreciated.

You can use Array.reduce for this, iterating first over each of the categories, then each of the subcategories, only adding the subcategory to the output if one of its items contains the search query, and then only adding the category to the output if one of the subcategories contains the search query:
const data = [{
"id": 1,
"label": "Breakfast",
"subCategories": [{
"id": 100,
"label": "Cereals, Muesli",
"items": [{
"productId": "4fdddf1d-8d31-411d-a908-5edd68a775b7",
"label": "Bircher Muesli"
},
{
"productId": "000673e7-47ec-4dce-a940-ad4aacbd7d73",
"label": "Individual Cereals"
},
{
"productId": "0f739661-5531-4734-9dfd-e145b60667cc",
"label": "Organic Porridge Oats"
}
]
},
{
"id": 101,
"label": "Eggs, Omelettes",
"items": [{
"productId": "6d608133-ab44-4f9d-ab8e-fc6a3f955397",
"label": "Crushed Avocado with Soughdough Toast"
},
{
"productId": "fcfe91ab-e9b1-4dc0-8c57-ffb9646e0658",
"label": "Crushed Avocado with Crispy Bacon"
},
{
"productId": "2a80e48b-76f6-4bda-abf3-ec8dc7bf1419",
"label": "Crushed Avocado with Smoked Salmon"
},
{
"productId": "ae35e949-abf3-4795-a5df-9af4250c2185",
"label": "Egg White Omelette"
}
]
}
]
},
{
"id": 2,
"label": "Light Lunch",
"subCategories": [{
"id": 103,
"label": "Condiments",
"items": [{
"productId": "25503a9b-b553-4b56-a152-49e4121cf4ae",
"label": "Butter"
},
{
"productId": "c1dd9761-f170-4e6a-a7d7-5519a4213874",
"label": "Jam"
}
]
},
{
"id": 104,
"label": "Yoghurts",
"items": [{
"productId": "938fed24-6d4c-e0cd-8303-0fcd42c87be4",
"label": "Fruit Yoghurt",
},
{
"productId": "62137176-0966-4424-9093-51bd7871d31b",
"label": "Greek Yoghurt",
},
{
"productId": "307e59c4-b103-43d4-988c-75ee539d5d75",
"label": "Granola Parfait: Layers of Berries, Fruit Granola, Yoghurt & Honey",
}
]
}
]
}
];
const searchQuery = "Greek";
const regex = new RegExp(searchQuery, "i");
const result = data.reduce((cats, cat) => {
cat.subCategories = cat.subCategories.reduce((subs, sub) => {
sub.items = sub.items.filter(item => regex.test(item.label));
if (sub.items.length) subs.push(sub);
return subs;
}, []);
if (cat.subCategories.length) cats.push(cat);
return cats;
}, []);
console.log(result);

Here I have a working example which returns your requested output:
function finder(data, query) {
for(let i in data) {
// return the item if the label contains the search query
if(new RegExp(query,"i").test(data[i].label)) return data[i]
// go deeper in subCategories if exist
if(data[i].subCategories) {
let sub = finder(data[i].subCategories, query)
if(sub) {
data[i].subCategories = [sub]
return data[i]
}
// go deeper in items if exist
} else if(data[i].items){
let item = finder(data[i].items, query)
if(item) {
data[i].items = [item]
return data[i]
}
}
}
// didn't find the search query in this branch
return false
}
console.log(finder(data, 'Greek'))
with data your input data

Related

nest items in JSON based on value?

Trying to get my head around this one..
Incoming data looks like:
[
{
"value": {
"label": "MZ Algal bloom",
"type": "case",
"incident": {
"name": "Algal bloom"
},
"personName": "Lionel Carter"
}
},
{
"value": {
"label": "BW Algal bloom",
"type": "case",
"incident": {
"name": "Algal bloom"
},
"personName": "Jerome Yost"
}
},
{
"value": {
"label": "Detergent",
"type": "case",
"incident": null,
"personName": "Jerald Legros"
}
}
]
I would like to transform this into
[
{
"label": "Algal bloom",
"children": [
{ "label": "Lionel Carter", "type": "case"},
{ "label": "Jerome Yost", "type": "case" }]
},
{ "label": "Detergent", "type": "case" }
]
Basically, the rule is that if incident is not NULL then the incident name becomes the parent and the children hold the personName - otherwise we simply pass through the label and type. I can walk the array and switch out the label with the incident name, but I'm not sure how to group up the incidents..
It's basic grouping with an exception for elements without incident.
You can group the elements without incident in a separate group:
const data = [{"value": {"label": "MZ Algal bloom","type": "case","incident": {"name": "Algal bloom"},"personName": "Lionel Carter"}},{"value": {"label": "BW Algal bloom","type": "case","incident": {"name": "Algal bloom"},"personName": "Jerome Yost"}},{"value": {"label": "Detergent","type": "case","incident": null,"personName": "Jerald Legros"}}];
function group(data) {
const result = data.reduce((acc, { value }) => {
if (!value.incident) {
acc.ungrouped.push({ label: value.label, type: value.type });
} else {
if (!acc.groups[value.incident.name]) acc.groups[value.incident.name] = { label: value.incident.name, children: [] };
acc.groups[value.incident.name].children.push({ label: value.personName, type: value.type });
}
return acc;
}, { groups: {}, ungrouped: [] });
return [...Object.values(result.groups), ...result.ungrouped];
}
console.log(group(data));

if name of array object same then combine array object in ts

I have an array of objects coming from backend.
var values = [
{
"name": "Patient Introductions",
"series": [
{
"name": "Organization ABC",
"value": 3
}
]
},
{
"name": "Patient Assessment",
"series": [
{
"name": "Organization ABC",
"value": 2.5
}
]
},
{
"name": "Patient Introductions",
"series": [
{
"name": "Organization XYZ",
"value": 2.5
}
]
},
{
"name": "Patient Assessment",
"series": [
{
"name": "Organization XYZ",
"value": 3.3
}
]
},
];
I want to combine the inner array's objects and get one single array of objects for same name of objects.
var output = [
{
"name": "Patient Introductions",
"series": [
{
"name": "Organization ABC",
"value": 3
},
{
"name": "Organization XYZ",
"value": 2.5
}
]
},
{
"name": "Patient Assessment",
"series": [
{
"name": "Organization ABC",
"value": 2.5
},
{
"name": "Organization XYZ",
"value": 3.3
}
]
},
];
I think, I need to use reduce but not sure how I can combine objects of series of same name.
Please help and guide. Thanks
You are right with reducer
var values = [
{
"name": "Patient Introductions",
"series": [
{
"name": "Organization ABC",
"value": 3
}
]
},
{
"name": "Patient Assessment",
"series": [
{
"name": "Organization ABC",
"value": 2.5
}
]
},
{
"name": "Patient Introductions",
"series": [
{
"name": "Organization XYZ",
"value": 2.5
}
]
},
{
"name": "Patient Assessment",
"series": [
{
"name": "Organization XYZ",
"value": 3.3
}
]
},
];
var result = values.reduce((acc, curr) => {
var existing = acc.find(element => element.name === curr.name);
if(existing) {
existing.series.push(...curr.series);
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(result);
Make a new empty array
loop through your original array if the name does not exist in the
new array then add to it'
if the name existed just add the new array to it
this way ensures no duplicate name will be in the array
var values = [
{
name: "Patient Introductions",
series: [
{
name: "Organization ABC",
value: 3,
},
],
},
{
name: "Patient Assessment",
series: [
{
name: "Organization ABC",
value: 2.5,
},
],
},
{
name: "Patient Introductions",
series: [
{
name: "Organization XYZ",
value: 2.5,
},
],
},
{
name: "Patient Assessment",
series: [
{
name: "Organization XYZ",
value: 3.3,
},
],
},
];
let newValues = [];
values.forEach((value) => {
let index = newValues.findIndex((item) => item.name === value.name);
if (index === -1) {
newValues.push({ name: value.name, series: [value.series[0]] });
} else {
newValues[index].series.push(value.series[0]);
}
});
console.log(newValues)

React - Filter Multidimensional array with another array

I want to return only matches results.
My array:
products: [
{
"id": 1,
"name": "Product 1",
"concepts": [
{
"id": 10,
"name": "Blabla"
},
{
"id": 15,
"name": "Zlazla"
}
]
},
{
"id": 2,
"name": "Product 2",
"concepts": [
{
"id": 14,
"name": "Gulagula"
},
{
"id": 15,
"name": "Zlazla"
}
]
}
]
I want to filter products which only have one of the concepts below.
concepts array:
['14', '15']
Couldn't solve this in an easy way.
You can try this way:
var products= [
{
"id": 1,
"name": "Product 1",
"concepts": [
{
"id": 10,
"name": "Blabla"
},
{
"id": 15,
"name": "Zlazla"
}
]
},
{
"id": 2,
"name": "Product 2",
"concepts": [
{
"id": 14,
"name": "Gulagula"
},
{
"id": 15,
"name": "Zlazla"
}
]
}
]
var products = products.filter((product) => product.concepts = product.concepts.filter( (x) => x.id == 14 || x.id == 15));
console.log(products);

Replace all object properties with a new name

I want to replace all object properties label to the property name text.
Given:
[
{
"value": "45a8",
"label": "45A8",
"children": [
{
"value": "45a8.ba08",
"label": "BA08",
"children": [
{
"value": "45a8.ba08.45a8",
"label": "45A8"
}
],
"checked": false
}
]
},
...
]
Needed:
[
{
"value": "45a8",
"text": "45A8",
"children": [
{
"value": "45a8.ba08",
"text": "BA08",
"children": [
{
"value": "45a8.ba08.45a8",
"text": "45A8"
}
],
"checked": false
}
]
},
...
]
You can first stringify the the data with JSON.stringify() then replace the string. Finally parse the string with JSON.parse():
var data = [
{
"value": "45a8",
"label": "45A8",
"children": [
{
"value": "45a8.ba08",
"label": "BA08",
"children": [
{
"value": "45a8.ba08.45a8",
"label": "45A8"
}
],
"checked": false
}
]
}
];
data = JSON.parse(JSON.stringify(data).replaceAll('"label"', '"text"'));
console.log(data);
Update: You can try using RegEx like the following way:
var data = [
{
"value": "45a8",
"label": "45A8",
"children": [
{
"value": "45a8.ba08",
"label": "BA08",
"children": [
{
"value": "45a8.ba08.45a8",
"label": "45A8"
}
],
"checked": false
}
]
}
];
var find = '"label"';
var re = new RegExp(find, 'g');
data = JSON.parse(JSON.stringify(data).replace(re, '"text"'));
console.log(data);
You can also browse through the keys and find the label key. Get it's value assign it to the new key i.e. text and delete the old key i.e. label.
let data = [
{
"value": "45a8",
"label": "45A8",
"children": [
{
"value": "45a8.ba08",
"label": "BA08",
"children": [
{
"value": "45a8.ba08.45a8",
"label": "45A8"
}
],
"checked": false
}
]
}
]
for (const key in data.keys()){
if(key === "label"){
data.text = data[key]
delete data.key
}
}
You can also do this with using recursion. It could be something like this:
const nestArr=[ { "value": "45a8", "label": "45A8", "children": [ { "value": "45a8.ba08", "label": "BA08", "children": [ { "value": "45a8.ba08.45a8", "label": "45A8" } ], "checked": false } ] }];
changeLabelToText=arr=>{
return arr.map(({label,...rest})=>{
if(rest.children) rest.children = changeLabelToText(rest.children);
return {text:label, ...rest}
})
};
console.log(changeLabelToText(nestArr));

How to get the respective JSON object based on id

how to get the respective nested JSON object based on Id. For example below is my complete JSON.
[
{
"id": 1,
"title": "ASD Headquarters",
"items": [
{
"id": 11,
"title": "San Jose",
"items": [
{
"id": 13,
"title": "Jensen Chapman's Team",
"items": [
{
"id": 14,
"title": "Jimmy John"
},
{
"id": 15,
"title": "Daniel Mills"
},
{
"id": 16,
"title": "Chris Boden"
}
]
}
]
},
{
"id": 12,
"title": "Irvine",
"items": [
{
"id": 23,
"title": "Tracey Chapman's Team",
"items": [
{
"id": 24,
"title": "San Jesus"
},
{
"id": 25,
"title": "Fat Albert"
},
{
"id": 26,
"title": "Connor McDavid"
}
]
}
]
},
{
"id": 30,
"title": "San Diego",
"items": [
{
"id": 31,
"title": "Duran Duran's Team",
"items": [
{
"id": 32,
"title": "Amberlynn Pinkerton"
},
{
"id": 33,
"title": "Tony Mejia"
},
{
"id": 34,
"title": "Richard Partridge"
},
{
"id": 35,
"title": "Elliot Stabler"
}
]
},
{
"id": 40,
"title": "Steely Dan's Team",
"items": [
{
"id": 36,
"title": "Tony Stark"
},
{
"id": 37,
"title": "Totally Rad"
},
{
"id": 38,
"title": "Matt Murdock"
},
{
"id": 39,
"title": "Stan Lee"
}
]
}
]
}
]
}
]
From the above json how do i filter only particular nested object which have id as 11 => {"id": 11} using underscore.js
Output which i required is : {
"id":11,
"title":"San Jose",
"items":[
{
"id":13,
"title":"Jensen Chapman's Team",
"items":[
{
"id":14,
"title":"Jimmy John"
},
{
"id":15,
"title":"Daniel Mills"
},
{
"id":16,
"title":"Chris Boden"
}
]
}
]
}
You can use a recursive algorithm to look for an object in the current array as well as the nested ones.
var data = [{"id":1,"title":"ASD Headquarters","items":[{"id":11,"title":"San Jose","items":[{"id":13,"title":"Jensen Chapman's Team","items":[{"id":14,"title":"Jimmy John"},{"id":15,"title":"Daniel Mills"},{"id":16,"title":"Chris Boden"}]}]},{"id":12,"title":"Irvine","items":[{"id":23,"title":"Tracey Chapman's Team","items":[{"id":24,"title":"San Jesus"},{"id":25,"title":"Fat Albert"},{"id":26,"title":"Connor McDavid"}]}]},{"id":30,"title":"San Diego","items":[{"id":31,"title":"Duran Duran's Team","items":[{"id":32,"title":"Amberlynn Pinkerton"},{"id":33,"title":"Tony Mejia"},{"id":34,"title":"Richard Partridge"},{"id":35,"title":"Elliot Stabler"}]},{"id":40,"title":"Steely Dan's Team","items":[{"id":36,"title":"Tony Stark"},{"id":37,"title":"Totally Rad"},{"id":38,"title":"Matt Murdock"},{"id":39,"title":"Stan Lee"}]}]}]}];
console.log(find(12, data));
function find(id, [head, ...tail]) {
if (!head)
return null;
return checkObj(id, head) || find(id, tail);
}
function checkObj(id, obj) {
return obj.id === id ? obj : find(id, obj.items || [])
}
This also uses parameter destructuring in order to conveniently separate the "head" of the array from its "tail".
It could also be done within a single function.
var data = [{"id":1,"title":"ASD Headquarters","items":[{"id":11,"title":"San Jose","items":[{"id":13,"title":"Jensen Chapman's Team","items":[{"id":14,"title":"Jimmy John"},{"id":15,"title":"Daniel Mills"},{"id":16,"title":"Chris Boden"}]}]},{"id":12,"title":"Irvine","items":[{"id":23,"title":"Tracey Chapman's Team","items":[{"id":24,"title":"San Jesus"},{"id":25,"title":"Fat Albert"},{"id":26,"title":"Connor McDavid"}]}]},{"id":30,"title":"San Diego","items":[{"id":31,"title":"Duran Duran's Team","items":[{"id":32,"title":"Amberlynn Pinkerton"},{"id":33,"title":"Tony Mejia"},{"id":34,"title":"Richard Partridge"},{"id":35,"title":"Elliot Stabler"}]},{"id":40,"title":"Steely Dan's Team","items":[{"id":36,"title":"Tony Stark"},{"id":37,"title":"Totally Rad"},{"id":38,"title":"Matt Murdock"},{"id":39,"title":"Stan Lee"}]}]}]}];
console.log(find(12, data));
function find(id, [head, ...tail]) {
if (!head)
return null;
if (head.id === id)
return head;
return find(id, head.items || []) || find(id, tail);
}

Categories

Resources