Home > Software design >  loop over more then one different arrays with different array name
loop over more then one different arrays with different array name

Time:06-29

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.

...
// 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

arr = window[arrNames[i]][j]['h'];

CodePudding user response:

You can use lodash zip function

_.zip(['a', 'b'], [1, 2], [true, false]);

In your case it should look like

_.zip(books, infos );

here is documentation page

CodePudding user response:

The issue is that localStorage variables can only store Strings. 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:

[
  "[{\"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:

  • Related