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));

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)
添加回答
舉報(bào)