基于另一个数组对对象数组进行排序

时间:2022-09-25 07:54:30

So, there will probably be guys that will say this is a duplicate, but I read many relatively similar questions and still I'm unable to find an answer that resolves this particular problem, I've even tried some npm modules that claim to solve this, but to no avail...

所以,可能会有人会说这是重复的,但我读了许多相对类似的问题,但我仍然无法找到解决这一特定问题的答案,我甚至尝试了一些声称解决的npm模块这,但无济于事......

Well, I have an array of objects:

好吧,我有一个对象数组:

 var objArr = [{ '5': 1,'6': 1,'34': 3,id: 1,question: '12',author: 'Zoran  Stanić' }];

and another array which should help in sorting the first array:

和另一个应该有助于排序第一个数组的数组:

var sortingArr = [ 'id', 'question', 'author', '34', '5', '6' ];

The result should be this:

结果应该是这样的:

[{id: 1, question: '12', author: 'Zoran  Stanić', '34': 3, '5': 1, '6': 1}]

How can I achieve this?

我怎样才能做到这一点?

P.S. These are the values from the database, and the order of objArr could be completely different than the one in example, depending on the db logic - it's MySQL thingy.

附:这些是数据库中的值,objArr的顺序可能与示例中的顺序完全不同,具体取决于db逻辑 - 它是MySQL的东西。

3 个解决方案

#1


0  

Note: you cannot sort an object's keys, that depends on browser implementation....nor does it matter what "order" an objects keys are in, it only matters to you or your users what you display/how you display it.

注意:您无法对对象的键进行排序,这取决于浏览器的实现....对于对象键的“顺序”也不重要,只对您或您的用户显示您显示的内容/显示方式。

If you want to print things out in a certain way, then you can do the following:

如果您想以某种方式打印出来,那么您可以执行以下操作:

var arrOfObjects = [{...}];
var sortingArr = [ 'id', 'question', 'author', '34', '5', '6' ];

arrOfObjects.forEach(function(obj) {
   sortingArr.forEach(function(k) {
      console.log(k + ':' + obj[k]);
   });
});

#2


0  

Objects cannot be sorted in JS. You'd either have to do create your own implementation using an Array. If you can use ES6, Map will maintain the order with which pairs are added. The link below talks about this:

无法在JS中对对象进行排序。您要么必须使用Array创建自己的实现。如果您可以使用ES6,Map将保持添加对的顺序。以下链接说明了这一点:

http://www.jstips.co/en/javascript/map-to-the-rescue-adding-order-to-object-properties/

#3


0  

Alternatively, you can have an array or arrays, in which every array conforms to the structure of sortingArr, like this:

或者,您可以拥有一个或多个数组,其中每个数组都符合sortingArr的结构,如下所示:

var objArr = [{ '5': 1,'6': 1,'34': 3,id: 1,question: '12',author: 'Zoran  Stanić' }];

var sortingArr = [ 'id', 'question', 'author', '34', '5', '6' ];

var sortedArrsVals = objArr.map(el => 
  sortingArr.map(e => el[e])
)

console.log(sortedArrsVals)

#1


0  

Note: you cannot sort an object's keys, that depends on browser implementation....nor does it matter what "order" an objects keys are in, it only matters to you or your users what you display/how you display it.

注意:您无法对对象的键进行排序,这取决于浏览器的实现....对于对象键的“顺序”也不重要,只对您或您的用户显示您显示的内容/显示方式。

If you want to print things out in a certain way, then you can do the following:

如果您想以某种方式打印出来,那么您可以执行以下操作:

var arrOfObjects = [{...}];
var sortingArr = [ 'id', 'question', 'author', '34', '5', '6' ];

arrOfObjects.forEach(function(obj) {
   sortingArr.forEach(function(k) {
      console.log(k + ':' + obj[k]);
   });
});

#2


0  

Objects cannot be sorted in JS. You'd either have to do create your own implementation using an Array. If you can use ES6, Map will maintain the order with which pairs are added. The link below talks about this:

无法在JS中对对象进行排序。您要么必须使用Array创建自己的实现。如果您可以使用ES6,Map将保持添加对的顺序。以下链接说明了这一点:

http://www.jstips.co/en/javascript/map-to-the-rescue-adding-order-to-object-properties/

#3


0  

Alternatively, you can have an array or arrays, in which every array conforms to the structure of sortingArr, like this:

或者,您可以拥有一个或多个数组,其中每个数组都符合sortingArr的结构,如下所示:

var objArr = [{ '5': 1,'6': 1,'34': 3,id: 1,question: '12',author: 'Zoran  Stanić' }];

var sortingArr = [ 'id', 'question', 'author', '34', '5', '6' ];

var sortedArrsVals = objArr.map(el => 
  sortingArr.map(e => el[e])
)

console.log(sortedArrsVals)