admin管理员组

文章数量:1122832

I am working with wp-rest api and i have this json structure to work with:

[
  {
    "id": 11,
    "title": {
      "rendered": "Test-Font"
    },
    "acf": {
      "schrift": [
        {
          "zeichen": "A",
          "anzahl": "23"
        },
        {
          "zeichen": "B",
          "anzahl": "46"
        },
        {
          "zeichen": "C",
          "anzahl": "42"
        },
        {
          "zeichen": "D",
          "anzahl": "49"
        },
        {
          "zeichen": "E",
          "anzahl": "31"
        },

…


 {
    "id": 12,
    "title": {
      "rendered": "Test-Font2"
    },
    "acf": {
      "schrift": [
        {
          "zeichen": "A",
          "anzahl": "20"
        },
        {
          "zeichen": "B",
          "anzahl": "12"
        },

…

i want to load this like this:

jQuery(function ($) {
$.ajax({
    url: 'http://localhost/wordpress-dev/mi/wp-json/wp/v2/schriften/',
    type: "GET",
    dataType: "json",
    success: function (data) {
        data.forEach(function (element) {

    }
});
});

I want to achieve this structure, like an associative array:

[11]
  [A]:23
  [B]:46
  [C]:42
…
 [12]
  [A]:20

how could this be done? can somebody push me in the right direction? Thanks alot!

I am working with wp-rest api and i have this json structure to work with:

[
  {
    "id": 11,
    "title": {
      "rendered": "Test-Font"
    },
    "acf": {
      "schrift": [
        {
          "zeichen": "A",
          "anzahl": "23"
        },
        {
          "zeichen": "B",
          "anzahl": "46"
        },
        {
          "zeichen": "C",
          "anzahl": "42"
        },
        {
          "zeichen": "D",
          "anzahl": "49"
        },
        {
          "zeichen": "E",
          "anzahl": "31"
        },

…


 {
    "id": 12,
    "title": {
      "rendered": "Test-Font2"
    },
    "acf": {
      "schrift": [
        {
          "zeichen": "A",
          "anzahl": "20"
        },
        {
          "zeichen": "B",
          "anzahl": "12"
        },

…

i want to load this like this:

jQuery(function ($) {
$.ajax({
    url: 'http://localhost/wordpress-dev/mi/wp-json/wp/v2/schriften/',
    type: "GET",
    dataType: "json",
    success: function (data) {
        data.forEach(function (element) {

    }
});
});

I want to achieve this structure, like an associative array:

[11]
  [A]:23
  [B]:46
  [C]:42
…
 [12]
  [A]:20

how could this be done? can somebody push me in the right direction? Thanks alot!

Share Improve this question asked Apr 8, 2016 at 19:31 buckdannybuckdanny 1258 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Take a look at this: https://jsfiddle.net/9j96jbxn/1/

Replace your data.forEach(function (element) {...} with a nested for loop. This should get you close to what you are looking for.

This is more about JavaScript than the WordPress REST API, but it's kind of fun to do and a good example of Array.reduce:

var data2 = data.reduce(function(list, item) {
  var schrift = item.acf.schrift.reduce(function(obj, n) {
    obj[n.zeichen] = n.anzahl;
    return obj;
  }, new Object());
  list[item.id] = schrift;
  return list;
}, new Object());

console.log(JSON.stringify(data2, null, 2))

That and a less verbose ES6 example are in this fiddle: https://jsfiddle.net/joemaller/dga39bse/4/

The result looks like this (as JSON):

{
  "11": {
    "A": "23",
    "B": "46",
    "C": "42",
    "D": "49",
    "E": "31"
  },
  "12": {
    "A": "20",
    "B": "12"
  }
}

Note JavaScript makes it somewhat easier to loop arrays than object keys. Flattening into an array of objects with an id key might result in simpler loops later on:

[
  {
    _id: 11,
    A: 23,
    B: 46
  }, {
    _id: 12
    A: 20
  }
]

本文标签: Restructure Objects coming from RestApi