萧十郎
如果你想要以下格式的 json[ {'s1':['a','b']}, {'s2':['c','d']}, {'d1':['p','q']}, {'d2':['r','s']}, {'':['']}, {'d2':['r','s']}]然后你可以对单个孩子进行 api 调用,或者发送整个数组。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));
素胚勾勒不出你
检查数据的频率。如果 FrontEnd 上的数据太多,那么最好在块中发送。否则可以在 FE 上积累,然后发送。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)