【问题标题】:How to organize Array of objects via index node.js如何通过索引 node.js 组织对象数组
【发布时间】: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&currency=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


    【解决方案1】:

    const groups = [{
        xSize: 4,
        xPrice: 1855
      },
      {
        xSize: 4.5,
        xPrice: 1819
      },
      {
        xSize: 5,
        xPrice: 2021
      },
      {
        xSize: 5.5,
        xPrice: 2200
      },
      {
        xSize: 6,
        xPrice: 1891
      },
      {
        xSize: 6.5,
        xPrice: 1929
      },
      {
        xSize: 7,
        xPrice: 1975
      },
      {
        xSize: 7.5,
        xPrice: 1850
      },
      {
        xSize: 0,
        xPrice: 1890
      },
      {
        xSize: 8.5,
        xPrice: 2175
      },
      {
        xSize: 9,
        xPrice: 2163
      },
      {
        xSize: 9.5,
        xPrice: 2000
      },
      {
        xSize: 10,
        xPrice: 2000
      },
      {
        xSize: 10.5,
        xPrice: 2184
      },
      {
        xSize: 11,
        xPrice: 2100
      },
      {
        xSize: 11.5,
        xPrice: 2098
      },
      {
        xSize: 12,
        xPrice: 2190
      },
      {
        xSize: 12.5,
        xPrice: 2700
      },
      {
        xSize: 13,
        xPrice: 2255
      },
      {
        xSize: 14,
        xPrice: 1950
      },
      {
        xSize: 15,
        xPrice: 3500
      },
      {
        gSize: 4,
        gPrice: 1680
      },
      {
        gSize: 4.5,
        gPrice: 1790
      },
      {
        gSize: 5,
        gPrice: 1970
      },
      {
        gSize: 5.5,
        gPrice: 2100
      },
      {
        gSize: 6,
        gPrice: 1889
      },
      {
        gSize: 6.5,
        gPrice: 1950
      },
      {
        gSize: 7,
        gPrice: 1905
      },
      {
        gSize: 7.5,
        gPrice: 1800
      },
      {
        gSize: 0,
        gPrice: 1895
      },
      {
        gSize: 8.5,
        gPrice: 2091
      },
      {
        gSize: 9,
        gPrice: 2195
      },
      {
        gSize: 9.5,
        gPrice: 2110
      },
      {
        gSize: 10,
        gPrice: 2295
      }
    ]
    var groupedUp = [];
    groups.forEach(x => {
      if (x.hasOwnProperty("xSize")) {
        let group = groupedUp.find(y => y.gSize == x.xSize)
        if (group) {
          group.xSize = x.xSize;
          group.xPrice = x.xPrice;
        } else {
          groupedUp.push(x);
        }
    
      }
      if (x.hasOwnProperty("gSize")) {
        let group = groupedUp.find(y => y.xSize == x.gSize)
        if (group) {
          group.gSize = x.gSize;
          group.gPrice = x.gPrice;
        } else {
          groupedUp.push(x);
        }
      }
    })
    console.log(groupedUp);

    【讨论】:

    【解决方案2】:

    您好,欢迎来到 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;
    }, []);
    

    【讨论】:

    • 您好!感谢您的热烈欢迎,抱歉我没有包含代码,我现在已经更新了,它需要来自前端客户端的输入,所以它不能真正运行,我只包含了对这个数组很重要的两个部分,显示的第一个函数是收集数组 stockx 的数据并将其推送到那里,最后一个函数也在执行相同的操作并将数据推送到 stockx。不幸的是,我无法让您的代码正常工作,它只会输出相同的 (42) 数组大小。感谢您的浏览和帮助,非常感谢!
    猜你喜欢
    • 2013-10-07
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2020-01-27
    • 2022-12-12
    • 1970-01-01
    相关资源
    最近更新 更多