[英]loop over more then one different arrays with different array name
how can I do that?我怎样才能做到这一点? In PHP I have the $$name form for doing that but in js I don't know how to solve this.
在 PHP 中,我有 $$name 表单来执行此操作,但在 js 中,我不知道如何解决这个问题。
...
// rest of the code
var books = [
{'h':'book 1','i':[ {'s':'1','w':'some text'}, {'s':'2','w':'other text'} ] }
];
var infos = [
{'h':'HERE','i':[ {'h':'something','i':[ {'v':[{'g':1,'b':0}],'h':'text X','a':['bla','bla', 'bla']}]} ] }
];
var arrNames = ['books','infos'];
let z = document.getElementById('output');
for( i=0;i<arrNames.length;i++ ){
arr = localStorage.getItem(arrNames[i]).split(','); // doesnt work :(
html += arr[0]['h']+'<br>';
}
z.innerHTML = html;
output should to be:输出应该是:
book 1
HERE
but how?但如何?
thx @ Heretic Monkey and his link thx @ Heretic Monkey 和他的链接
arr = window[arrNames[i]][j]['h'];
You can use lodash zip function您可以使用 lodash zip 功能
_.zip(['a', 'b'], [1, 2], [true, false]);
In your case it should look like在你的情况下,它应该看起来像
_.zip(books, infos );
The issue is that localStorage
variables can only store Strings.问题是
localStorage
变量只能存储字符串。 So when you get that value, and split it, you have now broken it up into an Array of Strings.因此,当您获得该值并将其拆分时,您现在已将其分解为一个字符串数组。 Here is an example to clarify.
这里有一个例子来说明。
var local = {}; local.books = JSON.stringify([{ 'h': 'book 1', 'i': [{ 's': '1', 'w': 'some text' }, { 's': '2', 'w': 'other text' }] }]); local.infos = JSON.stringify([{ 'h': 'HERE', 'i': [{ 'h': 'something', 'i': [{ 'v': [{ 'g': 1, 'b': 0 }], 'h': 'text X', 'a': ['bla', 'bla', 'bla'] }] }] }]); var arrNames = ['books', 'infos']; var z = document.getElementById('output'); var html = "" arrNames.forEach(function(elem) { var arr = local[elem].split(","); console.log(arr); html += arr[0]['h'] + '<br>'; }); z.innerHTML = html;
<div id="output"></div>
You can see that .split()
is doing what is needed, and you see the following in console:您可以看到
.split()
正在执行所需的操作,并且您在控制台中看到以下内容:
[
"[{\"h\":\"book 1\"",
"\"i\":[{\"s\":\"1\"",
"\"w\":\"some text\"}",
"{\"s\":\"2\"",
"\"w\":\"other text\"}]}]"
]
I suspect you need to Parse the String back into Variables.我怀疑您需要将字符串解析回变量。 Consider the following.
考虑以下。
var local = {}; local.books = JSON.stringify([{ 'h': 'book 1', 'i': [{ 's': '1', 'w': 'some text' }, { 's': '2', 'w': 'other text' }] }]); local.infos = JSON.stringify([{ 'h': 'HERE', 'i': [{ 'h': 'something', 'i': [{ 'v': [{ 'g': 1, 'b': 0 }], 'h': 'text X', 'a': ['bla', 'bla', 'bla'] }] }] }]); var arrNames = ['books', 'infos']; var z = document.getElementById('output'); var html = "" arrNames.forEach(function(elem) { var temp = JSON.parse(local[elem]); console.log(temp); html += temp[0]['h'] + '<br>'; }); z.innerHTML = html;
<div id="output"></div>
For your code, you might consider taking it further and making helper functions.对于您的代码,您可能会考虑更进一步并制作辅助函数。
function setLocal(index, value) { localStorage.setItem(index, JSON.stringify(value)); return JSON.stringify(value); } function getLocal(index) { return JSON.parse(localStorage.getItem(index)); } var arrNames = ['books', 'infos']; var z = document.getElementById('output'); var html = "" arrNames.forEach(function(elem) { var obj = getLocal(elem); console.log(obj); html += obj[0]['h'] + '<br>'; }); z.innerHTML = html;
<div id="output"></div>
See More:看更多:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.