【发布时间】:2021-03-29 23:23:18
【问题描述】:
我有一组对象要重新组织,但我不知道该怎么做,我尝试过使用 push 和 map to,但没有成功。这是当前的输出,低于我希望数据变成的输出。
0: {xSize: "4", xPrice: 1855}
1: {xSize: "4.5", xPrice: 1819}
2: {xSize: "5", xPrice: 2021}
3: {xSize: "5.5", xPrice: 2200}
4: {xSize: "6", xPrice: 1891}
5: {xSize: "6.5", xPrice: 1929}
6: {xSize: "7", xPrice: 1975}
7: {xSize: "7.5", xPrice: 1850}
8: {xSize: "8", xPrice: 1890}
9: {xSize: "8.5", xPrice: 2175}
10: {xSize: "9", xPrice: 2163}
11: {xSize: "9.5", xPrice: 2000}
12: {xSize: "10", xPrice: 2000}
13: {xSize: "10.5", xPrice: 2184}
14: {xSize: "11", xPrice: 2100}
15: {xSize: "11.5", xPrice: 2098}
16: {xSize: "12", xPrice: 2190}
17: {xSize: "12.5", xPrice: 2700}
18: {xSize: "13", xPrice: 2255}
19: {xSize: "14", xPrice: 1950}
20: {xSize: "15", xPrice: 3500}
21: {gSize: 4, gPrice: 1680}
22: {gSize: 4.5, gPrice: 1790}
23: {gSize: 5, gPrice: 1970}
24: {gSize: 5.5, gPrice: 2100}
25: {gSize: 6, gPrice: 1889}
26: {gSize: 6.5, gPrice: 1950}
27: {gSize: 7, gPrice: 1905}
28: {gSize: 7.5, gPrice: 1800}
29: {gSize: 8, gPrice: 1895}
30: {gSize: 8.5, gPrice: 2091}
31: {gSize: 9, gPrice: 2195}
32: {gSize: 9.5, gPrice: 2110}
33: {gSize: 10, gPrice: 2295}
我希望像这样重新格式化对象数组。
0: { xSize: 4,
xPrice: 1855,
gSize: 4,
gPrice: 1680},
1: { xSize: 4.5,
xPrice: 1819,
gSize: 4.5,
gPrice: 1790},
2: etc. etc.
这是下面脚本的一部分,它接收来自前端客户端的输入,然后搜索 2 个外部 api,并将信息重新加工成一个新数组。
function searchBrow(stockXurl) {
return new Promise((resolve, reject) => {
request(
{
url:
"https://stockx.com/api/products/" +
stockXurl +
"?includes=market¤cy=USD",
method: "GET",
headers: stockxGETHeaders,
},
function (err, res, body) {
let json = JSON.parse(body);
let shoeId = json.Product.title;
let sku = json.Product.styleId;
let release = json.Product.releaseDate;
let parsedSizes = json.Product.children;
let stockx = [];
for (let a = 0; a < Object.keys(parsedSizes).length; a++) {
let item = parsedSizes[Object.keys(parsedSizes)[a]];
if (item.shoeSize.indexOf(".") == -1) {
item.shoeSize = item.shoeSize.toString();
}
let sizing = item.shoeSize;
let pricing = item.market.lowestAsk;
stockx.push({
xSize: sizing,
xPrice: pricing,
});
}
console.log(stockx);
sendSkuGoat(sku, stockx).then((goatJoin) => {
resolve(goatJoin);
});
}
);
});
}
function getGoatAsk(slug, photo, stockx) {
return new Promise((resolve, reject) => {
request(
{
method: "GET",
url:
"https://www.goat.com/api/v1/product_variants?productTemplateId=" +
slug,
headers: goatGETHeaders,
},
function (err, res, body) {
json = JSON.parse(body);
goatSizes = json[0].size;
goatAsk = json[0].lowestPriceCents.amount;
for (let j of json) {
if (
j.shoeCondition === "new_no_defects" &&
j.boxCondition === "good_condition" &&
j.size >= 4
) {
const size = j.size;
const amount = j.lowestPriceCents.amount * 0.01;
stockx.push({ gSize: size, gPrice: amount });
}
}
return resolve(stockx);
}
);
});
}
【问题讨论】:
标签: javascript node.js arrays