简体   繁体   English

Javascript - Array - 将值从一个数组分配给另一个数组中的字符串

[英]Javascript - Array - Assigning values from one array to strings in another array

Hopefully the title is somewhat clear...dont know how to be more specific. 希望标题有点清楚......不知道如何更具体。

Here is my problem: I have several different variables that should be passed into the localstorage from one to another Html page. 这是我的问题:我有几个不同的变量应该从一个Html页面传递到localstorage。 On one (of many) pages, I can (with clickevents) changes values, which should be saved afterwards and being loaded on the next page. 在一个(多个)页面中,我可以(使用clickevents)更改值,这些值应该在之后保存并在下一页上加载。 If there is no value other then the start value "0", the value 0 should be saved, so the var does not get undefined. 如果除了起始值“0”之外没有其他值,则应保存值0,因此var不会被定义。 Technically the localstorage thing is working with the following code within a function : document.addEventListener('DOMContentLoaded', function(): 从技术上讲,localstorage正在使用函数中的以下代码: document.addEventListener('DOMContentLoaded', function():

var XXX;
if (localStorage.XXX> 0){
    XXX= Number(localStorage.XXX);
} else {
    XXX = 0;
}
document.getElementById("XXX").innerText = XXX;

This whole code is most probably not the prettiest thing you have ever seen, but it is working. 整个代码很可能不是你见过的最漂亮的东西,但它正在发挥作用。 As I have several variables, which get more and more, I think it would be good to put all of this copy / pasted code for each variable into a function... I tried many things but nothing is working, I do not even get it with the help of ctrl+Shift+I... here is what I was trying: 由于我有几个变量越来越多,我认为将每个变量的所有复制/粘贴代码都放入函数中会很好...我尝试了很多东西,但没有任何工作,我甚至都没有它在ctrl + Shift + I的帮助下......这就是我的尝试:

var resource = ["XXX", "YYY", "ZZZ"];
var localStorageResource = [localStorage.XXX, localStorage.YYY, localStorage.ZZZ];
function getLocalStorage() {
    for (var i = 0; i < resource.length; i++){
        if (localStorageResource[i] > 0){
            resource[i] = Number(localStorageResource[i]);
        } else {
            resource[i] = 0;
        }
        document.getElementById("resource[i]").innerText = resource[i];

So as you can see, the code is not really different, I just placed the variables and their respective localstorage values into two different arrays. 正如您所看到的,代码并没有真正不同,我只是将变量及其各自的localstorage值放入两个不同的数组中。 I am not able to understand why the variable XXX get overwriten with the actual value, instead of the value getting assigned to the variable... which is working fine in the "non flexible" solution. 我无法理解为什么变量XXX被实际值覆盖,而不是被分配给变量的值...这在“非灵活”解决方案中工作正常。

Just to give you the background: It is all about harvesting resources on one page or another (different resources on different locations) and bringing all these information, on the other html pages, to do something with these resources.... Right now I am using vanilla Javascript with the exception of easelJS for some canvas stuff 只是为了给你背景:这是关于在一个页面或另一个页面上获取资源(不同位置上的不同资源)以及在其他html页面上将所有这些信息带到这些资源上的所有信息....现在我我使用vanilla Javascript除了easelJS以外的一些画布

I would very much appreciate your advice and hope to learn a ton from your answers. 我非常感谢你的建议,并希望从你的答案中学到很多东西。

Thanks in advance! 提前致谢!

Better try with an object and iterate over the keys (act as the variable names for your example) 最好尝试使用对象并迭代键(作为示例的变量名称)

var resource = {"XXX": 0, "YYY": 0, "ZZZ": 0};

function getLocalStorage() {
    for (var i in resource){ //Variable "i" will be "XXX", "YYY" and "ZZZ" strings in each iteration
        resource[i] = localStorage.getItem(i) || 0; //In the first iteration will assign localStorage.getItem("XXX") to resource.XXX, and so on...
        document.getElementById(i).innerText = resource[i]; //In the first iteration will get element by id="XXX" and assign the value of resource.XXX, and so on...
    }
}

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

相关问题 JavaScript:将值从一个数组分配给另一个数组中的变量 - JavaScript: Assigning values from one array to variables in another 将一个数组分配给另一个 javascript - Assigning one array to another javascript 如何将数组值从一个数组映射到另一个数组JavaScript? - How to Map Array values from one Array to Another Array JavaScript? Javascript:将一个数组分配给另一个数组,这是对象的属性 - Javascript : Assigning one array to another array which is a property of an object Javascript将数组分配给另一个数组的元素 - Javascript assigning array to element of another array 将一个数组对象分配给另一个数组的Javascript - Javascript assigning an array object to another array 从一个数组中删除字符串(如果存在于另一个数 - Remove Strings from one Array if present in another 从另一个对象数组过滤字符串数组中的值 - filter values from an array of strings from another array of objects 在javascript中将数组中填充的值从一个函数返回到另一个函数 - returning values populated in an array from one function to another in javascript 匹配 JavaScript 中另一个数组的对应值后,如何根据一个数组中的值提取数据? - How to Extract data based on the values in one array after matching the corresponding values from another array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM