I have being wandering around this but can’t make it work. I am getting a text value from an array on localStorage
but I am failing with the easy part, display this value on a div
.
This is my code:
javascript
function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}console.log(Sellings);
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
var lselling = document.getElementById("lselling");
var lSelling = sellings[x].vdate;
}console.log(lSelling);
}
html
<div class="item-title">Fecha de confección:</div>
<div class="item-inner">
<div class="item-subtitle" id="lselling"></div>
</div>
This is the debuging result:
But I can’t manage to display the text value on the div as shown in this image:
What I want is to get the last element of the array and display it on the div
. My code is working but I can’t manage to diplay the element on the div
. I need to get the last object withing sellings
and display the value of vdate
on #lselling
. Just that. console.log(lSelling);
is showing 19/03/2023 05:34 AM
, and this is what I want to be displayed there. I’m already within a contact
, now I need to get the last selling
of this contact and display the value of vdate
.
What’s wrong within my code?
UPDATE
For future askers I solved it turning the function to async
just like this:
async function loadLastSell() {
var contacts = JSON.parse(localStorage.getItem("f7Contacts")) || {};
for (var k = 0; k < contacts.length; k++) {
var Sellings = contacts[k].sellings;
}
var sellings = Sellings;
for (var x = 0; x < sellings.length; x++) {
await 2;
lselling = document.getElementById("lselling")
lSelling = sellings[x].vdate;
}
lselling.innerHTML = lSelling
}
Now the value of vdate
last selling
ise properly displayed on the div
.
3
Answers
Your loop contents are wrong
is setting the same var over and over
This is wrong:
you replace lSelling over and over and do not update the content of the lselling element. Also very poor choice of names lselling (element) and lSelling (value)
You might have more luck with this assuming my testdata looks like your data. If not show an example of the data instead of a picture of it
I have used the optional/conditional chaining operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
.?
and the nullish coalescing operator??
to give a "No sales" if vdate is missingIf you only want to show the last one:
You are not working with
lselling
anywhere. You are getting the element withgetElementById
and then just reasigning the variablelselling
. Just doBtw this code needs tons of optimizations.
As I get it you already have the text value stored in the "lSelling" var.
And the "lselling" var keeps the div you want to show this text in.
To put your text into the div, you may use "innerHTML" as an option:
In fact, you don’t need the loop there, can be written like:
UPD:
For the JS code to work, it has to be executed after the HTML content is loaded.
The easiest for this is placing the tag after the lselling div: