[] ? !![] : ![];输出结果是什么?
let val = [] ? !![] : ![];
console.log(val);
//true:[] 是一个null,做判断则为false,false执行![]语句,结果为非空,即true
下面代码输出什么?
let k;
for(let i = 0,j = 0;i < 10,j < 8; i++, j++){
k = i + j;
}
答案是:14。这里的i和j是同步增长,当j加到7的时候,i也等于7,k执行等于14,j再加1,不满足条件,跳出循环,结果为14,如果再问i和j的值,则都为8。
处理杂乱无章的数据
有这样一串杂乱无章的数据————dahsidoai 213907;a poas198jdo 213089 as13d115。我希望它输出[“213907”, “198”, “213089”, “13”, “115”],请写出实现过程
let str = "dahsidoai 213907;a poas198jdo 213089 as13d115";
function searchNumUnit(str){
let arr = [],
str0 = str, //不影响原数据
reg = /\d+/;
while(reg.test(str0)){
arr.push(reg.exec(str0)[0]);
str0 = str0.split('').slice(reg.exec(str0).index + reg.exec(str0)[0].length,str0.length).join('');
}
return arr;
}
console.log(searchNumUnit(str));
综合题
下面的程序输出什么?假如说我用"==>"表示程序延迟了多久输出,比如1,2 ==> 3 ,表示12同时输出,之后间隔1000ms输出了3
for(var i = 0; i < 6; i ++){
setTimeout(function(){
console.log(i);
},1000);
}
// 输出结果:6,6,6,6,6,6
那我如果想输出0 , 1 , 2 , 3 , 4 , 5呢?得怎么写?能否通过多种方式来写出?(最少2种)
//第1种方式:
for(var i = 0; i < 6; i ++){
(function(j){
setTimeout(function(){
console.log(j);
},1000);
})(i)
}
//第2种方式:
for(let i = 0; i < 6; i ++){
setTimeout(function(){
console.log(i);
},1000);
}
//输出结果:0 , 1 , 2 , 3 , 4 , 5
那我如果是想输出0 ==> 1 ==> 2 ==> 3 ==> 4 ==> 5程序得怎么改变?
// 代码如下:
for(var i = 0; i < 6; i ++){
(function(j){
setTimeout(function(){
console.log(j);
},1000 * j);
})(i)
}
上面这种代码能实现逻辑,但代码太烂,没法给你加分,有没有更好的办法?另外我为什么说你的代码太烂?能顺便说明一下吗?
//首先我的代码太烂是因为我创建了太多的定时器,仅仅这里我就创建了6个定时器,如果i值非常大,会非常消耗资源,大大降低执行性能
//优化代码如下:这里的好处是即使你的i1值达到10000甚至1亿,我始终只有1个定时器。
let i1 = 0;
let time = setInterval(output_i1,1000);
function output_i1(){
i1 < 6 ? console.log("i1=" + i1++) : clearInterval(time);
}
这样算可以给你加5分,如果我不是0 , 1 , 2 , 3 , 4 , 5呢?而是0,1,2,3,4,5…简单的说能否给我自定义?
这个简单啊,改成这样不就可以了?
let i1 = 0;
let time = setInterval(output(6),1000);
function output(num){
i1 < num-1 ? console.log(++i1) : clearInterval(time);
}
可惜你这个函数是错的,setInterval接收的是一个function:output,而不是接收一个已经运行的output(),所以呢?你得怎么改?
//改成这样:
let i2 = 0;
let time1 = setInterval(_output_i2(8),1000);
function _output_i2(num){
return function(){
output_i2(num);
}
}
function output_i2(num){
i2 < num ? console.log("i2="+ i2++) : clearInterval(time1);
}
如果你到了这一步,嗯,还行,勉强达到了基本要求,但是呢,其实这里涉及到异步,用promise又得怎么写?还能再进一步吗?答案是肯定的,不过呢,先答下一题吧。
这是一道简单的数据处理题
一个树形json数据有3层,基本的结构如下:(…代表后续有若干个类似的对象)
数据大概是这样:
[
{
id : "1",
name : "dorsey1",
children: [
{
id : "1-1",
name : "dorsey1-1",
children : [
{
id : "1-1-1",
name : "dorsey1-1-1",
children : [
{
id : "1-1-1-1",
name : "dorsey1-1-1-1",
}
...
]
}
...
]
}
...
]
}
...
]
请写一个函数传入id值返回name值,另外呢?这里虽说只是3层,能否拓展到若干层?
let data = [
{
id : "1",
name : "dorsey1",
children: [
{
id : "1-1",
name : "dorsey1-1",
children : [
{
id : "1-1-1",
name : "dorsey1-1-1",
children : [
{
id : "1-1-1-1",
name : "dorsey1-1-1-1",
}
]
},
{
id : "1-1-2",
name : "dorsey1-1-2"
}
]
},
{
id : "1-2",
name : "dorsey1-2",
children : [
{
id : "1-2-1",
name : "dorsey1-2-1"
},
{
id : "1-2-2",
name : "dorsey1-2-2"
}
]
},
{
id : "1-3",
name : "dorsey1-3",
children : [
{
id : "1-3-1",
name : "dorsey1-3-1"
},
{
id : "1-3-2",
name : "dorsey1-3-2"
}
]
}
]
},
{
id : "2",
name : "dorsey2",
children: [
{
id : "2-1",
name : "dorsey2-1",
children : [
{
id : "2-1-1",
name : "dorsey2-1-1"
},
{
id : "2-1-2",
name : "dorsey2-1-2"
}
]
},
{
id : "2-2",
name : "dorsey2-2",
children : [
{
id : "2-2-1",
name : "dorsey2-2-1"
},
{
id : "2-2-2",
name : "dorsey2-2-2"
}
]
}
]
}
]
这是基本的json解析,请看下面的实现:
//最原始的实现如下:
function getJsonDataNameById(dataArr,id) {
let agent = [];
let data = dataArr,
len = data.length;
for (let i = 0; i < len; i++) {
let item = data[i];
if(item.children && item.children.length !== 0){
for(let j = 0 ; j < item.children.length; j ++){
agent.push(item.children[j]);
}
data = data.concat(agent); //children降维
len += agent.length;
agent = [];
}
}
for(let i = 0; i < data.length; i++){
if(data[i].id === id){
return data[i].name;
}
}
}
let a = getJsonDataNameById(data, "1-3-2");
console.log(a);