My first one/instance outputs fine.. However, if there is multiple found from 'ZipSearch' variable (which is the JSON header i.e. in my JSON code below, parrent 23222 value), "23222":[ ...
Below is a recent attempt at using [2] have also tried [1] etc. Any ideas on how to output the content of the following JSON datas with common Key value? Into my HTML page?
$('span.zip').html(myjson[ZipSearch][0].Zipcode);
$('span.state').html(myjson[ZipSearch][0]["Primary State"]);
$('span.city').html(myjson[ZipSearch][0].City);
$('span.countyS').html(myjson[ZipSearch][0]["County S"]);
$('span.county').html(myjson[ZipSearch][0].County);
$('span.zip1').html(myjson[ZipSearch][2].Zipcode);
$('span.state1').html(myjson[ZipSearch][2]["Primary State"]);
$('span.city1').html(myjson[ZipSearch][2].City);
$('span.countyS1').html(myjson[ZipSearch][2]["County S"]);
$('span.county1').html(myjson[ZipSearch][2].County);
Here's my JSON.
"23222":[ # sometimes has multiple responses per, ie below
{"Zipcode":"23222","City":"","Primary State":"Virginia","County S":"555","County":"Sample City"},
{"Zipcode":"23222","City":"","Primary State":"Utah","County S":"444","County":"Sample Bigger City"}
],
Basically, I'm unable to output the second line above, Utah etc, in my HTML page...... Just always repeating the content from the first.
This is the mark-up: (I am just trying to handle the content if there is multiple common arrays)
<li class="zip">Zip Code: <span class="zip"></span></li>
<li class="state">Primary State: <span class="state"></span></li>
<li class="city">City: <span class="city"></span></li>
<li class="countySSA">County SSA: <span class="countyS"></span></li>
<li class="county">County: <span class="county"></span></li>
<li class="zip1">Zip Code: <span class="zip1"></span></li>
<li class="state1">Primary State: <span class="state1"></span></li>
<li class="city1">City: <span class="city1"></span></li>
<li class="countySSA1">County SSA: <span class="countyS1"></span></li>
<li class="county1">County: <span class="county1"></span></li>
Update:
So, I have just tried this, below per answer - and still it just prints one set of arrays values to HTML.
myjson[ZipSearch].forEach(function(innerobj,index){
$('span.zip'+index).html(innerobj["Zipcode"]);
$('span.county'+index).html(innerobj["County"]);
$('span.county1'+index).html(innerobj["County"]);
})
if your object is
var obj=[
{"Zipcode":"23222","City":"","Primary State":"Virginia","County S":"555","County":"Sample City"},
{"Zipcode":"23222","City":"","Primary State":"Utah","County S":"444","County":"Sample Bigger City"}
]
use for each to access multiple object
obj.forEach(function(innerobj,index){
$('span.zip'+index).html(innerobj["Zipcode"]);
})
Related
I coded like this to remove duplicate values.
vue
<div class="col-md-6" style="float: left">
<ul class="list-group">
<li class="list-group-item"
:class="{ active: index1 == currentIndex1 }"
v-for="(member, index1) in uniqueMemName"
v-bind:value="member.mem_name"
:key="index1"
#click="setActiveMember(member, index1)"
>
<strong style="margin-bottom: 5px"> {{member.mem_name}} </strong>
</li>
</ul>
</div>
vue (script)
computed: {
uniqueMemName() {
return _.uniqBy(this.members, function(m) {
return m.mem_name;
});
}
},
I also installed lodash. But I get an error. Which part is wrong?
Please let me know if there is another method other than the one I wrote.
++) error console
console window
++) array information:
I have tables A and B. Table A imports only the mem_name column. Table B imports all columns.
Example ->
a.mem_name
b.col1
b.col2
mem1
10
20
mem1
30
40
mem2
50
60
I'm working on making duplicate mem_names into one at this time. Using lodash's unique features.
If you want to use lodash just for this, which sounds like the case, I suggest that there may be a better way without it, only using newer vanilla JS:
...
computed: {
uniqueMemName() {
return [...new Set(this.members.map(m => m.mem_name))]
}
}
Sets are always unique, so mapping and converting to a set and then back to array gives us a unique array.
This is a snippet of my code on my site which displays json data onto the html page however, theres a lot of data on the json file. how would I limit the number of movies(data) being shown to 5.
function actors(actors) {
return `
<h5>Actors</h5>
<ul class="actors-list">
${actors.map(actor => `<li>${actor}</li>`).join("")}
</ul>
`;
}
function movieTemplate(movie) {
return `
<div class="animal" >
<img class="movie-photo" src="${movie.info.image}">
<h2 class="movie-name">${movie.title} <span class="species">(${movie.info.rating})</span></h2>
<p><strong>Year:</strong> ${movie.year}</p>
${movie.info.actors ? actors(movie.info.actors) : ""}
</div>
`;
}
document.getElementById("app").innerHTML = `
<h1 class="app-title">movies (${movieData.length} results)</h1>
${movieData.map(movieTemplate).join("")}
<p class="footer">These ${movieData.length} movies were added recently. Check back soon for updates.</p>
Image
This image shows the data being shown however thousands of movies are being shown I only want 5.
To limit the data you can use the slice function arr.slice([begin[, end]])
<ul class="actors-list">
${actors.slice(0,5).map(actor => `<li>${actor}</li>`).join("")}
</ul>
I have some Jquery tabs with Names on them.
(Ie: |Apple|Orange|Pear| )
I would like to update these titles to say something like
|Apple(2)|Orange|Pear(4)|
or something else based on the data returned by a php page ( say, numbers.php )
So,
Jquery requests numbers.php
numbers.php returns
Apple:2
Pear:4
How can I have JS, then use that data to dynamically update the Jquery tab "titles" with the data returned?
Then, if I re run the function, it would ask for numbers.php again and once again, update the tab titles based on what php returns?
Also, the names could change.. So next time it runs, Apples may not exist, but Pear may read 5 ..
In this instance, apple should be renamed "Apple"
Any help would be most excellent.
( hope ive made this clear? )
[ UPDATE ]
I have tried the below answer, but no luck.
The LI appears like this in the console:
<li id="tab-tabtestuser" class="ui-state-default ui-corner-top ui-tabs-active ui-state-active" role="tab" tabindex="0" aria-controls="newtab-tabtestuser" aria-labelledby="ui-id-5" aria-selected="true">tabtestuser <span class="count"></span><span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>
If you return the data from numbers.php in a meaningful format such as JSON, you can then process the data and update the tabs in Javascript much more easily. Here is a basic example of what you want to do, you will probably want to improve upon it.
numbers.php
{
apple: 5,
orange: 3,
pear: 2
}
Javascript:
$.getJSON( "numbers.php", function( data ) {
$.each( data, function( key, val ) {
$('#tab-' + key).find('span.count').text(val);
if (val == 0) {
$('#tab-' + key).hide();
};
});
});
HTML:
<div id="tabs">
<ul>
<li id="tab-apple">Apple <span class="count"></span></li>
<li id="tab-orange">Orange <span class="count"></span></li>
<li id="tab-pear">Pear <span class="count"></span></li>
</ul>
<div id="tabs-apple"></div>
<div id="tabs-orange"></div>
<div id="tabs-pear"></div>
</div>
I am attempting to sort a nested list using different variables. So, I have something like the following:
<div id="title">
<h2>AWARDS by TYPE</h2>
<span>
<p>Sort by: </p>
Trophy
Gold
Silver
Bronze
Other
</span>
</div>
And the lists:
<ul id="accolade-display-list">
<li class="accolade-display-list-item"> <img src="/images/us/law/accolades/organisation/27.jpg">
<ul>
<li class="accolades-org-name"> Argentina Wine Awards </li>
<li class="accolades-org-details">Silver Medal - 2012, Argentina</li>
</ul>
</li>
<li class="accolade-display-list-item"> <img src="/images/us/law/accolades/organisation/2.jpg">
<ul>
<li class="accolades-org-name"> Royal Adelaide Wine Show </li>
<li class="accolades-org-details">Regional Trophy - 2012, Argentina</li>
</ul>
</li>
<li class="accolade-display-list-item"> <img src="/images/us/law/accolades/organisation/57.jpg">
<ul>
<li class="accolades-org-name"> Wines of Chile Awards </li>
<li class="accolades-org-details"> Blue Gold Medal - 2012, Argentina</li>
</ul>
</li>
</ul>
What I would like to do is to be able to click, say "trophy" and sort the items with "trophy" at the top, then click "gold" and have those items together at the top, etc. There are currently one of each in my example, but there may be several "gold" items, "silver" items, and so on. I've tried a number of methods, but I have at the most been able to get the list to sort alphabetically, which is not what I need.
These are coming in from a JSP and I may be able to add additional classes to things as appropriate - I can also alter the list structure if necessary. The reason I have the nested lists is simply to make the alignment with the images easier, and because it is possible that there may be an additional line (<li>) in the future.
The way I've gotten the list to sort alphabetically, if it helps:
$("a.sort-by-trophy").click(function(){
console.log('sort-by-trophy clicked');
var list = $("ul#accolade-display-list");
var desc = false;
list.append(list.children().get().sort(function(a, b) {
var aProp = $(a).find(".accolades-org-name").text(),
bProp = $(b).find(".accolades-org-name").text();
return (aProp > bProp ? 1 : aProp < bProp ? -1 : 0) * (desc ? -1 : 1);
}));
});
Anyway, if anyone has any ideas as to something I might try, I would really appreciate it.
what i recommend is to have the values you should have, then generate the output (check underscore.js) and each time you click a sort, then generate it again and replace it.
what i mean, is that you havethe primivitve data:
var list = [{name: 'Argentina Wine Awards', medal: 'silver', ... }, {}, ...];
then, when is clicked a sort link, you sort the list array by the correct property and generate a new html (using underscore templates) and replace the old html with the new one.
I have two arrays:
var content = {
"girls": ["Maria", "Angela", "Bianca"],
"digits": ["21.143.191.2", "123.456.78.90", "971.6.17.18.1"]
};
and a template:
<script id="template" type="text/template">
<ul>
<li>{{girls}}</li>
</ul>
</script>
I'd like the end result to be:
<ul>
<li>Maria</li>
<li>Angela</li>
<li>Bianca</li>
</ul>
I've tried block mustaches like {{#girls}} {{.}} {{/girls}} and {{#digits}} {{.}} {{/digits}} but no matter which way I nest them I seem to get repeats instead of interlacing.
Any Ideas?
PS: Obviously in the future we'll be asking for IP addresses, not phone numbers.
PPS: None of those are intended to be real IPs, please don't try to contact those girls!
You need to rearrange your content so that you can iterate over just one thing. If you combine those two arrays with something like this:
var data = { girls: [ ] };
for(var i = 0; i < content.girls.length; ++i)
data.girls.push({
name: content.girls[i],
number: content.digits[i]
});
Then a template like this:
<script id="template" type="text/template">
<ul>
{{#girls}}
<li>{{name}}</li>
{{/girls}}
</ul>
</script>
should work.
With "mu is too short"'s advice. And a few crazy ideas I came up with an interesting approach to complex templating. *It almost works!
So let's say I have this content (or data or view) which I want to put into a template:
var content = {
title: "Black Book",
girls: ["blonde", "brunette", "redhead"],
digits: ['123', '456', '789'],
calc: function () {
return 2 + 4;
}
};
And I have a template like this:
<script type="text/template" id="template">
<h1>{{title}}</h1>
<h3>{{calc}}</h3>
<ul>
<li>{{hair}}</li>
</ul>
</script>
And the end result I want is this:
<h1>Black Book</h1>
<h3>6</h3>
<ul>
<li>blonde</li>
<li>brunette</li>
<li>redhead</li>
</ul>
The problem we'll encounter is that we have arrays (or lists) nested in our original content, and if we tried to Mustache.render(html, content) we'd end up with only one li item or a whole array within one href="" attribute. So sad...
So he's the approach, pass through the template multiple times. The first time, pass through and replace the top level items, and adjust the template for the next pass through. The second time, adjust one of the lists, and adjust the template for the third pass through, etc for how ever many layers of content you have. Here's the new starting template:
<script type="text/template" id="template">
<h1>{{title}}</h1>
<h3>{{calc}}</h3>
<ul>
{{#hair}}
{{#digits}}
<li>{{hair}}</li>
{{/digits}}
{{/hair}}
</ul>
</script>
On the first pass through fill in the top level stuff, and change {{digits}} to {{.}}
$.each(content, function (index, value) {
template2 = template.replace(/{{title}}/ig, content.title)
.replace(/{{calc}}/ig, content.calc)
.replace(/{{digits}}/ig, '{{.}}');
});
Now you have this:
<h1>Black Book</h1>
<h3>6</h3>
<ul>
{{#hair}}
{{#digits}}
<li>{{hair}}</li>
{{/digits}}
{{/hair}}
</ul>
On the next pass through we'll just call Mustache.render(template2, content.digits); and that should give us:
<h1>Black Book</h1>
<h3>6</h3>
<ul>
{{#hair}}
<li>{{hair}}</li>
<li>{{hair}}</li>
<li>{{hair}}</li>
{{/hair}}
</ul>
And that's where my logic dies, haha. Any help thinking this through would rock! I'm thinking I could take out the {{hair}} block elements and just do a $.each pass through content.girls and stack .replace three times. Or I could try to start with lowest level of content, and work my way up. I dunno.
All this basically leaves me wondering if there's some "logic-less" way for this kind of nesting or multiple pass throughs to be put into mustache does handlebars do it?