Javascript 如何通过index node.js组织对象数组
我有一个数组的对象,我想重新组织,但我不知道如何做,我尝试使用推和映射到没有运气。这是当前的输出,低于此,我希望数据转换为Javascript 如何通过index node.js组织对象数组,javascript,node.js,arrays,Javascript,Node.js,Arrays,我有一个数组的对象,我想重新组织,但我不知道如何做,我尝试使用推和映射到没有运气。这是当前的输出,低于此,我希望数据转换为 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"
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);
}
);
});
}
函数searchBrow(stockXurl){
返回新承诺((解决、拒绝)=>{
请求(
{
网址:
"https://stockx.com/api/products/" +
斯托克塞尔+
“?包括=市场和货币=美元”,
方法:“获取”,
标题:stockxGETHeaders,
},
功能(错误、恢复、正文){
让json=json.parse(body);
让shoeId=json.Product.title;
让sku=json.Product.styleId;
让release=json.Product.releaseDate;
让parsedSizes=json.Product.children;
设stockx=[];
for(设a=0;a{
解决(goatJoin);
});
}
);
});
}
函数getGoatAsk(slug、photo、stockx){
返回新承诺((解决、拒绝)=>{
请求(
{
方法:“获取”,
网址:
"https://www.goat.com/api/v1/product_variants?productTemplateId=" +
鼻涕虫
标题:goatGETHeaders,
},
功能(错误、恢复、正文){
json=json.parse(body);
goatSizes=json[0]。大小;
goatAsk=json[0].LowestPriceRecents.amount;
for(让j代表json){
如果(
j、 shoeCondition==“新的无缺陷”&&
j、 boxCondition==“良好的条件”&&
j、 大小>=4
) {
常数大小=j.大小;
常数金额=j.最低价格。金额*0.01;
stockx.push({gSize:size,gPrice:amount});
}
}
返回解析(stockx);
}
);
});
}
大家好,欢迎来到StackOverflow
对于您的下一个问题,有一个提示:显示您的代码。如果我们不知道你在做什么,我们也帮不了你
有几种方法可以解决你的问题。我建议Array.prototype.reduce:
const allInOneArray = yourArray.reduce((reorderedArray, object) => {
//shortcut to our sizes
const xSize = object.xSize;
const gSize = object.gSize;
const xPrice = object.xPrice;
const gPrice = object.gPrice;
// declare the foundObject (important for scoping)
let foundObject;
// if our object has xSize => search for an object with the same gSize
if (typeof xSize === 'number') {
foundObject = reorderedArray.find(checkingObject => checkingObject.gSize === xSize);
// we found one and add the other size
if (foundObject) {
foundObject.gSize = xSize;
foundObject.gPrice = xPrice;
}
} else {
// if we dont have the xSize given, check the gSize
// if our object has gSize => search for an object with the same xSize
if (typeof gSize === 'number') {
foundObject = reorderedArray.find(checkingObject => checkingObject.xSize === gSize);
// we found one and add the other size
if (foundObject) {
foundObject.xSize = gSize;
foundObject.xPrice = gPrice;
}
}
}
// if we found no object
if (!foundObject) {
// we will create one (by copy the original object)
reorderedArray.push({ ...object });
}
// return the reorderedArray to the next step
return reorderedArray;
}, []);
const组=[{
xSize:4,
价格:1855
},
{
xSize:4.5,
价格:1819
},
{
xSize:5,
价格:2021
},
{
xSize:5.5,
价格:2200
},
{
xSize:6,
价格:1891
},
{
xSize:6.5,
价格:1929
},
{
xSize:7,
价格:1975年
},
{
xSize:7.5,
价格:1850
},
{
xSize:0,
价格:1890
},
{
xSize:8.5,
价格:2175
},
{
xSize:9,
价格:2163
},
{
xSize:9.5,
价格:2000
},
{
xSize:10,
价格:2000
},
{
xSize:10.5,
价格:2184
},
{
xSize:11,
价格:2100
},
{
xSize:11.5,
价格:2098
},
{
xSize:12,
价格:2190
},
{
xSize:12.5,
价格:2700
},
{
xSize:13,
价格:2255
},
{
xSize:14,
价格:1950
},
{
xSize:15,
价格:3500
},
{
gSize:4,
价格:1680
},
{
gSize:4.5,
价格:1790
},
{
gSize:5,
价格:1970年
},
{
gSize:5.5,
价格:2100
},
{
gSize:6,
价格:1889
},
{
gSize:6.5,
价格:1950
},
{
gSize:7,
价格:1905
},
{
gSize:7.5,
价格:1800
},
{
gSize:0,
价格:1895
},
{
gSize:8.5,
价格:2091
},
{
gSize:9,
价格:2195
},
{
gSize:9.5,
价格:2110
},
{
gSize:10,
价格:2295
}
]
var groupedUp=[];
groups.forEach(x=>{
if(x.hasOwnProperty(“xSize”)){
让group=groupedUp.find(y=>y.gSize==x.xSize)
国际单项体育联合会(小组){
group.xSize=x.xSize;
group.xPrice=x.xPrice;
}否则{
分组推送(x);
}
}
如果(x.hasOwnProperty(“gSize”)){
让group=groupedUp.find(y=>y.xSize==x.gSize)
国际单项体育联合会(小组){
group.gSize=x.gSize;
组.gPrice=x.gPrice;
}否则{
分组推送(x);
}
}
})
console.log(groupedUp)代码>你好!感谢您的热情欢迎,很抱歉,我没有包含代码,我现在已经更新了,它需要前端客户端的输入,因此无法真正运行,我只包含了对该阵列非常重要的两个部分,显示的第一个功能是收集阵列stockx的数据并将其推入其中,最后一个函数也是这样做的,将数据推送到stockx。我无法让您的代码正常工作,不幸的是,它只会输出相同(42)的数组大小。感谢您的关注和帮助,非常感谢!如果xSIze或gSize为0,则代码将失败。使用typeof或hasOwnProperty检查是否存在值。请不要在2021年使用var(如果没有必要;)@凯莱曼,更新。。。谢谢你@阿纳斯布兹拉马