第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何將 JSON 對象添加到 API 調(diào)用

如何將 JSON 對象添加到 API 調(diào)用

千萬里不及你 2022-06-09 10:34:13
我在一個(gè)動(dòng)態(tài)創(chuàng)建的數(shù)組中有 x 個(gè)對象。我想通過 API 發(fā)送來單獨(dú)保存這些對象。問題是可以有 x 個(gè)對象。編寫一個(gè)循環(huán)來讀取屬性名稱及其屬性值并點(diǎn)擊 api,然后讀取第二個(gè)屬性及其值并點(diǎn)擊 api 的最佳方法是什么。所以基本上一次api只會保存一個(gè)對象。IE{        "attributeName": "s1",        "attributeValues": [          "a",          "b"        ]      }我的 JSON 對象如下所示:[  {    "attributeName": "s1",    "attributeValues": [      "a",      "b"    ]  },  {    "attributeName": "s2",    "attributeValues": [      "c",      "d"    ]  },  {    "attributeName": "d1",    "attributeValues": [      "p",      "q"    ]  },  {    "attributeName": "d2",    "attributeValues": [      "r",      "s"    ]  },  {    "attributeName": "",    "attributeValues": [      ""    ]  },  {    "attributeName": "d2",    "attributeValues": [      "r",      "s"    ]  }]我想選擇屬性名稱及其值,然后將其發(fā)送到 API?;蛘哒f,我將整個(gè) JSON 保存到 API,那么如果我事先不知道屬性名稱,我該如何過濾掉單個(gè)對象。還有什么其他方法可以獲取屬性名稱列表。
查看完整描述

2 回答

?
蕭十郎

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊

如果你想要以下格式的 json


[

  {'s1':['a','b']},

  {'s2':['c','d']},

  {'d1':['p','q']},

  {'d2':['r','s']},

  {'':['']},

  {'d2':['r','s']}

]

然后你可以對單個(gè)孩子進(jìn)行 api 調(diào)用,或者發(fā)送整個(gè)數(shù)組。


var arr = [

  {

    "attributeName": "s1",

    "attributeValues": [

      "a",

      "b"

    ]

  },

  {

    "attributeName": "s2",

    "attributeValues": [

      "c",

      "d"

    ]

  },

  {

    "attributeName": "d1",

    "attributeValues": [

      "p",

      "q"

    ]

  },

  {

    "attributeName": "d2",

    "attributeValues": [

      "r",

      "s"

    ]

  },

  {

    "attributeName": "",

    "attributeValues": [

      ""

    ]

  },

  {

    "attributeName": "d2",

    "attributeValues": [

      "r",

      "s"

    ]

  }

];


var obj = arr.map((o1) => {

  var o = {};

  o[o1.attributeName] = o1.attributeValues;

  return o;

});


console.log(JSON.stringify(obj));


查看完整回答
反對 回復(fù) 2022-06-09
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

檢查數(shù)據(jù)的頻率。如果 FrontEnd 上的數(shù)據(jù)太多,那么最好在塊中發(fā)送。否則可以在 FE 上積累,然后發(fā)送。


async function sendAll(data) {

  let results = [];

  for (let index = 0; index < data.length; index++) {

    const result = await axios.post('url', data[index].attributeValues);

    results.push(result);

  }

}

帶有假模擬 api 的示例。


// function* dataLake(data) {

//   for (let item of data) yield item;

// }

const getDataFake = data => {

  return new Promise(r => {

    setTimeout(() => {

      r(data);

    }, 100);

  });

};


async function sendAll(data) {

  let results = [];

  for (let index = 0; index < data.length; index++) {

    const result = await getDataFake(data[index].attributeValues);

    results.push(result);

  }

  return results;

}


const data = [

  {

    attributeName: "s1",

    attributeValues: ["a", "b"]

  },

  {

    attributeName: "s2",

    attributeValues: ["c", "d"]

  },

  {

    attributeName: "d1",

    attributeValues: ["p", "q"]

  },

  {

    attributeName: "d2",

    attributeValues: ["r", "s"]

  },

  {

    attributeName: "",

    attributeValues: [""]

  },

  {

    attributeName: "d2",

    attributeValues: ["r", "s"]

  }

];

sendAll(data).then(console.log)


查看完整回答
反對 回復(fù) 2022-06-09
  • 2 回答
  • 0 關(guān)注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號