根据对象的值添加项目

时间:2017-08-26 16:24:15

标签: javascript arrays node.js object

这是我目前的代码:

//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个新对象,但具有不同的断言

3 个答案:

答案 0 :(得分:0)

&#13;
&#13;
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
        })  
  })

 }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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

&#13;
&#13;
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());
&#13;
&#13;
&#13;

由于在上面的示例代码中,我使用了"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 :(得分:0)

此示例使用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);