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
Related
I have a html select element with options(categories). Upon selecting an option(category) I get the option's value and store it in a variable.
const currentSelectValue = $('option:selected', this).attr('value');
I also have a JSON array with all subcategories.
const myObjArr = {
data: [
{ id: 1, name: 'name_1', parent_id: '1' },
{ id: 2, name: 'name_2', parent_id: '2' },
{ id: 1, name: 'name_2', parent_id: '3' }
]
};
My goal is to get the selected option value and match it to the list of subcategories, find the parent_id(which is the value from the selected option) and list all the results.
I'm new to JavaScript, so a detailed answer with an example would be much, much appreciated. Thanks in advance!
Check this out JS .filter()
Assume you have 3 in your parent_id. Here is one line clean code solution
const myObjArr = {
data: [{
id: 1,
name: 'name_1',
parent_id: '1'
},
{
id: 2,
name: 'name_2',
parent_id: '2'
},
{
id: 1,
name: 'name_2',
parent_id: '3'
}
]
};
// if parent id is 3
const currentSelectValue = 3
const selected = myObjArr.data.filter((ele,index) => ele.parent_id == currentSelectValue
)
console.log(selected)
This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed last year.
I need to find first child element in array and just return it.
Here is problem because here is few children elements and i need loop thought each and return every first children element.
Example of array:
let allItems =
[{ id: 1 ,
name: 'Test 1' ,
children: [
id: 12,
title: 'Child element'
]
},
{
id: 2 ,
name: 'Test 2'
},
{
id: 3 ,
name: 'Test 3',
children: [
id: 12,
title: 'Child element',
children: [
id: 123,
title: 'GRAND Child element',
]
]
}]
What's the problem here? Since there can be many children elements, do I need to find a parent for each of those elements?
After looping i need array to be:
[{ id: 1 ,
name: 'Test 1'
},
{
id: 2 ,
name: 'Test 2'
},
{
id: 3 ,
name: 'Test 3'
}]
Wihout children elements.
What I am try:
allItems.map(item => item).filter(filteredItem => !filteredItem.children);
But this is no return me good results
Based on your expected output, here is my solution.
Also note, that you had missing curly braces with your children.
See you modified snippet below:
let allItems = [{
id: 1,
name: 'Test 1',
children: [{
id: 12,
title: 'Child element'
}]
},
{
id: 2,
name: 'Test 2'
},
{
id: 3,
name: 'Test 3',
children: [{
id: 12,
title: 'Child element',
children: [{
id: 123,
title: 'GRAND Child element',
}]
}]
}
]
console.log(allItems.map(item => {
return {
id: item.id,
name: item.name
}
}))
Using map and destructuring is a nice way to achieve what you're looking for
let allItems = [{
id: 1,
name: 'Test 1',
children: [{
id: 12,
title: 'Child element'
}]
},
{
id: 2,
name: 'Test 2'
},
{
id: 3,
name: 'Test 3',
children: [{
id: 12,
title: 'Child element',
children: [{
id: 123,
title: 'GRAND Child element',
}]
}]
}
];
const res = allItems.map(x => {
const {id, name} = x;
return {id, name};
});
console.log(res);
Use these propertys for call first child:
.firstchild==>this property calls first Node
.firstElementChild==>calls first element
I have a following array
const _array = [{id: 1, name: 'Adam'}, {id:3, name: 'Crystal'}, {id:2, name: 'Bob'}, {id: 4, name: 'Daisy'}];
How to write a single line of code in typescript to get item where name equal to Crystal from the array?
You can use array find method like following:
const _array = [
{ id: 1, name: "Adam" },
{ id: 3, name: "Crystal" },
{ id: 2, name: "Bob" },
{ id: 4, name: "Daisy" },
];
const item = _array.find((item) => item.name === "Crystal");
console.log(item);
Output
{ id: 3, name: 'Crystal' }
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.
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.