[英]how can I sort array by other array
There are 2 json arrays below. 下面有2个json数组。
arrayA = [
{attr1: "text", attr2: true, field: "format4"},
{attr1: "text", attr2: true, field: "format2"},
{attr1: "text", attr2: true, field: "format1"},
{attr1: "text", attr2: true, field: "format3"}];
arrayB = [
{ name: 'format1', type: 'text' },
{ name: 'format2', type: 'text' },
{ name: 'format3', type: 'text' },
{ name: 'format4', type: 'text' }
];
I want to sort array B name by array A's field my goal is like this 我想按数组A的字段对数组B的名称进行排序,我的目标是这样的
arrayB = [
{ name: 'format4', type: 'text' },
{ name: 'format2', type: 'text' },
{ name: 'format1', type: 'text' },
{ name: 'format3', type: 'text' }
];
I thought like this but this is not my goal. 我以为这样,但这不是我的目标。
arrayB = arrayA.map((a) => {
return arrayB.filter((b) => {
return a.field === b.name
});
});
please give me a advise for accomplish my goal. 请给我建议以实现我的目标。
If you are comfortable using ES6 features, you can use Array.findIndex
to get the index and return difference 如果您习惯使用ES6功能,则可以使用
Array.findIndex
获取索引并返回差值
var arrayA = [{attr1: "text", attr2: true, field: "format4"}, {attr1: "text", attr2: true, field: "format2"}, {attr1: "text", attr2: true, field: "format1"}, {attr1: "text", attr2: true, field: "format3"}]; var arrayB = [ { name: 'format1', type: 'text' }, { name: 'format2', type: 'text' }, { name: 'format3', type: 'text' }, { name: 'format4', type: 'text' }]; function getIndexInArrayA(name) { return arrayA.findIndex(function(obj){ return obj.field === name}) } arrayB.sort(function(a, b) { return getIndexInArrayA(a.name) - getIndexInArrayA(b.name); }); console.log(arrayB)
Loop over array and create a map that holds name and index. 遍历数组并创建一个保存名称和索引的映射。 This way you do not need to loop over array to get index.
这样,您无需遍历数组即可获取索引。
Note: This is more preferred as it involves less iterations. 注意:这是更可取的,因为它涉及较少的迭代。 retrieving data from object is faster than getting index form array.
从对象检索数据比获取索引表单数组要快。
var arrayA = [{attr1: "text", attr2: true, field: "format4"}, {attr1: "text", attr2: true, field: "format2"}, {attr1: "text", attr2: true, field: "format1"}, {attr1: "text", attr2: true, field: "format3"}]; var arrayB = [ { name: 'format1', type: 'text' }, { name: 'format2', type: 'text' }, { name: 'format3', type: 'text' }, { name: 'format4', type: 'text' }]; var indexNameMap = arrayA.reduce(function(acc, obj, i) { acc[obj.field] = i; return acc; }, {}) arrayB.sort(function(a, b) { return indexNameMap[a.name] - indexNameMap[b.name]; }); console.log(arrayB)
You can try following 您可以尝试关注
var arrayA = [{attr1: "text", attr2: true, field: "format4"}, {attr1: "text", attr2: true, field: "format2"}, {attr1: "text", attr2: true, field: "format1"}, {attr1: "text", attr2: true, field: "format3"}]; var arrayB = [ { name: 'format1', type: 'text' }, { name: 'format2', type: 'text' }, { name: 'format3', type: 'text' }, { name: 'format4', type: 'text' }]; // Create a temporary array of fields of array A var tempArr = arrayA.map(function(item) { return item.field; }); // Create custom sort function to sort based on temp Arr arrayB.sort(function(a, b) { return tempArr.indexOf(a.name) - tempArr.indexOf(b.name); }) console.log(arrayB);
另一个解决方案:
const res = _.map(arrayA, a => _.find(arrayB, { name: a.field }));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.