Deep filter / if in Handlebars Helper - javascript

I have an urgent demo showcase for Ghost and I have the following context. Though not specific to Ghost, its a general problem
{
posts: [{
name: 'post1',
id: 1,
tags: [{
id: 1,
name: 'tag1'
},{
id: 5,
name: 'tag5'
}]
},
{
name: 'post2',
id: 2,
tags: [{
id: 1,
name: 'tag1'
},{
id: 3,
name: 'tag3'
}]
}]
}
I want to create a filter helper in HandleBars.js which will display all posts whose tag name is 'tag1'
Something like
{{#filter posts tags[].name="tag5"}}
<h1> {{name}} </h1>
{{/filter}}
Outputs
<h1> post1 </h1>
EDIT
I have seen filter helpers, I think what I need is deepfilter helper.

Related

How to combine a flat arrays into hierarchy in Javascript

say I have a data structure like this:
const flat = [
[
{
id: 0,
schema: "class",
name: "basement below baseLevel"
}
{
id: 1,
schema: "class",
name: "baseLevel"
},
{
id: 2,
schema: "TopLevelClass",
name: "The Top"
},
],
[
{
id: 3,
schema: "class",
name: "Second base level"
},
{
id: 2,
schema: "TopLevelClass",
name: "The Top"
}
]
]
The structure assumes a nested hierarchy where the root is the last element of each inner array. I need to restructure the data to look like this:
const root = [
id: 2,
schema: "TopLevelClass",
name: "The Top",
children: [
{
id: 1,
schema: "class",
name: "base Level",
children: [
{
id: 0,
schema: "class",
name: "Basement below base level"
}
]
},
{
id: 3,
schema: "class",
name: "Second base level"
}
]
]
There are some examples that combine arrays into hierarchies similar to this, but they always include either a parent or child id and the data structure I am working with does not. It just assumes (I know this is probably not a good assumption to be making, but its what I am working with) that each inner array is structured base up. Is there a concise way to nest the flattened array into a hierarchy?
Thanks!

Inserting a unique id into a multidimensional object

I have an object with this structure (imagine it could be way bigger than this)
And I need to insert a new item inside, say, 'Name 3' children array. How would I go about creating a unique id for it? I have thought about saving the ids inside the object in a separate array and then creating one that is not on that, but I'd have to maintain it and I'm not sure that would be the best approach?
let obj = {
id: 1,
name: 'Name 1',
parent_id: null,
children: [{
id: 2,
name: 'Name 2',
parent_id: 1,
children: []
},
{
id: 3,
name: 'Name 3',
parent_id: 1,
children: [{
id: 4,
name: 'Name 4',
parent_id: 3,
children: []
}]
}
]
};
EDIT: The need for a unique id is for me to be able to delete an item. This is the code I've been given to work with, I can't alter the structure.

How to remove duplicated item in array of array

I have an array like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
},
{
name: 'Cancellation',
component: '/cancellation'
}
]
}]
and another like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
}
]
},
{
id: 3,
name: 'Example'
}]
My question is how is the best way to filter arrays, and remove one if have duplicated or more, even in array of children. Or the best, is like arrays merge!

How to set value in Angular JS Listbox

How to set the item in Angular JS Listbox. This should be shown as selected in Listbox.
HTML
<select multiple ="multiple" style="width:180px" ng-model="md_fruitpref" ng-options="fruit.Id as fruit.Name for fruit in Fruits"></select>
Controller
//Controller to set the items in Fruit
$scope.Fruits = [{
Id: 1,
Name: 'Apple'
}, {
Id: 2,
Name: 'Mango'
}, {
Id: 3,
Name: 'Orange'
}, {
Id: 4,
Name: 'Guava'
}, {
Id: 5,
Name: 'Banana'
}, {
Id: 6,
Name: 'Watermellon'
}];
This will fill the Listbox with the various fruits.
Now I want to show the webpage with all the fruits in the Listbox but selected with Apple and Watermellon. So I do this but it does not help.
$scope.md_fruitpref= [{
Id: 1,
Name: 'Apple',
Id: 6,
Name: 'Watermellon'
}];
How to set value in Angular JS Listbox?
Try:
// array with 2 elements (each one an object with the same properties)
$scope.md_fruitpref= [{
Id: 1,
Name: 'Apple'
}, {
Id: 6,
Name: 'Watermellon'
}];
The structure you currently have does not work because you only have one element in your md_fruitpref array, and the object is invalid because it uses the same keys twice.
Finally I found the answer to this.
It will be :-
$scope.md_fruitpref= [1,6];
After this line soon you will see the selection in Listbox

Setting deep associations in normalizr schema

I have a server response that looks like this:
[{
id: 1,
title: 'Some Article',
related_articles: {
total: 4,
results: [
{ id: 2, title: 'Some other article' },
{ id: 3, title: 'Yet another article' },
]
}
}]
As you can see, what makes this tricky is it isn't a simple arrayOf: I want to normalize article.related_articles.results.
I've tried this, to no avail:
articleSchema.define({
related_articles: {
results: arrayOf(relatedArticleSchema),
},
});
It seems as though supported relations have to be "top level".
Anyone know how I can wind up with something like:
[{
id: 1,
title: 'Some Article',
related_articles: {
total: 4,
results: [2, 3]
}
}]
Thanks!
Your scheme defines a key "relatedArticles", but it should be snake_case, "related_articles"

Categories