1、[] ? !![] : ![];输出结果是什么?
let val = [] ? !![] : ![];
console.log(val);
//true:
//之前的错误解释:[] 是一个null,做判断则为false,false执行![]语句,结果为非空,即true
//更正:[]是一个object,object判断为true(null实际上也是一个object不过比较特殊是6种false
// 之一),true执行 !![] 两次取反为true.多谢@此生只为你倾心指正。
这里顺便说明一下,js用于做判断,只有以下6种情况判断出来的值为false:
值为false: false 0 null ""(空字符串) undefined NaN
其他均为true。
延展一下,有个比较有意思的情况是:[] == ![]; //true,
还有[]我们刚刚判断为true对吧? 但是呢 [] == false; //true,
为什么呢?
== :趋向于先转换成0和1再做判断,一起来看看吧
先看[] == false首先把比较的双方转成number(双方有一方是布尔值),[]是空数组,转换成基本类型为"",空所以被转化为0,false也被转成0。0 == 0,true(不过如果用恒等于 ===则为false,因为两者数据类型不一致,[]为object(里的array),false为布尔值)
再看[] == ![]; []是object即true,!true ==> false; false ==> 0; 而[]我们知道最终转化为0,所以 0 == 0; //true;
[] == [] 实际上是false哈。是不是很别扭?
所以:慎用 "==",用“===”
2、下面代码输出什么?
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。
3、有这样一串杂乱无章的数据————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));
4、下面是一道综合题,问题会由浅及深一步步问,你需要一步步解决:
下面的程序输出什么?假如说我用"==>"表示程序延迟了多久输出,比如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又得怎么写?还能再进一步吗?答案是肯定的,不过呢,先答下一题吧。
5、这是一道简单的数据处理题
一个树形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);
原文:https://www.imooc.com/article/48993
閱讀更多 全棧取經之路 的文章