简体   繁体   English

从 javascript 中的 arrays 数组中加入和拆分元素

[英]Joining and Splitting an element from an array of arrays in javascript

So I am learning JavaScript slowly but surely, can someone help me figure out the most efficient way of using join() and split() for very large array or arrays?所以我正在缓慢但肯定地学习 JavaScript,有人可以帮我找出将join()split()用于非常大的数组或 arrays 的最有效方法吗? Im talking thousands or lines from a spreadsheet... This is what I have so far.我说的是电子表格中的数千行......这就是我到目前为止所拥有的。

 function joinsplitPaste () { var data = [["mike",33,,"KS","male"],["jan",32," ","KS","female"],["dave",33,,"KS","male"],["mike",33, ,"KS","male"],["stan",31,,"KS","male"],["beth",19,"dataText","KS","female"]]; var dataJoin = []; //sample is made up for (i = 0;i<data.length;i++) { return data[i].join(" "); dataJoin.push(data[i]); } var dataSplit = []; /* output - [["mike 33 "KS" "male"],["jan" 32 " " "KS" "female"],["dave" 33 "KS" "male"], and so on...*/ for (i = 0;i<data.length;i++) { return dataJoin[i].Split(" "); dataSplit.push(dataJoin[i]); } //hopefully dataSplit == data? }

Any suggestions would be appreciated, Thank you.任何建议将不胜感激,谢谢。

based on the last comment in your sample code, I am assuming the endgoal you are after is that 'dataSplit' array should be equal to original 'data' array.根据示例代码中的最后一条评论,我假设您追求的最终目标是“dataSplit”数组应该等于原始“data”数组。

However, there are 2 problems with the sample code due to which you can not achieve what you wish:但是,示例代码存在 2 个问题,导致您无法实现您想要的:

  1. presence of 'return' statement inside 'for' loop: which stops execution of first loop just after first iteration and prevents second loop from even getting started.在“for”循环中存在“return”语句:在第一次迭代后停止执行第一个循环,甚至阻止第二个循环开始。

  2. equality check doesn't work for non-primitive data types: array and object data types in javascript are non-primitive and equality check never works for them unless the two operands are really pointing to the same location in memory.相等检查不适用于非原始数据类型:javascript 中的数组和 object 数据类型是非原始的,除非两个操作数确实指向 ZCD69B4957F06CD818D7ZBF3D691 中的同一位置,否则相等检查对它们不起作用

Eg: {} == {} // false (both objects, however identical, point to 2 different memory locations so they are never 'equal')例如:{} == {} // false(两个对象,尽管相同,都指向 2 个不同的 memory 位置,因此它们永远不会“相等”)

Similarly, [1,2,3] == [1,2,3] // false (for the reason same as described for objects)类似地,[1,2,3] == [1,2,3] // false(原因与对对象的描述相同)

Coming to the sample code, I have a revised version for you to generate 'dataSplit' array identical to original 'data' array but never equal since they are two different variables, hence, different memory locations.来到示例代码,我有一个修订版本供您生成与原始“数据”数组相同但从不相等的“dataSplit”数组,因为它们是两个不同的变量,因此,不同的 memory 位置。 The use of join and split, however, is correct.然而,join 和 split 的使用是正确的。 Revised sample:修订样本:

function joinsplitPaste(){
  var data = [["mike",33,,"KS","male"],["jan",32," ","KS","female"],["dave",33,,"KS","male"],["mike",33, ,"KS","male"],["stan",31,,"KS","male"],["beth",19,"dataText","KS","female"]];
  var dataJoin = [];
  for (i = 0;i<data.length;i++){
    dataJoin.push(data[i].join(" ")) ;
  }
  var dataSplit = [];
  for (i = 0;i<data.length;i++){
    dataSplit.push(dataJoin[i].split(" ")) ;
  }
}

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

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