简体   繁体   English

循环多个具有不同数组名称的不同数组

[英]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?但如何?

Solution Update:解决方案更新:

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 );

here is documentation page这是文档页面

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.

相关问题 如何使用一个forEach循环遍历两个不同的数组? - How can I use one forEach loop to iterate over two different arrays? 如何为不同的数组进行循环 - How do one for loop for different arrays 可以在一个多数组中命名不同的数组吗? - Possible to name different arrays within a multi array? 为什么 JavaScript “数组中的长度”对于具有一个或多个元素的 arrays 给出不同的结果? - Why JavaScript “length in array” gives different result for arrays having one element or more elements? 2个大小不同的数组,一旦达到最大索引,将在循环中重用一个数组 - 2 Arrays, different sizes, reuse one array within loop once max index is reached 转换一个数组中的首字母以匹配另一部作品中的全名,但当数组长度不同时则不然 - Converting an initial in one array to match a full name in another works, but not when arrays are different lengths 在一个对象数组中结合两个不同对象数组的不同属性 - Combining different properties of two different arrays of objects in one array of objects 循环不同的数组javascript - loop different arrays javascript 遍历包含不同长度数组的二维数组的行 - Iterating over rows of 2-dimensional array containing arrays of different length 使用另一个数组在 for 循环中引用两个不同数组的语法? - Syntax to reference two different arrays within a for loop using another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM