简体   繁体   English

乘法2数组javascript / jquery

[英]Multiplication 2 arrays javascript/jquery

I have two arrays of objects in js those looks like this: 我在js中有两个对象数组,看起来像这样:

Array1
   0:Object
      name: "name1"
      value:7
   1:Object
      name: "name2"
      value:5
   2:Object
      name: "name3"
      value:6

Array2
   0:Object
      name: "name1"
      value:3
   1:Object
      name: "name2"
      value:4
   2:Object
      name: "name3"
      value:8

I'd like to create a third array which will contain results from multiplication values from array1 and array2 (doesn't need to be an objects array - could contain only int values). 我想创建第三个数组,其中包含来自array1和array2的乘法值的结果(不需要是对象数组-可以仅包含int值)。 This mean: 这个意思:

Array1        Array2        Array3
value:7  *    value:3   =   value:21
value:5  *    value:4   =   value:20
value:6  *    value:8   =   value:48

Do you know an easy and good way to create this third table with values like above and display it on website? 您知道一种简单而又好用的方法来创建具有上述值的第三个表并将其显示在网站上吗? Thanks in advance :) 提前致谢 :)

Pure javascript, no jQuery needed: 纯JavaScript,无需jQuery:

 function addArrays(arr1, arr2, prop) { var func = function(curr) { return curr[prop]; } var arr1v = arr1.map(func), arr2v = arr2.map(func), output = []; arr1v.forEach(function(curr, i){ output[i] = arr1v[i] * arr2v[i]; }); return output; } addArrays([{value:7},{value:5},{value:6}],[{value:3},{value:4},{value:8}],"value") //->[21, 20, 48] 

You can use the power of LINQ to join the two result sets and select a third column as the result of the multiplication of the two source columns. 您可以使用LINQ的功能来合并两个结果集,并选择第三列作为两个源列相乘的结果。 (Just like you would do in SQL) (就像在SQL中一样)

The JavaScript implementation of LINQ can come in quite handy here. LINQ的JavaScript实现在这里可以派上用场。 Check out LinqJS: 查看LinqJS:

http://linqjs.codeplex.com/ http://linqjs.codeplex.com/

Here's a good article on it http://www.codeproject.com/Articles/603742/LINQ-for-JavaScript 这是一篇很好的文章, 网址为http://www.codeproject.com/Articles/603742/LINQ-for-JavaScript

$Linq also looks promising https://jscriptlinq.codeplex.com/ $ Linq看起来也很有前途https://jscriptlinq.codeplex.com/

You can do it in multiple ways: 您可以通过多种方式进行操作:

Option 1: Pure JavaScript nested loops You can do it with pure JavaScript just with two nested loops. 选项1:纯JavaScript嵌套循环只需两个嵌套循环,就可以使用纯JavaScript做到这一点。 It is relatively slow on big arrays, but quite simple. 在大型阵列上,它相对较慢,但非常简单。

Option 2: Pure JavaScript loop with preindexation You can do it preindex one of arrays to fast access to it. 选项2:具有预索引的纯JavaScript循环可以对数组之一进行预索引以快速访问它。

Option 3: Using special library As an alternative you can use special SQL or LINQ (as mentioned in the other answer) libraries, eg Alasql , where the library automatically creates index on the second array to faster access to avoid unnecessary loops. 选项3:使用特殊的库作为替代方案,您可以使用特殊的SQL或LINQ(如在其他答案中提到的)库,例如Alasql ,其中该库自动在第二个数组上创建索引以更快地访问以避免不必要的循环。

Performance: You can compare the speed of all three variants on joining 10000 x 10000 arrays (simple nested loops and with preindexation) in this jsPerf test . 性能:此jsPerf测试中,您可以比较连接10000 x 10000数组(简单的嵌套循环和预索引 )时所有三个变体的速度。 Option 2 is fastest. 选项2最快。

See the working snippet below with all three options or play with it in jsFiddle . 参见下面所有三个选项的工作片段,或在jsFiddle中使用它。

(Disclaimer: I am an author of Alasql) (免责声明:我是Alasql的作者)

 var Array1 = [{name:'name1',value:7},{name:'name2',value:5},{name:'name3',value:6}]; var Array2 = [{name:'name1',value:3},{name:'name2',value:4},{name:'name3',value:8}]; // 1. Pure JavaScript option with nested loops var Array3 = []; for(var i=0;i<Array1.length;i++) { for(var j=0;j<Array2.length;j++) { if(Array1[i].name == Array2[j].name) { Array3.push({value:Array1[i].value * Array2[i].value}); break; } } } document.getElementById("res1").textContent = JSON.stringify(Array3); // 2. Preindexation version var idx = {}; for(var j=0;j<Array2.length;j++) { idx[Array2[j].name] = Array2[j].value; } var Array3 = []; for(var i=0;i<Array1.length;i++) { Array3.push({value:Array1[i].value * idx[Array1[i].name]}); } document.getElementById("res2").textContent = JSON.stringify(Array3); // 3. SQL library option var Array3 = alasql('SELECT Array1.[value]*Array2.[value] AS [value] \\ FROM ? Array1 JOIN ? Array2 USING name',[Array1, Array2]); document.getElementById("res3").textContent = JSON.stringify(Array3); 
 <script src="http://alasql.org/console/alasql.min.js"></script> <p>Pure JavaScript nested loops: </p><div id="res1"></div> <p>Pure JavaScript preindexation: </p><div id="res2"></div> <p>Alasql version: </p><div id="res3"></div> 

Try this 尝试这个

var ar3 = [];
for(var i = 0; i <= array1.length; i++){
    var valu = array1[i].value * array2[i].value;
    ar3[i] = valu;
}

Really ,i am wondered why there are a complicated answers here ,while , Javascript is a functional programming language : 真的,我很想知道为什么这里会有一个复杂的答案,而Javascript是一种功能编程语言:

Just one line : 仅一行:

array3= array1.map(function(e,i){return {value : (e.value * array2[i].value)}; }) ; 

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM