Select a specific item from an array in a Handlebars template - javascript

If I have the following 2 arrays bound to a compiled Handlebars template:
questions: [
{id:1, text:'question1', answers: [
{id: 1, text:'answer1'},
{id: 2, text:'answer2'}
]},
{id:2, text:'question2', answers: [
{id: 1, text:'answer3'},
{id: 2, text:'answer4'}
]}
],
userAnswers: [
{questionId:1, answerId:2}
]
I would rather not change the data structure and need to do the following (pseudo-code):
for each (question in questions)
render question text
render user answer text
(note that there may be a question without an answer)
If I use 'each' in my Handlebars template:
{{#each question}}
Is there a way to get the answer id from the userAnswers object?
Then map this back to questions to get the text?
e.g. is there some kind of selector syntax such as:
{{../userAnswers[questionId=id].answerId}}

Related

How do I push an element into the array within an object in AngularJS [duplicate]

This question already has answers here:
How do I add items to a nested Array?
(4 answers)
Closed last year.
I have the following JavaScript object :
[{
Name: "Ishan",
Code: 1,
Is_Intern: "Yes",
DateOfJoining: "01/02/2022",
Skills: ["VB, DOT.NET, Angular"],
Status: "Working"
}]
How can I add an item to the skills array in this object?
I didn't find any particular article regarding this.
var data = [{
Name: "Ishan",
Code: 1,
Is_Intern: "Yes",
DateOfJoining: "01/02/2022",
Skills: ["VB, DOT.NET, Angular"],
Status: "Working"
}]
data[0].Skills.push("New Skill");
console.log(data);

Use object in included pug file, but returns list of objects instead

I would like to loop through an object. This object is defined in a template called testpage.pug.
block vars
-
var testObject = [
{id: '1', name: 'John'},
{id: '2', name: 'Bert'},
{id: '3', name: 'Fred'},
]
include ../_modules/testloop.pug
My goal is to loop through the object in the included testloop.pug file. And being able to use the values in that template as for example:
ul
each test in #{testObject}
li= test.name
But in this example, the #testObject isn't loopable. But I can return it. For example if I just run:
code.formattedDebug= #{testObject}
// output on page: <[object Object],[object Object],[object Object]>
So perhaps do you need to do something to this Array of Objects (or is it?) first to parse them?
I've consulted:
https://pugjs.org/language/interpolation.html
https://pugjs.org/language/iteration.html
https://www.w3schools.com/js/js_json_stringify.asp

Multi Level mappings in Parent/Child hierarchy in D3 Possible?

I've a flat file which is of format
"id":1,"depends_on":2
"id":1,"depends_on":4
"id":1,"depends_on":5
"id":2,"depends_on":3
"id":4,"depends_on":5
Normally if I don't have
"id":1,"depends_on":5
I can plot the D3 Layout as follows
When I include "id":1,"depends_on":5
Graph that's being plot will be
Ideally I should have a line between 1 & 5 too, along with other mappings.
1)How can I achieve this?
2)How should the data-structure should be?
Does it really need to have duplicate entries (objects) in various parts of main data-structure ( to obtain in the format D3 needed (parent,children[])
Using d3.layout.force produces
Check out this example, which uses d3.layout.force().
A force layout's data structure involves 2 arrays:
an array of nodes
[{id: 1}, {id: 2}, {id: 3}, {id: 4}]`
and array of links
[
{ source: {id: 1}, target: {id: 2} },
{ source: {id: 1}, target: {id: 3} },
{ source: {id: 2}, target: {id: 4} }
]
The objects used in the nodes array and links array should be the same objects. I.e, in the example above, nodes[0] == links.source[0] should be true.

angular filter a scope down to 1 key type to prepare for send

So I have a scope that has a large chunk of key value pairs, so that I can work with it and make it do a bunch of stuff before I send it.
The problem I am having is I want to change that scope into just a small array of the keys of id with inside of it before I send it
so I have the scope which has the obect that is like this :
$scope.myScope = {name: name 1, id: 1, order: 1}, {name: name 2, id: 2, order: 2}
And I want to turn it into
$scope.FilteredScope = {1,2};
of just the id's. I'm wondering if you can filter this scope inside of the controller before send? I've only ever used filters in stuff like repeats. so inside of the controller, it would filter out the ids and place them in maybe a new scope for sending just the ids. Is this possible?
Thanks!
You could just use array.map (may need to polyfill for older browsers):
angular.module('MyModule', [])
.controller('MyController', function( $scope ) {
$scope.myScope = [
{name: 'name1', id: 1, order: 1},
{name: 'name2', id: 2, order: 2}
];
$scope.ids = $scope.myScope.map( function(obj){
return obj.id;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='MyModule' ng-controller='MyController'>
ids: {{ids | json}}
</div>
Firstly for the $scope.myScope to be a correct Javascript Object ( in this case it is an array of map Objects ), it should be of the form:
$scope.myScope = [{name: "name 1", id: 1, order: 1}, {name: "name 2", id: 2, order: 2}]
Secondly, if assume you want your $scope.filteredScope to be an array of all the id values from the $scope.myScope object.
So it can easily be done like this
$scope.filteredScope = []
for(entry of $scope.myScope) $scope.filteredScope.push(entry.id)
This will lead to an output array like [1,2] which you can then send it.
Please let me know if my assumptions were right.If not please edit your question with more info and I will be happy to help

Using $pull in Mongodb to remove a deeply embedded object

I'm trying to delete ($pull) an object from an array that's embedded. (Using javascript/node.js driver.)
Here is the sample data, where one, two, three are the levels:
{
one : "All",
one : [
{
name: "People",
two: [
{
three_id: 123,
three: "Jonny",
},
{
three_id: 456,
three: "Bobby",
}
]
},
{
name: "Animals",
two: [
{
three_id: 828,
three: "Cat",
},
{
three_id: 282,
three: "Dog",
}
]
}
]
}
In this example, I'm trying get rid of "Bobby".
I can successfully match the document at the "three level" if I want, like this:
db.test.find({"one.two.three_id" : 456});
However, I've no idea how to eliminate that record using update. Here are some attempts, none of which work:
// failed attempts
db.test.update({"one.two.three_id" : 456}, {$pull:{'one.$.two.$.three_id': 456}});
db.test.update({"one.two.three_id" : 456}, {$pull:{'three_id': 456}});
// deletes entire level two "People"
db.test.update({"one.two.three_id" : 456}, {$pull: {one: {two : {$elemMatch: {'three_id': 456}}}}});
I read that you cannot use two positional $ operators and that you have to know the index position for the second one. However, I want to avoid having to use the index of the embedded dictionary I want to delete.
reference:
Mongodb on pull
http://docs.mongodb.org/manual/reference/operator/update/pull/
The value of the key in your $pull object needs to be the path of the array that you're targeting. This appears to work:
db.test.update(
{'one.two.three_id': 456},
{$pull: {'one.$.two': {three_id: 456}}}
);
It looks like the $ represents the index of the first matched array level in this case so it works even though we're matching across multiple nesting levels.

Categories