简体   繁体   English

当我想打印数组中的所有项目时,javascript getItem 打印最后一个数组

[英]javascript getItem prints last array when i want to print all items in array

function loadImg () {
    for(let y=1;y<=nuotraukos.length;y++)
    {
        const myImg = document.createElement("img");
        myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]);
        console.log(localStorage.getItem('nuotraukos',[y]));
        myImg.alt=[y]+'- img';
        myImg.width='250';
        myImg.height='200';
        const myDiv = document.querySelector(".imagePlace");
        myDiv.appendChild(myImg);
    }
}

so with code myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]);所以代码myImg.src = 'images/'+ localStorage.getItem('nuotraukos',nuotraukos[y]); i want to print all arrays items that are inside, but when i look at the console.log it only prints last one我想打印里面的所有数组项,但是当我查看 console.log 时,它只打印最后一个

The idea of this 'program' is tu mix images and save when i save i put them in array and after i refresh website i want to show mixed images after pressing button 'show saved' in this case it only shows the last image of mixed images, idk what to do been stuck in this for a while这个“程序”的想法是混合图像并保存,当我保存时我将它们放在数组中,在我刷新网站后我想在按下按钮“显示保存”后显示混合图像在这种情况下它只显示混合的最后一个图像图像,我知道该怎么做被困在这里一段时间

you need to parse the items Array :您需要解析 items Array :

function loadImg () {
const localnuotraukos= JSON.parse(localStorage.getItem('nuotraukos'))



for(let y=1;y<=nuotraukos.length;y++)
{
    const myImg = document.createElement("img");
    myImg.src = 'images/'+localnuotraukos[y] ;
    console.log(localnuotraukos[y]);
    myImg.alt=[y]+'- img';
    myImg.width='250';
    myImg.height='200';
    const myDiv = document.querySelector(".imagePlace");
    myDiv.appendChild(myImg);
}

As you said you are using this code to save in local storage正如您所说,您正在使用此代码保存在本地存储中

    for(var i=0;i<=7;i++)
    {
        let removeImg = document.querySelector('img');
        removeImg.remove();
        const myImg = document.createElement("img");
        myImg.src = 'images/'+nuotraukos[i];
        myImg.alt=[i]+'- img';
        myImg.width='250';
        myImg.height='200';
        const myDiv = document.querySelector(".imagePlace");
        myDiv.appendChild(myImg);
        localStorage.setItem('nuotraukos',nuotraukos[i]); // ********
    }

The line which I refer to with ******** is saying that you are override the nuotraukos item and it will be the last element in the array.我用********引用的那一行是说你覆盖了nuotraukos项目,它将是数组中的最后一个元素。

So, you need to save the array first after initiating your images array因此,您需要在启动图像数组后先保存数组

localStorage.setItem('nuotraukos', JSON.stringify(nuotraukos)); // save all array elements in one item

And when you need to load the array you have to load it and parse to JSON then iterate in the for loop.当您需要加载数组时,您必须加载它并解析为 JSON,然后在 for 循环中进行迭代。 And note that getItem() method takes only one parameter which is the key.并注意getItem()方法只接受一个作为关键的参数。

function loadImg () {
    let imgArray = JSON.parse(localStorage.getItem('nuotraukos')); // get array and parse it
    for(let y=0; y <imgArray.length; y++) // iterate from 0 to length
    {
        const myImg = document.createElement("img");
        myImg.src = 'images/'+  imgArray[y];
        myImg.alt= y +'- img';
        myImg.width='250';
        myImg.height='200';
        const myDiv = document.querySelector(".imagePlace");
        myDiv.appendChild(myImg);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想打印数组的最后一个 &#39;n&#39; 元素 - I want to print the last 'n' elements of an array JavaScript 仅打印数组的最后一个元素 - JavaScript prints only the last element of an array For循环仅在javascript中打印最后一个值数组 - For loop prints only last value array in javascript 打印数组JAVASCRIPT / d3中所有项目的值 - print values of all items in array JAVASCRIPT / d3 JavaScript中的一个返回数组的函数。 我可以在调用函数时打印数组中的项目,但不能打印数组中的每个项目 - A function in JavaScript that returns an array. I am able to print the items in the array when the function is called, but not each item in the array 不想用Javascript打印Array of Array - Don't want to print Array of Array in Javascript 我使用 for 为数组赋值,但是当我打印数组时,它只是在整个数组中的最后一个值 - im using for to assign a value to an array but when I print the array it just shoes the last value in the full array 用JavaScript替换数组中的最后N个项目 - Replace last N items in an array with JavaScript 我想在javascript中预加载图像数组并暂停直到所有图像都加载完毕 - I want to preload an array of images in javascript and pause until they are all loaded 替换 JavaScript 中数组的最后两项 - Replacing the last two items from an array in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM