简体   繁体   English

如何基于另一个JavaScript数组对JavaScript中的数组进行排序?

[英]How to Sort an array in JavaScript based on another JavaScript array?

I want to Sort an array in JavaScript based on another JavaScript array. 我想基于另一个JavaScript数组对JavaScript中的数组进行排序。

First arr = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];

Second arr = [Name: "kd", State: "test", Address1: "tt"]

Now I want to sort the second arr in the order of the first arr. 现在,我要按第一个arr的顺序对第二个arr进行排序。 I want this type of result: 我想要这种类型的结果:

[Name: "kd", Address1: "tt", State: "test"]

You can sort second array by itrating over first array as below: 您可以通过对第一个数组进行迭代来对第二个数组进行排序,如下所示:

Firstarr = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];
Secondarr = {Name: "kd", State: "test", Address1: "tt"};

var finalArr = {};

$.each(Firstarr, function(i, value){
      if(typeof Secondarr[value] !== "undefined"){
         finalArr[value] =  Secondarr[value];
      }
});

You have to use JSON format for second array. 您必须对第二个数组使用JSON格式。

 var reference = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"]; var toSort = { "Name": "kd","State": "test", "Address1": "tt"}; var sorted={}; for(var i=0;i<reference.length;i++) { if(toSort.hasOwnProperty(reference[i])) { sorted[reference[i]]=toSort[reference[i]]; } } console.log(sorted); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

This is a simple algorithm in js without jQuery. 这是没有jQuery的js中的简单算法。 orderArray[i] could be called a key and the associative arrays in js are really objects, but that is just semantics. orderArray [i]可以称为键,而js中的关联数组实际上是对象,但这只是语义。

 myOrderArray = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"]; myUnsortedArray = { Name: "kd", State: "test", Address1: "tt" }; function sortArrayByArray( unsortedArray, orderArray ) { let sortedArray = {}; for ( var i = 0; i < orderArray.length; i++ ) { if ( orderArray[i] in unsortedArray ) { sortedArray[orderArray[i]] = unsortedArray[orderArray[i]]; } } return sortedArray; } mySortedArray = sortArrayByArray( myUnsortedArray, myOrderArray ); console.log( mySortedArray ); 

As I have already commented, second one looks more like an Object, not an array. 正如我已经评论过的,第二个看起来更像是一个对象,而不是数组。 It has to be initialized as var second = {Name: "kd", State: "test", Address1: "tt"}; 它必须初始化为var second = {Name:“ kd”,State:“ test”,Address1:“ tt”};

Your final output would be in a Array() which is enclosed in an Object called finlObj. 您的最终输出将在Array()中,该数组包含在名为finlObj的对象中。 Please find the below code. 请找到下面的代码。 You can optimize the code. 您可以优化代码。 But for your understanding, i did in a elaborate way: 但是为了您的理解,我做了精心设计:

 var second = {Name: "kd", State: "test", Address1: "tt"};
     var first = ["Name", "Address1", "Address2", "City", "State", "Zip Code", "Formatted Address"];
     var finlObj ={specList:[]};

     for(var elem in first){
           if(second[first[elem]] !== undefined && second[first[elem]] !== null){


     finlObj.specList[finlObj.specList.length] = second[first[elem]];
     }
     }

     for(var el in finlObj.specList){
     alert(finlObj.specList[el]);

     }

This won't work. 这行不通。 In JavaScript, by virtue of an array being an object, it will accept non-integer keys as you show here, but neither the runtime or provided methods for working with arrays will allow you any control over their order. 在JavaScript中,由于数组是对象,它会接受此处显示的非整数键,但是运行时或提供的用于数组的方法都不允许您对其顺序进行任何控制。 This is completely up to the interpreter implementation. 这完全取决于解释器的实现。

Likewise JavaScript objects are unsortable and, while they usually appear in alphabetical order when displayed in debug tools, this is generally a convenience provided by the browser. 同样,JavaScript对象也是不可排序的,尽管它们在调试工具中显示时通常按字母顺序显示,但这通常是浏览器提供的便利。

What you need is a Map . 您需要的是Map You can sort this with the representations you have shown here. 您可以使用此处显示的表示形式对此进行排序。

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

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