Javascript 数组迭代
Javascript array iteration
我在 sessionStorage
中有一个 JavaScript 数组,当我执行 sessionStorage.getitem('states')
时,我会变成这样:
"{
"TamilNadu": [
"Chennai",
"Trichy",
"Madurai"
],
"Andhra": [
"Hyderabad",
"Vizhag"
],
"Karnataka": [
"Bangalore",
"Mysore",
"Darwad"
],
"Maharashtra": [
"Mumbai",
"Pune"
]
}"
现在我的要求是从城市名中获取州名。
例如,如果我将 "Mumbai"
作为参数传递给一个函数,该函数应该读取此 sessionstorage
值和 return 我 "Maharashtra"
.
你能帮我实现这个目标吗?
此代码:
sessionStorage.getitem('states')
returns 一个字符串。您必须使用以下方法解析它:
var obj = JSON.parse(sessionStorage.getitem('states'))
接下来用方括号或点访问对象属性:
for (var attr in obj) {
if (obj.hasOwnProperty(attr) && obj[attr][0] == 'Mumbai') {
return attr;
}
}
return null;
function getStateByCityName (cityName) {
var states = {};
try {
states = JSON.parse(sessionStorage.getitem('states'));
} catch (e) {
}
for (var state in states) {
for (var i = 0; i < states[state].length; i ++) {
if (states[state][i] == cityName)
return state;
}
}
return null;
}
应该可以,但无法测试:
function (arrayTarget, itemToSearch){
for (x in arrayTarget) {
for(y in arrayTarget[x])
if(itemToSearch == arrayTarget[x][y])
return x;
}
}
或者直接使用if (states[state].indexOf(cityName) !== -1)
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
for (var state in states) {
if (states[state].indexOf(cityName) !== -1) {
return state;
}
}
return false;
}
或者在 EcmaScript 5 循环样式中:))
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
Object.keys(states).forEach(function(state) {
if (states[state].indexOf(cityName) !== -1) {
return state;
}
})
return false;
}
有很多方法(也请参阅其他答案)所以让我们看一下性能。
首先遍历你的对象
http://jsperf.com/object-keys-vs-for-in-with-closure/3
在我的浏览器中最快的情况是 Object.keys for loop
,所以
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
// check if city exists
}
return false;
}
接下来我们需要检查该州是否存在城市。我看到两种方法:
1) 正如其他人建议的那样,使用 for
循环进行迭代
2) 我的建议 .indexOf()
http://jsperf.com/js-for-loop-vs-array-indexof/2
在我的浏览器中 indexOf()
得到了更好的结果,所以最终函数
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
if (states[states_keys[i]].indexOf(cityName) !== -1) {
return states_keys[i];
}
}
return false;
}
并且有人建议使用 try/catch
getStateByCityName(cityName) {
var states = {};
try {
states = JSON.parse(sessionStorage.getitem('states'));
} catch (e) {
}
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
if (states[states_keys[i]].indexOf(cityName) !== -1) {
return states_keys[i];
}
}
return false;
}
我在 sessionStorage
中有一个 JavaScript 数组,当我执行 sessionStorage.getitem('states')
时,我会变成这样:
"{
"TamilNadu": [
"Chennai",
"Trichy",
"Madurai"
],
"Andhra": [
"Hyderabad",
"Vizhag"
],
"Karnataka": [
"Bangalore",
"Mysore",
"Darwad"
],
"Maharashtra": [
"Mumbai",
"Pune"
]
}"
现在我的要求是从城市名中获取州名。
例如,如果我将 "Mumbai"
作为参数传递给一个函数,该函数应该读取此 sessionstorage
值和 return 我 "Maharashtra"
.
你能帮我实现这个目标吗?
此代码:
sessionStorage.getitem('states')
returns 一个字符串。您必须使用以下方法解析它:
var obj = JSON.parse(sessionStorage.getitem('states'))
接下来用方括号或点访问对象属性:
for (var attr in obj) {
if (obj.hasOwnProperty(attr) && obj[attr][0] == 'Mumbai') {
return attr;
}
}
return null;
function getStateByCityName (cityName) {
var states = {};
try {
states = JSON.parse(sessionStorage.getitem('states'));
} catch (e) {
}
for (var state in states) {
for (var i = 0; i < states[state].length; i ++) {
if (states[state][i] == cityName)
return state;
}
}
return null;
}
应该可以,但无法测试:
function (arrayTarget, itemToSearch){
for (x in arrayTarget) {
for(y in arrayTarget[x])
if(itemToSearch == arrayTarget[x][y])
return x;
}
}
或者直接使用if (states[state].indexOf(cityName) !== -1)
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
for (var state in states) {
if (states[state].indexOf(cityName) !== -1) {
return state;
}
}
return false;
}
或者在 EcmaScript 5 循环样式中:))
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
Object.keys(states).forEach(function(state) {
if (states[state].indexOf(cityName) !== -1) {
return state;
}
})
return false;
}
有很多方法(也请参阅其他答案)所以让我们看一下性能。
首先遍历你的对象 http://jsperf.com/object-keys-vs-for-in-with-closure/3
在我的浏览器中最快的情况是 Object.keys for loop
,所以
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
// check if city exists
}
return false;
}
接下来我们需要检查该州是否存在城市。我看到两种方法:
1) 正如其他人建议的那样,使用 for
循环进行迭代
2) 我的建议 .indexOf()
http://jsperf.com/js-for-loop-vs-array-indexof/2
在我的浏览器中 indexOf()
得到了更好的结果,所以最终函数
getStateByCityName(cityName) {
var statesRaw = sessionStorage.getitem('states');
var states = JSON.parse(statesRaw);
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
if (states[states_keys[i]].indexOf(cityName) !== -1) {
return states_keys[i];
}
}
return false;
}
并且有人建议使用 try/catch
getStateByCityName(cityName) {
var states = {};
try {
states = JSON.parse(sessionStorage.getitem('states'));
} catch (e) {
}
var states_keys = Object.keys(states);
for (var i = 0, l = states_keys.length; i < l; i++) {
if (states[states_keys[i]].indexOf(cityName) !== -1) {
return states_keys[i];
}
}
return false;
}