有人可以解释这个javascript代码吗?它用于从两个数组输出唯一值。 [英] Can someone explain this javascript code please? It is used to output unique values from two arrays.

查看:38
本文介绍了有人可以解释这个javascript代码吗?它用于从两个数组输出唯一值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数唯一(array1,array2){

var obj1 = {};

for(var i = array1.length-1; i> = 0; - i)

obj1 [array1 [i]] = array1 [i];

for(var i = array2.length-1; i> = 0; - i)

obj1 [array2 [i]] = array2 [i];

var result = []

for(var k在obj1){

结果+ =(obj1 [k]);



}

返回结果;

}



我的尝试:



我只想知道它是如何工作的,当我在浏览器调试器中查看它时没有多大意义。

解决方案

我认为只有当array1和array2是字符串变量的每个数组时,它才会起作用,它对任意数组类型都不起作用。



此外,这些字符串不能是任意的。具体来说,它们不能包含空格(数组元素某些东西不起作用,但某些东西就行了。)



我已经注释了下面的代码来解释它。编写它的人应该有点羞于没有提供一些解释性注释,尤其是函数头来解释所需的参数类型。尝试捕捉块也不会出错。



无论如何,这里是:



< pre lang =Javascript> function unique(array1,array2){

var obj1 = {}; // 声明一个新对象(参见下面的参考资料)。


// 循环遍历array1,从头到尾工作。
// 获取每个字符串元素并将其用作
// obj1中要创建的新属性,该属性
// 被分配相同的字符串值。
for var i = array1.length-1; i> = 0 ; - i)
obj1 [array1 [ i]] = array1 [i];

// 类似于在array2上循环,复制每个元素(string
// value)将obj1作为属性。
//
// 这是实现唯一性的地方,因为如果
// array2 [i ]也包含在array1 [< any index>]
// 中字符串值已经存在
// 在obj1中,下面的循环只会覆盖
// 在现有属性之上的相同值。
for var i = array2.length-1; i> = 0 ; - i)
obj1 [array2 [i]] = array2 [i];

// 声明一个数组来存储结果。
< span class =code-keyword> var
result = []

// 将obj1中的属性值复制到结果中。这些
// 属性是唯一列出的字符串集
// 在array1,array2中。
for var k in obj1){
result + =( OBJ1 [K]);
}

返回结果;
}





obj1的声明如下 Javascript最佳实践,用于声明新对象。



理解上述代码的关键如下:



考虑一个Javascript对象myObj,包含诸如prop1,prop2等属性。您可能熟悉使用这种表示法访问这些对象:

  var  prop1Val = myObj.prop1; 
var prop2Val = myObj.prop2;



但是,你也可以使用这个来访问这些属性符号:

  var  prop1Val = myObj [  prop1]; 
var prop2val = myObj [ prop2 ];





希望您现在可以看到为什么我在开始时说过array1,2必须只包含简单的字符串不包含空格。如果他们这样做你会尝试这样做:

  var  prop1Val = myObj [< span class =code-string>  some something]; 



哪个为你现在知道用更熟悉的符号来表示:

  var  prop1Val = myObj.some事情;  //  这是一个无效的语法! 





任何怀疑Javascript之美的人都无法真正了解这种语言。这些结构 - 尽管它们可能令人困惑 - 非常强大。这是一种远远超过时代的语言!


function unique (array1, array2) {
var obj1 = {};
for (var i = array1.length-1; i >= 0; -- i)
obj1[array1[i]] = array1[i];
for (var i = array2.length-1; i >= 0; -- i)
obj1[array2[i]] = array2[i];
var result = []
for (var k in obj1) {
result +=(obj1[k]);

}
return result;
}

What I have tried:

I just want to know exactly how it works, when I look at it in the browser debugger it doesn't make much sense.

解决方案

I think that this will ONLY work if array1 and array2 are each arrays of string variables, it won't work for arbitrary array types.

Furthermore, those strings cannot be arbitrary. Specifically they cannot contain white-space (array element "some thing" would not work but "something" would be OK).

I have annotated the code below to explain it. Whoever wrote it should be a little ashamed for not providing some explanatory comments, not least a function header to explain the required argument types. A try-catch block wouldn't have gone amiss either.

Anyway here goes:

function unique (array1, array2) {

   var obj1 = {};  // Declare a new object (see reference below).


    // Loop over array1, working from end to beginning.
    // Take each string element and use this as a key for
    // a to-be-created new property within obj1, that property
    // being assigned the same string value.
    for (var i = array1.length-1; i >= 0; -- i)
      obj1[array1[i]] = array1[i];

    // Similarly loop over array2, copying each element (string
    // value) into obj1 as a property.
    //
    // This is where the uniqueness is achieved because if
    // array2[i] was also contained within array1[<any index>]
    // then this particular string value will already exist
    // within obj1 and the loop below will simply overwrite the
    // same value on top of the existing property.
    for (var i = array2.length-1; i >= 0; -- i)
      obj1[array2[i]] = array2[i];

    // Declare an array to store results.
    var result = []

    // Copy property values from obj1 into the results. These
    // properties are the set of strings which are uniquely listed
    // in array1, array2.
    for (var k in obj1) {
      result +=(obj1[k]);
    }

    return result;
}



The declaration of obj1 follows Javascript best practice for the declaration of a new object.

The key to understanding the above code is the following:

Consider a Javascript object, myObj, containing properties such as "prop1", "prop2" etc. You are probably familiar with accessing those objects using this notation:

var prop1Val = myObj.prop1;
var prop2Val = myObj.prop2;


However, you can also access those properties using this notation:

var prop1Val = myObj["prop1"];
var prop2val = myObj["prop2"];



Hopefully you can now see why I said at the start that array1,2 must only contain simple strings that don't contain white-space. If they did you'd be trying to do something like this:

var prop1Val = myObj["some thing"];


Which as you now know would correspond, in the more familiar notation, to:

var prop1Val = myObj.some thing;  // WHICH IS AN INVALID SYNTAX!



Anyone whoever doubted the beauty of Javascript can't truly know the language. These kind of constructs - although they can be confusing - are exceptionally powerful. It's a language that was well ahead of its time!


这篇关于有人可以解释这个javascript代码吗?它用于从两个数组输出唯一值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆