实现 data.sort 按标题对 json 进行排序,不断收到“未定义”错误。不知道我做错了啥

Posted

技术标签:

【中文标题】实现 data.sort 按标题对 json 进行排序,不断收到“未定义”错误。不知道我做错了啥【英文标题】:Implementing data.sort to sort json by title, keep getting "undefined" error. Not sure what I am doing wrong实现 data.sort 按标题对 json 进行排序,不断收到“未定义”错误。不知道我做错了什么 【发布时间】:2020-06-15 07:33:31 【问题描述】:

我正在尝试按标题对一些 JSON 数据进行排序。

这是 JSON 数据:

[
  
    "products": null
  ,
  
    "products": 
      "1": 
        "id": "dLJgXNo",
        "title": "BBBBB",
        "image": 
          "url": "https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg",
          "path": "product_images/ATAzyRGXvIn8I4vTP275Xr3hHowmxhdNMdMDg3Dz.jpeg"
        ,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 1,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 13
      ,
      "2": 
        "id": "kVq0nNc",
        "title": "LLLLL",
        "image": 
          "url": "https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg",
          "path": "product_images/C8FFOV9u14AbEALsc6j8YOx68n2ddIxgCBI8T00b.gif"
        ,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 99.99,
        "currency": "USD",
        "stock_warning": 0,
        "type": "service",
        "stock": 9223372036854776000
      ,
      "3": 
        "id": "VVPanJG",
        "title": "QQQQQ",
        "image": 
          "url": "https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg",
          "path": "product_images/ag7cdutbMnSiFknvXi3lKuCHNgCjyo5MEdEic9rS.png"
        ,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 999.99,
        "currency": "USD",
        "stock_warning": 0,
        "type": "service",
        "stock": 9223372036854776000
      ,
      "4": 
        "id": "RE3EcTr",
        "title": "AAAAA",
        "image": 
          "url": "https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg",
          "path": "product_images/Wtu1jGF2FWnxRR00yfvKaKF5Z8jnHrWhVwWUhJzF.png"
        ,
        "quantity": 
          "min": 1,
          "max": 100
        ,
        "price": 0.5,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 87
      ,
      "5": 
        "id": "oeB4ekE",
        "title": "NNNNN",
        "image": 
          "url": "https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg",
          "path": "product_images/2X0adruZr4ObIgLujM8LbLzOYiEZxYBuif6K8NnR.jpeg"
        ,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 3,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 14
      ,
      "6": 
        "id": "17SHtAg",
        "title": "MMMMMM",
        "image": null,
        "quantity": 
          "min": 1,
          "max": 1000000
        ,
        "price": 1,
        "currency": "USD",
        "stock_warning": 1,
        "type": "account",
        "stock": 5
      ,
      "7": 
        "id": "815DuOe",
        "title": "PPPPP",
        "image": null,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 3,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 15
      ,
      "8": 
        "id": "9kRifdb",
        "title": "TTTTT",
        "image": null,
        "quantity": 
          "min": 1,
          "max": 1
        ,
        "price": 3,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 13
      ,
      "9": 
        "id": "BoVtpDQ",
        "title": "ZZZZZ",
        "image": null,
        "quantity": 
          "min": 1,
          "max": 1000000
        ,
        "price": 3,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 1206
      ,
      "10": 
        "id": "Mb0tcZ6",
        "title": "GGGGG",
        "image": null,
        "quantity": 
          "min": 1,
          "max": 1000
        ,
        "price": 2,
        "currency": "USD",
        "stock_warning": 0,
        "type": "account",
        "stock": 998
      
    
  
]

我正在尝试使用非常方便的 data.sort 函数。但只收到未定义的“localeCompare”错误。这是我的代码:

var data = ["products":null,"products":"1":"id":"dLJgXNo","title":"BBBBB","image":"url":"https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg","path":"product_images/ATAzyRGXvIn8I4vTP275Xr3hHowmxhdNMdMDg3Dz.jpeg","quantity":"min":1,"max":1,"price":1,"currency":"USD","stock_warning":0,"type":"account","stock":13,"2":"id":"kVq0nNc","title":"LLLLL","image":"url":"https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg","path":"product_images/C8FFOV9u14AbEALsc6j8YOx68n2ddIxgCBI8T00b.gif","quantity":"min":1,"max":1,"price":99.99,"currency":"USD","stock_warning":0,"type":"service","stock":9223372036854776000,"3":"id":"VVPanJG","title":"QQQQQ","image":"url":"https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg","path":"product_images/ag7cdutbMnSiFknvXi3lKuCHNgCjyo5MEdEic9rS.png","quantity":"min":1,"max":1,"price":999.99,"currency":"USD","stock_warning":0,"type":"service","stock":9223372036854776000,"4":"id":"RE3EcTr","title":"AAAAA","image":"url":"https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg","path":"product_images/Wtu1jGF2FWnxRR00yfvKaKF5Z8jnHrWhVwWUhJzF.png","quantity":"min":1,"max":100,"price":0.5,"currency":"USD","stock_warning":0,"type":"account","stock":87,"5":"id":"oeB4ekE","title":"NNNNN","image":"url":"https://www.adweek.com/wp-content/uploads/2017/11/google-G-logo-hed-2017.jpg","path":"product_images/2X0adruZr4ObIgLujM8LbLzOYiEZxYBuif6K8NnR.jpeg","quantity":"min":1,"max":1,"price":3,"currency":"USD","stock_warning":0,"type":"account","stock":14,"6":"id":"17SHtAg","title":"MMMMMM","image":null,"quantity":"min":1,"max":1000000,"price":1,"currency":"USD","stock_warning":1,"type":"account","stock":5,"7":"id":"815DuOe","title":"PPPPP","image":null,"quantity":"min":1,"max":1,"price":3,"currency":"USD","stock_warning":0,"type":"account","stock":15,"8":"id":"9kRifdb","title":"TTTTT","image":null,"quantity":"min":1,"max":1,"price":3,"currency":"USD","stock_warning":0,"type":"account","stock":13,"9":"id":"BoVtpDQ","title":"ZZZZZ","image":null,"quantity":"min":1,"max":1000000,"price":3,"currency":"USD","stock_warning":0,"type":"account","stock":1206,"10":"id":"Mb0tcZ6","title":"GGGGG","image":null,"quantity":"min":1,"max":1000,"price":2,"currency":"USD","stock_warning":0,"type":"account","stock":998];
data.sort(function (a, b) 
    return a.products.title.localeCompare(b.products.title);
);
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

当然我必须在这里遗漏一些东西,我是否在某处遗漏了一个循环?也许我没有正确导航 JSON。任何有帮助的东西都非常感谢。谢谢

【问题讨论】:

【参考方案1】:

您的代码a.products.title.localeCompare(b.products.title); 不适用于您的数据结构。请注意,products 是一个带有键/值对的 object。这些值具有title 属性,但products 对象没有,因此a.products.titleb.products.title 都是undefined,尝试执行undefined.localeCompare 将失败。

在我看来,您实际上是想先将产品从products 对象中提取出来,然后再对它们进行排序。试试这个:

const sortedData = data
  .reduce((products, obj) => products.concat(Object.values(obj.products || )), [])
  .sort((a, b) => a.title.localeCompare(b.title));

【讨论】:

感谢雅各布的提示,非常感谢。你真的不能更好地解释它。我不知道你可以减少 json,这对我来说更容易掌握。我对 sortedData 常量感到困惑,但这样做的目的是什么?谢谢 就在那里,所以当您使用document.write 时,您将可以调用JSON.stringify,例如JSON.stringify(sortedData)

以上是关于实现 data.sort 按标题对 json 进行排序,不断收到“未定义”错误。不知道我做错了啥的主要内容,如果未能解决你的问题,请参考以下文章

首先按布尔列对数组进行排序,然后按字符串列排序

按 JSON 数组对 SQL 数据进行分组

按字母顺序排序列表

如何按数值对 json 进行排序?

对 JSON 进行排序或按原始顺序获取 JSON [重复]

按属性对 JSON 数组进行排序的通用方法