Unrecognized expression - javascript

I have the following json output from a webservice:
[
{
"subCategories": [
{
"subCategories": [],
"menuItems": [],
"id": 2,
"title": "First Course",
"type": "Menu Section",
"categoryID": 9,
"isActive": true,
"orderIndex": 7
}, {
"subCategories": [],
"menuItems": [
{
"id": 0,
"price": 30,
"title": "Meat",
"ingredients": "Bread, Pate, Cilantro, Turkey.",
"cookingTimeInMinutes": 6,
"isActive": true,
"picture": "",
"categoryID": 3,
"orderIndex": 2
}],
"id": 3,
"title": "Banh Mi",
"type": "Food Item",
"categoryID": 9,
"isActive": true,
"orderIndex": 1
}],
"menuItems": [
{
"id": 1,
"price": 1,
"title": "Soup",
"ingredients": "Water, Good Stuffs, Noodles.",
"cookingTimeInMinutes": 10,
"isActive": true,
"picture": "",
"categoryID": 9,
"orderIndex": 4
}, {
"id": 3,
"price": 12,
"title": "Egg Sandwich",
"ingredients": "Egg, Sandwich",
"cookingTimeInMinutes": 6,
"isActive": true,
"picture": "",
"categoryID": 9,
"orderIndex": 3
}],
"id": 9,
"title": "Lunch",
"type": "Menu Section",
"categoryID": null,
"isActive": true,
"orderIndex": 0
}, {
"subCategories": [],
"menuItems": [],
"id": 7,
"title": "Snack",
"type": "Menu Section",
"categoryID": null,
"isActive": true,
"orderIndex": 8
}, {
"subCategories": [],
"menuItems": [],
"id": 6,
"title": "First Course",
"type": "Menu Section",
"categoryID": null,
"isActive": true,
"orderIndex": 5
}, {
"subCategories": [],
"menuItems": [
{
"id": 2,
"price": 3,
"title": "Salad",
"ingredients": "Veggies",
"cookingTimeInMinutes": 5,
"isActive": true,
"picture": "",
"categoryID": null,
"orderIndex": 9
}],
"id": -1,
"title": "Other",
"type": "Menu Section",
"categoryID": null,
"isActive": true,
"orderIndex": 1000
}]
And I have the following javascript snippet that is supposed to iterate over mentioned json and turn it into divs:
<script type="text/javascript">
/* wait until the document has finished loading before loading
* the rest of the content
*/
$(document).ready(function(){
function divifyCategory(containerID, gingerWebCategory){
$('#' + containerID).append(
$('<div class="category" id="' + gingerWebCategory.id + '">' + gingerWebCategory.title + '</div>')
);
for(menuItem in gingerWebCategory.menuItems){
$('.category#' + gingerWebCategory.id).append(
$('<div class="menuItem" id="' + menuItem.id + '">' + menuItem.title + '</div>')
);
}
}
// load menu from web service
$.get('http://localhost:50730/GingerWeb.asmx/getMenu', function(data){
var data = eval(data);
for(var i=0; i<data.length; i++){
divifyCategory(data[i]);
}
});
});
</script>
Any idea on why I get this error message in Chrome:
Uncaught Error: Syntax error, unrecognized expression: #[object
Object]
?

Your json object is an array of objects. When you pass data[i], you are passing the first element in the array, an object, and treating it like a string (containerID). You need to get the ID from the object you're passing.

I suppose datais json string mentioned at the very beginning of the post. data is an array of objects. So there
for(var i=0; i<data.length; i++){
divifyCategory(data[i]);
}
object has been passed to function divifyCategory which expects string as the first parameter. And that parameter is used like string there
$('#' + containerID)
in the runtime you get $('#[object Object]'). It is unxpected situation.
I hope it helps.

Related

Loop through a JSON object and display items as unordered list

i have a fetch that return a json object like this:
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "name",
"instant_invite": "link",
"channels": [
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Altri Giochi",
"position": 8
},
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Ascolto Musica",
"position": 11
},
],
"members": [
{
"id": "0",
"username": "xxxxxxxxxxxxxx",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},
],
"presence_count": 3
}
now i'm trying to map and display all these items to make an unordered list for my website, but i can't map this object with this.myResponse.map...i get an error "map is not a function".
i'm definitely lost here and i need some help to make an unordered list that contain all members name, avatar and other stuff.
can someone help me on this in vanilla js?
You can loop through the object using Object.keys(). Here's an example
let data = {
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "name",
"instant_invite": "link",
"channels": [{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Altri Giochi",
"position": 8
},
{
"id": "xxxxxxxxxxxxxxxxxxx2",
"name": "Ascolto Musica",
"position": 11
},
],
"members": [{
"id": "0",
"username": "xxxxxxxxxxxxxx",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},
{
"id": "1",
"username": "yyyyyyyyyyyyy",
"discriminator": "0000",
"avatar": null,
"status": "idle",
"avatar_url": "https://url"
},],
"presence_count": 3
}
data.members.forEach(m => {
// start the UL
let ul = "<ul class='member'>";
// loop through using Object.keys(m) or preset array of keys
['username','avatar'].forEach(md => {
ul += `<li>${md}: ${m[md]}</li>`;
})
ul += "</ul>";
document.querySelector('#members').innerHTML += ul;
})
<div id='members'></div>

Array object grouping in JavaScript

I need to convert below unformatted JSON format into formatted input. We need to find id's similar to parent id for different items inside array element of object and then need to push it into children to that id. Below is my code that needs to transform
Input
{
"0": [
{
"id": 10,
"title": "House",
"level": 0,
"children": [],
"parent_id": null
}
],
"1": [
{
"id": 12,
"title": "Red Roof",
"level": 1,
"children": [],
"parent_id": 10
},
{
"id": 18,
"title": "Blue Roof",
"level": 1,
"children": [],
"parent_id": 10
}
],
"2": [
{
"id": 17,
"title": "Blue Windoww",
"level": 2,
"children": [],
"parent_id": 12
},
{
"id": 16,
"title": "Door",
"level": 2,
"children": [],
"parent_id": 13
}
]
}
Output
[
{
"id": 10,
"title": "House",
"level": 0,
"children": [
{
"id": 12,
"title": "RedRoofff",
"level": 1,
"children": [
{
"id": 17,
"title": "Blue Windoww",
"level": 2,
"children": [],
"parent_id": 12
}
],
"parent_id": 10
},
{
"id": 18,
"title": "Blue Roof",
"level": 1,
"children": [],
"parent_id": 10
},
{
"id": 13,
"title": "Wall",
"level": 1,
"children": [
{
"id": 16,
"title": "Door",
"level": 2,
"children": [],
"parent_id": 13
}
],
"parent_id": 10
}
],
"parent_id": null
}
]
Please find the solution to above problem.
first, we track the node with Id and then we update the children array like this.
(btw, your input have a missing node, 13)
const input = {
"0": [{
"id": 10,
"title": "House",
"level": 0,
"children": [],
"parent_id": null
}, {
"id": 13,
"title": "Wall",
"level": 0,
"children": [],
"parent_id": null
}],
"1": [{
"id": 12,
"title": "Red Roof",
"level": 1,
"children": [],
"parent_id": 10
},
{
"id": 18,
"title": "Blue Roof",
"level": 1,
"children": [],
"parent_id": 10
},
],
"2": [{
"id": 17,
"title": "Blue Windoww",
"level": 2,
"children": [],
"parent_id": 12
},
{
"id": 16,
"title": "Door",
"level": 2,
"children": [],
"parent_id": 13
},
]
};
const results = [];
const mapId2Node = Object.values(input).reduce((acc, vals) => {
vals.forEach(val => {
acc[val.id] = val;
if (val.parent_id === null) {
results.push(val);
}
});
return acc;
}, {});
Object.values(input).forEach(vals => {
vals.forEach(val => {
if (val.parent_id !== null) {
mapId2Node[val.parent_id].children.push(val);
}
});
});
conosle.log(results);

How to remove all empty array childrens [] from a nested JSON recursively [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I've a JSON response as below.I'm using nested JSON data from my GeoRegionCountries APIController & custom class TreeView is used to format the data as per the required nested structure of plugin I'm using. I am using a combo multi select Treeview using this jquery plugin Multi-Select Drop Down Tree Plugin you can see it by this link jquery plugin Multi-Select Drop Down Tree Plugin
[
{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [
{
"Id": 7,
"Title": "Northwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1,
"Subs": []
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1,
"Subs": []
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1,
"Subs": []
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null,
"Subs": []
},
{
"Id": 3,
"Title": "France",
"ParentId": null,
"Subs": []
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null,
"Subs": []
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null,
"Subs": []
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null,
"Subs": []
}
]
I want to remove all "Subs" with empty array.
[
{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [
{
"Id": 7,
"Title": "Northwest",
"ParentId": 1
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null
},
{
"Id": 3,
"Title": "France",
"ParentId": null
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null
}
]
What is the best way to deep clean this? I tried different solutions in Stackopverflow but all i got is Object object in place of empty Subs - which i don't want.
[
{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [
{
"Id": 7,
"Title": "Northwest",
"ParentId": 1,
Object object
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1,
Object object
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1,
Object object
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1,
Object object
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1,
Object object
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null,
Object object
},
{
"Id": 3,
"Title": "France",
"ParentId": null,
Object object
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null,
Object object
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null,
Object object
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null,
Object object
}
]
which is not i want
You can use _.transform() to recursively check for a specific key (Subs), and remove it if it's value is empty:
const { transform, isObject, isEmpty } = _;
const removeEmpty = (obj, key) =>
transform(obj, (r, v, k) => {
if(k === key && isEmpty(v)) return;
r[k] = isObject(v) ? removeEmpty(v, key) : v;
});
const tree = [{"Id":1,"Title":"United States","ParentId":null,"Subs":[{"Id":7,"Title":"Northwest","ParentId":1,"Subs":[]},{"Id":8,"Title":"Northeast","ParentId":1,"Subs":[]},{"Id":9,"Title":"Central","ParentId":1,"Subs":[]},{"Id":10,"Title":"Southwest","ParentId":1,"Subs":[]},{"Id":18,"Title":"Southeast","ParentId":1,"Subs":[]}]},{"Id":2,"Title":"Canada","ParentId":null,"Subs":[]},{"Id":3,"Title":"France","ParentId":null,"Subs":[]},{"Id":4,"Title":"Germany","ParentId":null,"Subs":[]},{"Id":5,"Title":"Australia","ParentId":null,"Subs":[]},{"Id":6,"Title":"United Kingdom","ParentId":null,"Subs":[]}]
const result = removeEmpty(tree, 'Subs');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
The correct answer would be this:
let array = [
{
'Id': 1,
'Title': 'United States',
'ParentId': null,
'Subs': [
{
'Id': 7,
'Title': 'Northwest',
'ParentId': 1,
'Subs': []
},
{
'Id': 8,
'Title': 'Northeast',
'ParentId': 1,
'Subs': []
},
{
'Id': 9,
'Title': 'Central',
'ParentId': 1,
'Subs': []
},
{
'Id': 10,
'Title': 'Southwest',
'ParentId': 1,
'Subs': []
},
{
'Id': 18,
'Title': 'Southeast',
'ParentId': 1,
'Subs': []
}
]
},
{
'Id': 2,
'Title': 'Canada',
'ParentId': null,
'Subs': []
},
{
'Id': 3,
'Title': 'France',
'ParentId': null,
'Subs': []
},
{
'Id': 4,
'Title': 'Germany',
'ParentId': null,
'Subs': []
},
{
'Id': 5,
'Title': 'Australia',
'ParentId': null,
'Subs': []
},
{
'Id': 6,
'Title': 'United Kingdom',
'ParentId': null,
'Subs': []
}
]
let newArray = array.map(item=> {
if (item.Subs.length===0){
delete item.Subs
return item
}
item.Subs = item.Subs.map(item=>{
if (item.Subs.length===0){
delete item.Subs
return item
}
})
return item
}
)
console.log(newArray)
let data = [
{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [
{
"Id": 7,
"Title": "Northwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1,
"Subs": []
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1,
"Subs": []
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1,
"Subs": []
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null,
"Subs": []
},
{
"Id": 3,
"Title": "France",
"ParentId": null,
"Subs": []
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null,
"Subs": []
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null,
"Subs": []
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null,
"Subs": []
}
];
data = data.map(row=>{
if (!row.Subs.length) {
let {Subs,...r} = row;
return r;
} return row
})
console.log(data);
write two functions and pass the function that iterates through your array to a map function on data as shown below
function formatData(val) {
if (val.Subs.length > 0) val.Subs.map(a => a.Subs.length > 0 ? formatData(a.Subs) : deleteSubs(a));
else deleteSubs(val);
return val;
}
function deleteSubs(val) {
delete val.Subs;
}
var data = [{
"Id": 1,
"Title": "United States",
"ParentId": null,
"Subs": [{
"Id": 7,
"Title": "Northwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 8,
"Title": "Northeast",
"ParentId": 1,
"Subs": []
},
{
"Id": 9,
"Title": "Central",
"ParentId": 1,
"Subs": []
},
{
"Id": 10,
"Title": "Southwest",
"ParentId": 1,
"Subs": []
},
{
"Id": 18,
"Title": "Southeast",
"ParentId": 1,
"Subs": []
}
]
},
{
"Id": 2,
"Title": "Canada",
"ParentId": null,
"Subs": []
},
{
"Id": 3,
"Title": "France",
"ParentId": null,
"Subs": []
},
{
"Id": 4,
"Title": "Germany",
"ParentId": null,
"Subs": []
},
{
"Id": 5,
"Title": "Australia",
"ParentId": null,
"Subs": []
},
{
"Id": 6,
"Title": "United Kingdom",
"ParentId": null,
"Subs": []
}
]
console.log(data.map(formatData))

How to filter an array of objects where their values could contain null?

This is an example of an api response
[
{
"id": 1,
"name": "Medicine1",
"status": true,
"location": "E1-2",
"genericName": "Medicine1 Generic name",
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 1,
"name": "Tabletas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 25,
"percentage": null
},
{
"id": 2,
"name": "Medicine2",
"status": true,
"location": "E1-5",
"genericName": "Medicine2 Generic",
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 2,
"name": "Cremas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 500,
"percentage": null
},
{
"id": 3,
"name": "Medicine3",
"status": true,
"location": "E1-2",
"genericName": null,
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 3,
"name": "Unguentos"
},
"measure": {
"id": 3,
"unit": "Libras",
"abbreviation": "lb"
},
"quantity": 5,
"percentage": null
},
{
"id": 4,
"name": "Medicine4",
"status": true,
"location": "E5-1",
"genericName": null,
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 1,
"name": "Tabletas"
},
"measure": {
"id": 2,
"unit": "Kilogramos",
"abbreviation": "kg"
},
"quantity": 5,
"percentage": null
},
{
"id": 5,
"name": "Medicine5",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 1,
"name": "Tabletas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 5,
"percentage": null
},
{
"id": 6,
"name": "Medicine5",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 1,
"name": "Tabletas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 5,
"percentage": null
},
{
"id": 7,
"name": "Medicine6",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": {
"id": null,
"name": null
},
"presentation": {
"id": 1,
"name": "Tabletas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 5,
"percentage": null
},
{
"id": 8,
"name": "Medicine7",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": {
"id": 3,
"name": "Falcon"
},
"presentation": {
"id": 4,
"name": "Gotas"
},
"measure": {
"id": 1,
"unit": "Gramos",
"abbreviation": "g"
},
"quantity": 5,
"percentage": null
}
]
As you can see, the laboratory, percentage and genericName keys can have null value.
I need to filter this response according to a criteria that should be compared with each value
Here is the filter code
const criteria = 'some text';
fetchResource('medicines').then(medicines => {
const results = medicines.filter(medicine => {
return (
medicine.name.toLowerCase().includes(criteria) ||
medicine.genericName.toLowerCase().includes(criteria) ||
medicine.presentation.name
.toLowerCase()
.includes(criteria) ||
medicine.measure.unit.toLowerCase().includes(criteria) ||
medicine.measure.abbreviation
.toLowerCase()
.includes(criteria) ||
medicine.location.toLowerCase().includes(criteria)
);
});
const helper = makeHelper();
helper.render(results);
});
In backend, I thought about excluding the mentioned keys from the response when they do not have values. I have not tried yet but I understand that would work
I appreciate your advice on how to deal with this case on the client's side
There are other answers here that suggest using JSON.stringify to convert the entire object to a string, but that's not a great solution. It would make it impossible to search for generic, because every object has a property named genericName. There is another way to use stringify that's a bit more graceful, and that's to take advantage of the replacer callback.
For example:
const results = medicines.filter(m => {
var isMatch = false;
JSON.stringify(m, (key, value) => {
if (typeof value === "string" && value.toLowerCase().includes(criteria)) {
isMatch = true;
}
return value;
});
return isMatch;
});
results will contain only those entries from medicines that contain some value that is a string that matches the given filter. You can extend this logic to include numeric values, such as id, or exclude certain keys you're not interested in, such as abbreviation.
Here's a quick demo implementing some more advanced logic. You'll of course want to tweak it to suit your exact needs:
const medicines = [{
"id": 1,
"name": "Medicine1",
"status": true,
"location": "E1-2",
"genericName": "Medicine1 Generic name",
"laboratory": { "id": null, "name": null },
"presentation": { "id": 1, "name": "Tabletas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 25,
"percentage": null
},
{
"id": 2,
"name": "Medicine2",
"status": true,
"location": "E1-5",
"genericName": "Medicine2 Generic",
"laboratory": { "id": null, "name": null },
"presentation": { "id": 2, "name": "Cremas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 500,
"percentage": null
},
{
"id": 3,
"name": "Medicine3",
"status": true,
"location": "E1-2",
"genericName": null,
"laboratory": { "id": null, "name": null },
"presentation": { "id": 3, "name": "Unguentos" },
"measure": { "id": 3, "unit": "Libras", "abbreviation": "lb" },
"quantity": 5,
"percentage": null
},
{
"id": 4,
"name": "Medicine4",
"status": true,
"location": "E5-1",
"genericName": null,
"laboratory": { "id": null, "name": null },
"presentation": { "id": 1, "name": "Tabletas" },
"measure": { "id": 2, "unit": "Kilogramos", "abbreviation": "kg" },
"quantity": 5,
"percentage": null
},
{
"id": 5,
"name": "Medicine5",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": { "id": null, "name": null },
"presentation": { "id": 1, "name": "Tabletas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 5,
"percentage": null
},
{
"id": 6,
"name": "Medicine5",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": { "id": null, "name": null },
"presentation": { "id": 1, "name": "Tabletas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 5,
"percentage": null
},
{
"id": 7,
"name": "Medicine6",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": { "id": null, "name": null },
"presentation": { "id": 1, "name": "Tabletas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 5,
"percentage": null
},
{
"id": 8,
"name": "Medicine7",
"status": true,
"location": "E1-1",
"genericName": null,
"laboratory": { "id": 3, "name": "Falcon" },
"presentation": { "id": 4, "name": "Gotas" },
"measure": { "id": 1, "unit": "Gramos", "abbreviation": "g" },
"quantity": 5,
"percentage": null
}
];
const btn = document.getElementById("go");
const inp = document.getElementById("search");
btn.addEventListener('click', () => {
const criteria = inp.value.toLowerCase();
const results = medicines.filter(m => {
var isMatch = false;
JSON.stringify(m, (key, value) => {
// Search 'id' values
if (key === "id" && value !== null && value.toString().includes(criteria)) {
isMatch = true;
// Ignore 'abbreviation'
} else if (key !== "abbreviation") {
// Search all other string values
if (typeof value === "string" && value.toLowerCase().includes(criteria)) {
isMatch = true;
}
}
return value;
});
return isMatch;
});
console.log(results);
});
<input id="search" type="search" placeholder="filter" /><button id="go">Go</button><br>
<code>
Currently your code will error on medicine.genericName.toLowerCase() if the genericName field isn't a string. To avoid that you could try one of the following instead:
Fall back to a default:
(medicine.genericName || '').toLowerCase().includes(criteria)
Check the value first:
(medicine.genericName && medicine.genericName.toLowerCase().includes(criteria))
Its a bit tough to figure out what you're trying to do with the example, but I'm assuming you want to check if the value of multiple keys in your API response contains a substring criteria?
If that's the case, you could try something like:
fetchResource('medicines').then(medicines => {
const results = medicines.filter(medicine => {
for (var key in medicine){
if((typeof(medicine[key] == 'string' || typeof(medicine[key] == 'int') && medicine[key].toString().toLowerCase().includes(criteria)){
return true
}
else if(typeof(medicine[key]) === 'object'){
for(var subkey in medicine[key]){
if((typeof(medicine[key][subkey]) == 'string' || typeof(medicine[key][subkey]) === 'int') && medicine[key][subkey].toString().toLowerCase().includes(criteria)){
return true
}
}
}
}
return false
})
})
This is obviously much cleaner than hard coding all of the property names.
As I told before, use a forEach in your array; following a function to filter;
Use JSON.stringify so you que see all properties in row;
Apply a pattern as criteria
var yourCriteria = ""; // or any Regex
var yourArray = [];
var newArray = [];
yourArray.forEach(function(e){
if (JSON.stringify(e).toLowerCase().indexOf(yourCriteria) < 0)
newArray.push(e);
})

Knockout JS changing my viewmodel creation to use mapping pulgin

I am relatively new to Knockout.js and have a question regarding the creating of the view model.
What I am currently doing is:
viewModel = {
Menu: ko.observableArray(ko.utils.parseJson(data)),
editMenu: function (menu) {
ko.applyBindings(menu, document.getElementById("MenuCategories"));
$("#MenuCategories").bPopup();
}
There is more than one function, but trying to keep it short.
What I want to do is use the mapping plugin because my json data that I am using contains arrays of objects with each object containing an array and each object in that array also contains an array. What the JSON looks like:
[
{
"Id": 1,
"Name": "Test 5",
"Description": "Testing menu",
"BeveragesMenu": false,
"Active": true,
"Categories": [
{
"Id": 1,
"Name": "Test 1",
"Active": true,
"MenuItems": [
{
"Id": 1,
"Name": "Food",
"Description": "2 Eggs and Bread",
"Price": 50,
"DateBased": false,
"PriceOnRequest": false,
"DateFrom": null,
"DateTo": null,
"Active": true
}
]
},
{
"Id": 2,
"Name": "Test 2",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1004,
"Name": "test",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1005,
"Name": "Test 4",
"Active": true,
"MenuItems": [
]
}
]
},
{
"Id": 92,
"Name": "Test 4",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 1006,
"Name": "Test 1",
"Active": true,
"MenuItems": [
{
"Name": "Test",
"Description": "",
"Price": "",
"DateBased": false,
"PriceOnRequest": false
}
]
}
]
},
{
"Id": 93,
"Name": "Test 6",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
]
},
{
"Id": 94,
"Name": "Test 9",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
]
},
{
"Id": 95,
"Name": "Test 20]",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 4,
"Name": "Test 6666",
"Active": true,
"MenuItems": [
]
}
]
},
{
"Id": 96,
"Name": "Test 444",
"Description": "",
"BeveragesMenu": false,
"Active": false,
"Categories": [
{
"Id": 3,
"Name": "Test 555",
"Active": true,
"MenuItems": [
]
},
{
"Id": 5,
"Name": "Test 6666",
"Active": true,
"MenuItems": [
]
},
{
"Id": 1003,
"Name": "test 777",
"Active": true,
"MenuItems": [
]
}
]
}
]
Sorry for the long JSON code paste.
So now I am running into problems when updating certain parts of the objects within arrays withing objects etc.
So I taught that the mapping plugin might solve my problem as it creates all arrays as observable and I am assuming that is my problem.
So I tried:
viewModel = {
Menu: ko.mapping.fromJSON(ko.utils.parseJson(data)),
//Menu: ko.observableArray(ko.utils.parseJson(data)),
editMenu: function (menu) {
ko.applyBindings(menu, document.getElementById("MenuCategories"));
$("#MenuCategories").bPopup();
},
But then none of my binding are working and I can't seem to find the cause, any advice or tips on this matter would be appreciated.
Here is a Fiddle of all the things I have tried. http://jsfiddle.net/armandvdwalt/pjJc2/3/
Thanks
Try this fiddle, I switched the order of the resources. You were referencing the mapping plugin before knockout which was generating a script error about not recognizing "ko." Also, you don't need to use ko.utils.parseJson when using fromJSON, as fromJSON expects JSON as an argument.

Categories