【问题标题】:Adding items according to object's value根据对象的值添加项目
【发布时间】:2017-08-27 15:12:30
【问题描述】:

这是我目前拥有的代码:

//Create offer makes a new object
var offer = manager.createOffer(steamID)

//The items i have in cart
var cart = {
    "Scrap Metal": 2
}

//The json file that holds all the assetid
var data = {
    "Scrap Metal": {
        "appid": "440",
        "contextid": "2",
        "assetid": [
            "123456",
            "234567",
            "345678",
            "546789"
        ]
    }
}

var itemInCart = Object.keys(cart)
for(i=0 ; i< itemInCart.length; i++){
offer.addMyItem({
    "appid": data[itemInCart[i]].appid,
    "contextid": data[itemInCart[i]].contextid,
    "assetid": data[itemInCart[i]].assetid

    })
}

//Offer's output should be like this
[{
    "appid": "440",
    "contextid": "2",
    "assetid": "123456"
},{
    "appid": "440",
    "contextid": "2",
    "assetid": "234567"
}]

*为更好地理解我想要的东西而进行了编辑(希望:V)这意味着如果 cart 对象项的值为 2,它将添加 2 个新对象,但具有不同的资产 ID

【问题讨论】:

  • 请清楚一件事,在最后的offer object,你想assetid 是一个数组还是你想添加两个具有不同assetid 的对象,基于项目的值在购物车中,例如:{ scarp metal: 2 },所以您希望在报价中添加 2 个对象,每个对象分别具有前两个资产 ID?
  • 是的,我希望它根据购物车对象添加assetid的数量
  • 对不起,我无法完全理解你。请choose one of these或多解释一下。
  • offer 2,但我想根据购物车对象添加具有不同assetid 的相同商品,例如购物车{"scrap": 2},我希望它添加2 个不同assetid 的废金属
  • 我相信,offer_2 有不同的assetid,而我的回答也是如此。!!

标签: javascript arrays node.js object


【解决方案1】:

您可以使用数组slice 来获取数组的第一个n 元素。 Read More about slice

var items = {
  "Scrap Metal": {
    "market_hash_name": "Scrap Metal",
    "appid": 440,
    "contextid": "2",
    "assetid": [
      "6090807122",
      "6090807127",
      "6090807143",
      "6090807132",
      "6090807183",
      "6090807173",
      "6090807167",
      "6090807160",
      "6090807154"
    ]
  }
}

var cart = {
  "Scrap Metal": 4
}

var manager = {
  result: [],
  createOffer: function(thing) {
    return {
      addMyItem: function(obj) {
        manager.result = [ ...manager.result, obj ];
      },
      get: function(obj) {
        return manager.result;
      }
    };
  }
}

var offer = manager.createOffer();

Object.keys(cart).forEach( cartItem => {

  if (items[cartItem].assetid.length < cart[cartItem]) {
    console.log('dont have enough asset id');
  }

  items[cartItem].assetid.slice(0, cart[cartItem]).forEach( assetid => {
    
    offer.addMyItem({
      "assetid": assetid,
      "appid": items[cartItem].appid,
      "contextid": items[cartItem].contextid
    })
  })


})

console.log(offer.get());

由于在上面的示例代码中,我使用了"Scrap Metal": 4。它将产生如下所示的结果,结果对象中包含前 4 个资产 ID。

[
  {
    "assetid": "6090807122",
    "appid": 440,
    "contextid": "2"
  },
  {
    "assetid": "6090807127",
    "appid": 440,
    "contextid": "2"
  },
  {
    "assetid": "6090807143",
    "appid": 440,
    "contextid": "2"
  },
  {
    "assetid": "6090807132",
    "appid": 440,
    "contextid": "2"
  }
]

【讨论】:

  • 这不是我想要的,我在对象购物车中有一个 2,我想添加 2 个相同项目的资产 ID
  • 我已经加了一个sn-p,这样你就可以在这里测试代码了,根据你上面cmets中所说的,我相信这段代码是正确的。
【解决方案2】:

此示例使用 array.shift() 获取并从assetid 数组中删除第一项。

var fakeCart = [];
var manager = {
  createOffer: function(thing) {return {addMyItem: function(obj) { fakeCart.push(obj) }};}
}
var steamID = "fake";

var items = {
        "Scrap Metal": {
            "market_hash_name": "Scrap Metal",
            "appid": 440,
            "contextid": "2",
            "assetid": [
                "6090807122",
                "6090807127",
                "6090807143",
                "6090807132",
                "6090807183",
                "6090807173",
                "6090807167",
                "6090807160",
                "6090807154"
            ]
        }
    };

var cart = {
    "Scrap Metal": 2,
};

var offer = manager.createOffer(steamID);
for (var key in cart) {
  for (var i=0; i < cart[key]; i++) {
    if (items[key] && items[key].assetid.length) {
      offer.addMyItem({
        assetid: items[key].assetid.shift(),
        appid: items[key].appid,
        contextid: items[key].contextid
      });
    } else {
      console.log("I don't have the item " + key);
    }
  }
}

console.log(fakeCart);

【讨论】:

    【解决方案3】:

    for (i = 0; i < itemInCart.length; i++) {
      items[itemInCart[i]].assetid.forEach(function(aid){
            offer.addMyItem({
                "assetid": items[itemInCart[i]].assetid,
                "appid": items[itemInCart[i]].appid,
                "contextid": aid
            })  
      })
    
     }

    【讨论】:

    • items[itemInCart[i]].contextid.forEach(function(cid){ contextid ? 你的意思是assetid ?
    猜你喜欢
    • 2016-09-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多