如何在不使用for循环的情况下检索值数组 [英] how to retrieve array of values without using for loop

查看:139
本文介绍了如何在不使用for循环的情况下检索值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


for (i = 0; i < query.length; i++) {
   sessionStorage.setItem("list_data_key", JSON.stringify(query[i].id));
   alert(sessionStorage.getItem("list_data_key"));
}


在上面的for循环中,我将id值放置在会话存储中,我面临的问题是,如果循环执行多次,则在sessionStorage中,最后一个值仅存储并仅使用该值加载我的最后一条记录.

但是我想为每次迭代检索ID值,并使用所有值显示所有记录.


In the above for loop i am putting the id value in the session storage,the problem i am facing is that,If the loop execute multiple times then in the sessionStorage the last value is only storing and loading my last record with that value only.

but i want for each iteration i want to retrieve the id value and with all the values I have to display all records.

how to do this?

推荐答案

这完全是错误的想法.让我们跟随发生的事情:

您要检索某个数据容器(不是JSON)的所有值的id值.然后,您出于某种奇怪的原因尝试在一个循环中执行此操作,并在循环内调用警报.换句话说,您以某种错误的方式编写了循环,因此结果无法令人满意.接下来你要做什么? 你责怪循环!问:如何不循环?".检查您的逻辑!

来吧,它将无处可去.当您遇到编译错误时,您是否应归咎于编译器?还是平台?为什么不?有些人就是这样.但是问题...

在您的情况下,您应该改用大脑.您需要获取所有值,然后显示结果.收集循环中的所有结果,并将它们存储在某个容器(数组)中;然后,在循环之后,使用此数组显示所有记录.



下次,当问问题时,最好不要告诉使用什么和不使用什么.这很少是正确的.更好地专注于您自己的问题,不要忘了解释您的最终目标.没有多少人愿意浪费时间基于误解来回答问题,因此大多数专家都需要知道您的目的.

—SA
This it totally wrong thinking. Let''s follow what happens:

You want to retrieve id values for all the values of some data container, JSON or not. Then you tried to do it in a loop and call alert inside the loop, by some weird reason. In other way, you write the loop in some wrong way, so the result cannot satisfy you. And what do you do next? You blame the loop! And ask: "how to do it without a loop?". Check up you logic!

Come on, it will lead your nowhere. When you had a compilation error, did you blame a compiler? Or the platform? why not? Some people are like that. But the question…

In your case, you should use your brain instead. You need to get all values, and then display the result. Collect all the results in the loop and store them in some container, an array; and, later, after the loop, use this array to display all records.



And next time, when asking question, better don''t tell what to use and what''s not. It''s rarely can be correct. Better focus on your problem itself, and don''t forget to explain your ultimate goal. Not many would like to waste time on answering question based on misconception, so most experts would need to know your purpose.

—SA


这篇关于如何在不使用for循环的情况下检索值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆