Suppose I have this Json data in my controller:
var rootObj = [{
devices: ["device1", "device2"],
subGroups: [{devices: ["device3", "device4"]}, {devices: ["device5", "device6"]}]
}, {
devices: ["device7"],
subGroups: [{devices: ["device8"]}, {devices: ["device9"]}]
}];
And I want to show all devices in flat structure, like this:
<ul>
<li>device1</li>
<li>device2</li>
<li>device3</li>
<li>device4</li>
<li>device5</li>
<li>device6</li>
<li>device7</li>
<li>device8</li>
<li>device9</li>
</ul>
How can I use ng-repeat on rootObjto get above html?
I don't want to manipulate the original object, only using ng-repeat on rootObj.
The only option to solve it, is to use ng-repeat without creating any element on ng-repeat itself.
What is the best way to achieve it?
It achievable with black magic!
Returns
(Array): Returns the new flattened array.
Related
How to display long string data from JSON as a list wise if Description string is too long. How do i split or format description Using react and typescript .
I have below JSON data:
"options": [
{
"perkType": "GIFT_CARD",
"tierRewardId": "ff592a61-3e64-474e-a3e5-cb7c14cc73e1",
"perkDescription": "**Important details about the Perk:**\n* Perks are random additional items. These are not earned, but extra items given to customers outside of the spend levels.\n* See Terms and Conditions for exclusions and additional information.\n* [Terms & Conditions](www.xyz.net/TermsandConditions)",
},
{
"tierRewardId": "0aa6b029-3179-41dd-8726-78ca7e4bfe18",
"perkType": "TOOL_RENTAL",
"perkDescription": "**Important details about the Mik Perk:**\n* Mik Perks are random additional items. These are not earned, but extra items given to customers outside of the spend levels.\n* See Terms and Conditions for exclusions and additional information.\n* [Terms & Conditions](www.xyz.net/TermsandConditions)"
}
],
already i filter the data :
const optionGift = this.state.currentData.item.tierPerks[0].options.filter(
(list) => list.perkType === 'GIFT_CARD');
const optionGiftCard= optionGift.map((value)=> value );
const OptionRental = this.state.currentData.item.tierPerks[0].options.filter(
(list) => list.perkType === 'TOOL_RENTAL',);
const OptionRentalTool= OptionRental.map((value)=> value );
component tsx code :
<div> <ul className="YourPerkOption__modelParagraph">
<li>{props.optionGiftCard[0].perkDescription}</li></ul></div>
I am trying to display a list wise data into below format
if anybody can help please .
Custom Code
If I am understanding this correctly, the first line of the perkDescription is the title (**Important details about the Mik Perk:**) and the subsequent lines are bullet points. We need to break up the string into multiple string segments and then render them to the DOM through JSX. Let's make that into it's own reusable component.
The only prop that we need is the string text.
interface PerkDescriptionProps {
text: string;
}
Our component breaks that text into an array of lines with the string.Prototype.split() method. We store the first element of the array to a variable title and the rest of the lines to another variable bullets using spread syntax. We then loop through the bullets array and put each one inside of a li element.
export const PerkDescription = ({ text }: PerkDescriptionProps) => {
const lines = text.split("\n");
const [title, ...bullets] = lines;
return (
<div className="perkDescription">
<div className="perkDescriptionTitle">{title}</div>
<ul className="perkDescriptionList">
{bullets.map((line, i) => (
<li key={i}>{line}</li>
))}
</ul>
</div>
);
};
You would call it like this:
<PerkDescription text={optionGiftCard[0].perkDescription} />
But I think you should create a Perk component that uses this PerkDescription!
I am not dealing with the asterisks here so you'll still see them in the HTML output. Are you using some some of package to parse markdown syntax?
Markdown Parsing
Your JSON is using a standardized markdown syntax to denote the list. There are already packages out there which can handle the bulleted list as well as turning the link into an a element and adding the bold styling to the title.
Using react-markdown, all you need to do is put your text inside of a ReactMarkdown component.
import ReactMarkdown from 'react-markdown'
<ReactMarkdown>{optionGiftCard[0].perkDescription}</ReactMarkdown>
You could use a RegEx to split perkDescription into an array and then map that array.
Totally noob question from me. Different from other JSON nested questions, I want to access the value of the middle level.
Consider that I have a JSON like:
"nodes":[
{"Level1":[
{"Level2A":[
{"Level3A":"Value",
"Level3B":"Value"
},
{"Level3A":"Value",
"Level3B":"Value"
}]
},
{"Level2B":[
{"Level3A":"Value",
"Level3B":"Value"
},
{"Level3A":"Value",
"Level3B":"Value"
}]
}]
}]
I want to get the value of Level2 out(to use as label).
I can get lv3 value by calling for example,:
node.datum().Level1[0].Level2[0].Level3A
but if I tried
nodae.datum().Level1[].Level2
I get an object instead. The ideal output would be the array with [Level2A, Level2B,...]
Are you sure your json is in valid mode or you just post half of it?
That what I did-
{"nodes":[{"Level1":[{"Level2A":[{"Level3A":"Value","Level3B":"Value"},{"Level3A":"Value", "Level3B":"Value"}],"Level2B":[{"Level3A":"Value","Level3B":"Value"}]}]}]}
I am extracting data from the server and from that data I need to create the number of tabs and populate the data in the form of each tab.
For e.g.:
If my data has two fields named 'x' and 'y' then I need to create two tabs (where the form panel, meaning the fields inside the tab, are same) and then I need to populate these fields with the appropriate data.
Populating the data is fine but I need to know how to add tabs dynamically according to the JSON object I get. The name of the tab I'll be getting from the JSON object.
EDIT:
The JSON looks like this:
{
..
events : {
'X JOINED' : {
..
}
'Y JOINED' : {
..
}
}
}
Now I need two tabs named 'X JOINED' and 'Y JOINED' with the '..' giving the data to populate inside the tabs.
If you have a tabpanel you can use the method add to add a new tab.
To know the names of your object, just use the js keys method on your object obtained from JSON.
It will look something like that :
for(var prop in myObject)
myTabPanel.add(Ext.define("tab", {title: prop}));
do you have an sample of your JSON object?
Let's say I have a JSON array of data, something like:
[ {"name":"parijat","age":28},
{"name":"paul","age":28},
{"name":"steven","age"27},
...
]
that is part of my model, and this model is setup like this:
App.UserRoute = Ember.Route.extend({
model:function(){
return App.User.FIXTURES ; // defined above
}
});
I want to get the unique ages from this JSON array and display them in my template, so I reference the computed properties article and read up a little on how to enumerate Ember Enumerables, to eventually get this:
App.UserController = Ember.ArrayController.extend({
ages:function(){
var data = this.get('content');
var ages = data.filter(function(item){
return item.age;
});
}.property('content');
});
Now the above piece of code for controller is not correct but the problem is that it doesn't even go into data.filter() method if I add a console.log statements inside. IMO, it should typically be console logging as many times as there exist a App.Users.FIXTURE. I tried using property('content.#each') which did not work either. Nor did changing this.get('content') to this.get('content.#each') or this.get('content.#each').toArray() {spat an error}.
Not sure what to do here or what I am completely missing.
Filter is used for reducing the number of items, not for mapping.
You can use data.mapBy('age') to get an array of your ages:
ages:function(){
return this.get('content').mapBy('age');
}.property('content.#each')
or in your handlebars function you can just use the each helper:
{{#each item in controller}}
{{item.age}}
{{/each}}
I am new to Mustache, please bear with me :)
I have an array in my JSON
"prop":{"brands":["nike","adidas","puma"]}
if I have the template like this
{{#prop}}
<b>{{brands}}</b>
{{prop}}
and I want to get something like:
<b>nike</b>
<b>adidas</b>
<b>puma</b>
I understand the elements in the array are not hash key-value pairs, however I am wondering if there is anyway in mustache that I can iterate through the elements.
Thanks!
Here is a working fiddle: http://jsfiddle.net/Qa4UX/
Basically, you need to iterate over the brands array.
Since your array is raw and does not have objects inside you have to reference each string like so:
{{#props}}
<ul>
{{#brands}}
<li>
{{#.}}
<b>{{.}}</b>
{{/.}}
</li>
{{/brands}}
</ul>
{{/props}}
You can also find many more examples over here: https://github.com/janl/mustache.js#mustachejs---logic-less-mustache-templates-with-javascript
This works
{{#json.props.brands}}
<h1>{{.}}</h1>
{{/json.props.brands}}
{{.}} When looping over an array of strings, a . can be used to refer to the current item in the list.
mustache is logicless, so writing your own iteration/loop in it is impossible. It is easy to convert your JSON though. For example:
var json = '{"prop":{"brands":["nike","adidas","puma"]}}';
var obj = JSON.parse(json);
var data = {brands: obj.prop['brands'].map(function(x){ return {name: x}; })};
Gives you a data variable which will work with the template:
{{#brands}}
<b>{{name}}</b>
{{/brands}}