按键值

时间:2018-01-26 11:02:25

标签: javascript arrays json sorting object

在您将其标记为重复之前 - 我已经完成了这些答案:

Sort JSON by array key value

Sort a JSON array object using Javascript by value

我尝试将代码移到我的数据上,但它并不起作用。我也尝试将每个对象项目推送到数组中,但这并不起作用,因为每个项目中仍然有一个对象。

Here's a bit of the JSON response I get(来自浏览器控制台的屏幕截图)

如您所见,每个项目都有一个索引 - 我需要的是根据索引对响应进行排序。我该怎么做呢?

此回复来自维基百科 - 暂时不说。 Here's the full API call,如果有帮助的话。

编辑:在这里发布一些响应代码:

 "pages": {
      "736": {
        "pageid": 736,
        "ns": 0,
        "title": "Albert Einstein",
        "index": 2,
        "contentmodel": "wikitext",
        "pagelanguage": "en",
        "pagelanguagehtmlcode": "en",
        "pagelanguagedir": "ltr",
        "touched": "2018-01-24T22:40:11Z",
        "lastrevid": 821432412,
        "length": 145560,
        "fullurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
        "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Einstein&action=edit",
        "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
          "width": 38,
          "height": 50
        },
        "pageimage": "Einstein_1921_by_F_Schmutzer_-_restoration.jpg"
      },
      "983": {
        "pageid": 983,
        "ns": 0,
        "title": "Albert Camus",
        "index": 10,
        "contentmodel": "wikitext",
        "pagelanguage": "en",
        "pagelanguagehtmlcode": "en",
        "pagelanguagedir": "ltr",
        "touched": "2018-01-26T09:34:35Z",
        "lastrevid": 822358239,
        "length": 53639,
        "fullurl": "https://en.wikipedia.org/wiki/Albert_Camus",
        "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Camus&action=edit",
        "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Camus",
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg/42px-Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg",
          "width": 42,
          "height": 50
        },
        "pageimage": "Albert_Camus,_gagnant_de_prix_Nobel,_portrait_en_buste,_posé_au_bureau,_faisant_face_à_gauche,_cigarette_de_tabagisme.jpg"
      },
      "46721": {
        "pageid": 46721,
        "ns": 0,
        "title": "Edward VII",
        "index": 9,
        "contentmodel": "wikitext",
        "pagelanguage": "en",
        "pagelanguagehtmlcode": "en",
        "pagelanguagedir": "ltr",
        "touched": "2018-01-26T02:00:27Z",
        "lastrevid": 821663314,
        "length": 81925,
        "fullurl": "https://en.wikipedia.org/wiki/Edward_VII",
        "editurl": "https://en.wikipedia.org/w/index.php?title=Edward_VII&action=edit",
        "canonicalurl": "https://en.wikipedia.org/wiki/Edward_VII",
        "thumbnail": {
          "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Edward_VII_in_coronation_robes.jpg/35px-Edward_VII_in_coronation_robes.jpg",
          "width": 35,
          "height": 50
        },
        "pageimage": "Edward_VII_in_coronation_robes.jpg"
      }

3 个答案:

答案 0 :(得分:2)

您的回复似乎是一个无法排序的对象。您首先要将其转换为数组

const res = //... your response
const array = Object.keys(res).map(key => res[key]);

现在,您可以使用Array.sort功能按index对项目进行排序:

array.sort((itemA, itemB) =>  itemA - itemB)

请注意sort函数会直接改变数组。

请参阅:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

答案 1 :(得分:1)

这是我的方法,只需转换为数组,然后返回JSON对象。

更新:我所做的是创建每个page的数组,然后使用数组的Array.prototype.sort()函数比较每个page的索引,然后使用a for-loop我使用排序数组中的对象重新创建了JSON对象。

var json = {
  "pages": {
    "983": {
      "pageid": 983,
      "ns": 0,
      "title": "Albert Camus",
      "index": 10,
      "contentmodel": "wikitext",
      "pagelanguage": "en",
      "pagelanguagehtmlcode": "en",
      "pagelanguagedir": "ltr",
      "touched": "2018-01-26T09:34:35Z",
      "lastrevid": 822358239,
      "length": 53639,
      "fullurl": "https://en.wikipedia.org/wiki/Albert_Camus",
      "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Camus&action=edit",
      "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Camus",
      "thumbnail": {
        "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg/42px-Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg",
        "width": 42,
        "height": 50
      },
      "pageimage": "Albert_Camus,_gagnant_de_prix_Nobel,_portrait_en_buste,_posé_au_bureau,_faisant_face_à_gauche,_cigarette_de_tabagisme.jpg"
    },
    "736": {
      "pageid": 736,
      "ns": 0,
      "title": "Albert Einstein",
      "index": 2,
      "contentmodel": "wikitext",
      "pagelanguage": "en",
      "pagelanguagehtmlcode": "en",
      "pagelanguagedir": "ltr",
      "touched": "2018-01-24T22:40:11Z",
      "lastrevid": 821432412,
      "length": 145560,
      "fullurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
      "editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Einstein&action=edit",
      "canonicalurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
      "thumbnail": {
        "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
        "width": 38,
        "height": 50
      },
      "pageimage": "Einstein_1921_by_F_Schmutzer_-_restoration.jpg"
    }
  }
};

var array = [];
for (key in json.pages) {
  array.push(json.pages[key]);
}

array.sort(function(a, b) {
  return a.index - b.index;
});

json = {
  "pages": {}
};

for (var i = 0; i < array.length; i++) {
  json.pages[array[i]['pageid']] = array[i];
}

console.log(json);

希望有所帮助!

答案 2 :(得分:1)

我创建了这些函数来订购json,以便在其中包含另一个json时也可以使用。

function orderJson(json) {
    let ordered = this.jsonToSortedArray(json);
    let result = this.arrayToStringJson(ordered);
    return JSON.parse(result);
}

function jsonToSortedArray(json) {
    let ordered = [];
    for (let i in json) {
        let value;
        if (json[i] instanceof Object) {
            value = jsonToSortedArray(json[i]);
        } else {
            value = json[i];
        }
        ordered.push([i, value]);
    }
    return ordered.sort();
}

function arrayToStringJson(ordered) {
    let result = '{'
    for (let i = 0; i < ordered.length; i++) {
        const key = '"' + ordered[i][0] + '"';
        let value;
        if (ordered[i][1] instanceof Array) {
            value = ':' + this.arrayToStringJson(ordered[i][1]) + ',';
        } else {
            value = ':"' + ordered[i][1] + '",';
        }
        result += key + value;
    }
    result = result.substring(0, result.length - 1);
    return result + '}';
}

如果要将json作为字符串,请不要使用函数JSON.parse