[英]Javascript: Sorting Objects Challenge
I have a bit of a challenge. 我有点挑战。 I am working on a physics application with javascript.
我正在使用javascript开发物理应用程序。 The two main objects being used are
使用的两个主要对象是
var force = new Object();
var torque = new Object();
with properties
force.magnitude = newArray();
force.lengthfromorigin = new Array();
force.count;
torque.lengthfromorigin= new Array();
torque.count;
now, I'd like to sort these two objects into an array based on their respective lengthfromorigins 现在,我想根据它们各自的长度将其归类为一个数组
Example: force.lengthfromorigin = [5,8] and torque.lengthfromorigin=[2,6] so their order in this newArray would be [ torque[0], force[0], torque[1], force[1] ] 示例:force.lengthfromorigin = [5,8],torque.lengthfromorigin = [2,6],因此它们在此newArray中的顺序为[torque [0],force [0],torque [1],force [1]]
My question is it possible to have an array of different objects sorted by their respective properties, and to then use this array in a function which will make decisions based on which object is at the index. 我的问题是,可以有一个按其各自属性排序的不同对象数组,然后在一个函数中使用此数组,该函数将根据哪个对象位于索引进行决策。 Also will I need to have an id property in each respective object to identify if the object is a torque or force.
我还需要在每个对象中都具有id属性,以标识该对象是扭矩还是力。
Example: 例:
if(newArray[i] == torque)
//do stuff
else
//do other stuff.
Something like this perhaps? 大概是这样吗?
Let me explain the algorithm: 让我解释一下算法:
For each objects in objects
: 对于对象中的每个
objects
:
2.1 Let the current object be called obj . 2.1让当前对象称为obj 。
2.2 Use map
to generate a new array called tuples of [obj, num] tuples for each lengthFromOrigin
numbers of obj . 2.2使用
map
,以产生所谓的[OBJ,NUM]元组的每一个元组新的数组lengthFromOrigin
obj的号码。
3.3 Push all items of tuples into A . 3.3将所有元组推入A。
Sort A on tuple[1] (which is the number) ascending. 对tuple [1] (即数字)上的A进行升序排序。
var objects = [
{ type: 'force', lengthFromOrigin: [5, 8] },
{ type: 'torque', lengthFromOrigin: [2, 6] }
],
sorted = objects.reduce(function (arr, obj) {
arr.push.apply(arr, obj.lengthFromOrigin.map(function (num) {
return [obj, num];
}));
return arr;
}, []).sort(function (a, b) {
return a[1] - b[1];
});
console.log(sorted);
Then you can loop over sorted
and easily identify if it's a torque or force by looking at the first element in the tuple. 然后,您可以遍历
sorted
并通过查看元组中的第一个元素轻松确定是扭矩还是力 。
sorted.forEach(function (tuple) {
console.log(tuple[0].type, tuple[1]);
});
//torque 2
//force 5
//torque 6
//force 8
The answer is Yes , 答案是可以 ,
But you have to identify each object before you access their properties. 但是,您必须先识别每个对象,然后才能访问它们的属性。 In your case Both of the objects have a common property called lengthfromorigin which can be used to sort them properly.
在您的情况下,这两个对象都有一个共同的属性,称为lengthfromorigin ,可用于对它们进行正确排序。
To identify each object you can use a property like ID or Name . 要标识每个对象,可以使用ID或Name之类的属性。
if(Mydata[i].Name = 'torque'){
//your code goes here
}
else if(Mydata[i].Name = 'force'){
//your code goes here
}
Hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.