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 badges2 Answers
Reset to default 0Take 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
版权声明:本文标题:Restructure Objects coming from Rest-Api 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299104a1930376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论