在javascript中对嵌套的对象数组进行排序

时间:2021-05-14 09:53:22

标签: javascript arrays

我正在尝试按属性“title”对给定数组进行排序

我该如何排序? 请帮助我如何解决它。谢谢。

data = [
{
  _index: 'yo',
  _type: 'sample1',
  _id: '201',
  _score: null,
  _source: {
    type: 'digi',
    title: 'The Happy',
    description: 'san',
    image: 'good.jpg',
  },
  sort: [ sam ]
},
{
    _index: 'ya',
    _type: 'sample2',
    _id: '201',
    _score: null,
    _source: {
      type: 'sam',
      title: 'The lonely',
      description: 'san',
      image: 'y.jpg',
    },
    sort: [ sam ]
  },
  {
    _index: 'yi',
    _type: 'sample3',
    _id: '201',
    _score: null,
    _source: {
      type: 'sam',
      title: 'The Angry',
      description: 'sam',
      image: 'x.jpg',
    },
    sort: [ sam ]
  }
]

谢谢

这就是我正在尝试的

data.sort(function (x, y) {
    let a = x._source.title.toUpperCase(),
        b = y._source.title.toUpperCase();
    return a == b ? 0 : a > b ? 1 : -1;
});

console.console(数据);

但我无法访问标题。

0 个答案:

没有答案
相关问题