我有这个数组
[
"code:757786",
"code:940568",
"dev:P1e655f7431fce787",
"dev:P06b19c6891673644_fe1d",
"code:835130",
"code:716967",
"dev:P9b6fb5f078267bc6_fdb5",
"dev:P0a951f6ab1ca70a4_fe58",
"code:048889",
]
我希望根据"分开:"所以代码是一个数组而dev是另一个 到目前为止我有这个
var dev = [];
var code= [];
for (var i=0;i<obj.length;i++){
var tmp=obj[i].split(':');
if (tmp[0]='dev'){
dev[i]=tmp[1];
console.log(dev[i]);
$("#main-content").append(dev[i]);
}
现在在控制台日志中显示所有细节
答案 0 :(得分:0)
在这一行,您的作业=
不是比较==
。通过使用单=
,您将dev
分配给tmp[0]
,条件将始终评估为true
,从而导致if块始终输入。
if (tmp[0]='dev'){
// ^ change to double ==
答案 1 :(得分:0)
你可以这样做
var data = [
"code:757786",
"code:940568",
"dev:P1e655f7431fce787",
"dev:P06b19c6891673644_fe1d",
"code:835130",
"code:716967",
"dev:P9b6fb5f078267bc6_fdb5",
"dev:P0a951f6ab1ca70a4_fe58",
"code:048889",
];
var dev = [],
code = [];
for (var i = 0; i < data.length; i++) {
var value = data[i].split(':');
if (value[0] === 'dev') {
dev.push(value[1]);
$("#main-content").append(value[1]);
} else {
code.push(value[1]);
}
}
答案 2 :(得分:0)
RegExp怎么样?也许还有一些支票
var a = [ "code:757786", "code:940568", "dev:P1e655f7431fce787",
"dev:P06b19c6891673644_fe1d", "code:835130", "code:716967",
"dev:P9b6fb5f078267bc6_fdb5", "dev:P0a951f6ab1ca70a4_fe58", "code:048889"],
res = {dev : [], code : []};
for(var i = 0, l = a.length, tmp; i < l; i++){
tmp = a[i].match(/^(dev|code):(.*)/);
!!tmp && res[tmp[1]].push(tmp[2]);
}
console.debug(res);