根据另一个数组中的值对数组进行排序-Javascript

时间:2019-04-17 13:40:05

标签: javascript arrays sorting

我有以下对象数组:

document.addEventListener("DOMContentLoaded", function() {
  var chart1 = new Highcharts.Chart({
    chart: {
      type: 'pie',
      renderTo: 'container'
    },
    title: {
      verticalAlign: 'middle',
      floating: true,
      text: 'CENTERED<br>TEXT'
    },
    plotOptions: {
      pie: {
        innerSize: '50%',
        dataLabels: {
          align: 'right',
          distance: 10,
          connectorWidth: 0
        }
      }
    },

    series: [{
      data: [
        ['Firefox', 44.2],
        ['IE7', 26.6],
        ['IE6', 20],
        ['Chrome', 3.1],
        ['Other', 5.4]
      ]
    }]
  });
});

如何根据arr1标题的值对arr2进行排序?

所需的输出为:

arr1 =  [{chart: {field: "test", title: "ABC", type: 0}}, {chart: {field: "test", title: "123", type: 1}}, {chart: {field: "test", title: "XYZ", type: 2}}]

arr2 =   [{Name: "XYZ"}, {Name: "ABC"}, {Name: "123"}]

5 个答案:

答案 0 :(得分:4)

您可以使用内置的排序方法并使用findIndex()。并且camera.rotateX(Math.PI / 2) 基于该索引。我使用了一个公用函数sort()来获取两个参数的索引。

func

答案 1 :(得分:2)

不是对arr2进行排序,而是根据arr1中的值为其分配新值:

DropDownList

答案 2 :(得分:1)

您可以将Map用于索引。

var array1 = [{ chart: { field: "test", title: "ABC", type: 0 } }, { chart: { field: "test", title: "123", type: 1 } }, { chart: { field: "test", title: "XYZ", type: 2 } }],
    array2 = [{ Name: "XYZ" }, { Name: "ABC" }, { Name: "123" }],
    indices = new Map(array1.map(({ chart: { title }}, i) => [title, i]));

array2.sort(({ Name: a }, { Name: b }) => indices.get(a) - indices.get(b));

console.log(array2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:-1)

只需从arr1创建一个新的arr2数组。

return CGSize(width: (collectionView.frame.width / 2) - 10, height: 220)

谢谢。

答案 4 :(得分:-1)

尝试以下解决方案

TouchableOpacity

相关问题