In JavaScript, how do I loop through an array and add those elements as keys to an empty object?

Suppose I have an array and an empty object let array = [“a”, “b”, “c”, “d”, “e”, ” f”]let obj = {} I am trying to iterate through an array and add each element of the array as the key of an object with value 0. what should I do? I tried: for (let i = 0; i for (let key in obj) { key = array[i] obj[key] = 0 } } I think the output I want is like this console.log({obj}){a: 0, b: 0, c: 0, d: 0, e: 0, f: 0} Answer for (let key in obj) doesn’t make sense because the object is initially empty – it won’t be iterated at all. Use for (let i = 0; i obj[array[i]] = 0; } Or, more pragmatically, create the object in one go by mapping the array to an array of entries : const obj = Object.fromEntries(array.map( prop => [prop, 0]));

In JavaScript, how to infer data type (type detection)

Javascript data types can be divided into two categories: original types and reference types. Original types (basic data types) include five types: Undefined, Null, Boolean, Number and String, while reference types are also called complex types. In Javascript it is Object. Correspondingly, their values ​​are also called primitive values ​​and complex values ​​respectively. In many places we need to perform type detection. Common detection methods include the following: Inferred by typeof operator typeof 2 // numbertypeof null // objecttypeof {} // objecttypeof [] // objecttypeof undefined // undefinedtypeof ‘222’ // stringtypeof true // boolean But when using typeof to detect the reference type storage A problem arises with the value, it returns “object” regardless of what type of object is being referenced. And null is not conventional. It is a basic type, but typeof comes out as object. It can be understood as a placeholder for obj. The problem just now can be solved through the following three methods. p> Inferred through the Object.prototype.toString method This is a native prototype extension function of the object, which can be used to identify the data type more accurately. You can encapsulate it yourself for easy use. var gettype=Object.prototype.toStringgettype.call(‘#a’) // [object String] Inferred…

In JavaScript, for an array whose elements are objects, the intersection cannot be obtained using underscore? why

js array: var a = [{name:’huge’,age:23},{name:’lee’, age:24}];var b = [{name:’huge’,age:23},{name:’lee’,age:24}]; Two identical arrays , use underscore’s intersection function, and the result is []. Why?

In JavaScript, are these string tag methods used frequently?

1 anchor method | big method | blink method | bold method | fixed method | fontcolor method | fontsize method I just learned, this is from the manual , let me ask friends who have done projects, do you use all these methods?Why do I feel that they are useless? Don’t remember it!

In JavaScript, why are the attribute names of styles not consistent with CSS?

CSS attributes are generally font-weight and backgroud-color, but in Javascript, these attribute names become fontWeight and backgroudColor. Wouldn’t it be better to keep the property names the same?

In javascript, call and apply pass null or window execution efficiency

1234567891011121314 function sum (a, b, c, d) { return a+b+c+d; } var result = 0; var start = new Date().getTime(); for (var i = 0; i <10000000; i++) { result = sum.call(window, 1,2,3,4); // 2446ms //result = sum .call(null, 1,2,3,4); // 260ms } var dur = new Date().getTime() – start; alert(dur); Pass null or undefined to call or apply, Can it be regarded as changing the this of the function to window or global? Then why are the execution times of the two so different?

|| in javascript

Have you ever seen code like this:a=a||””; Javascript beginners may be confused by this. In fact: a=a||”defaultValue”; With: if(!a){a=”defaultValue”;} and: if(a==null||a==””| |a==undefined){a=”defaultValue”;} are equivalent ! In order to clarify this problem, first we must understand a problem – what happens when the data type in Javascript is converted to the bool type. In Javascript, data types can be divided into “true values” and “false values”. As the name suggests, “the true value is true when converted to bool” and the false value is false when converted to bool. The following table lists some common data types when converted to bool values: Data type The value converted to bool null FALSE undefined FALSE Object TRUE function TRUE 0 FALSE 1 TRUE Numbers other than 0 and 1 TRUE Strings TRUE “”(empty string) FALSE In the if expression, Javascript first converts the conditional expression into a bool type. If the expression is true, the logic in the if is executed. Otherwise, it is skipped. So there is: if(!a){a=”defaultValue”;} Let’s look at the two expressions “&&” and “||”. Since Javascript is a weakly typed language, these two expressions in Javascript may be different from those in other languages, such as java. In Javascript,the…

In Javascript, what is a closure (Closure)_Javascript tutorial

Two characteristics of closure: 1. As a reference to a function variable – it is active when the function returns. 2. A closure is a stack area that does not release resources when a function returns. Example 1. As a Javascript programmer, you should understand that the above code is a reference to a function. If you still don’t understand or are unclear, please understand some basic knowledge first. I won’t describe it here. Why is the above code a closure? Because there is an embedded anonymous function in the sayHello2 function sayAlert = function(){ alert(text); } In Javascript. If you create a nested function (as in the example above), you are creating a closure. In C or other mainstream languages, when a function returns, all local variables will be inaccessible because the stack where they are located has been destroyed. But in Javascript, if you declare an inline function, local variables will remain accessible after the function returns. For example, the variable sy in the above example refers to the anonymous function function(){ alert(text); } in the embedded function. The above example can be changed to this: This is consistent with the second characteristic of closure. Example 2. In…

In Javascript, how to start a server-side process and monitor its output until completion?

In my web application, I want to be able to start a process on the web server with certain parameters and continue to display the output of the process in a text area until it completes. How can I accomplish this using Javascript? I’m using jQuery and ASP.NET MVC 3. Workaround: I think you need to use something new. See for details: SignalR according to Scott Hanselman Node.js according to Scott Hanselman That being said, it’s brand new and I’m still developing it. But it looks like this development Method.

In JavaScript, why can a single closing tag of HTML written with a left slash or a right slash be displayed normally?

For example: write: 1 document.write(mycars[x]+”“); 1 document.write(mycars[x]+”“); Why is it written left-slanted Line or right slash, are they normal?

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索