2

I'm trying to add one object to another object.

Let's say I have two objects. selectedItemObj and selectedItemQueue. I'd like to add the selectedItemObj to selecteditemQueue but ONLY if that selectedItemObject does not match any of the objects in selectedItemQueue

Let's say I have this object here:

var selecteditemObj = [
   {
      "market_hash_name":"Chroma 2 Case Key 1",
      "assetid":"92700754417_143965972",
      "amount":1,
      "image":"https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA/"
   }];



var selectedItemQueue = [
   {
      "market_hash_name":"Chroma 2 Case Key 2",
      "assetid":"92700754667_143865972",
      "amount":1,
      "image":"https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOie3rKFRh16PKd2pDvozixtSOwaP2ar7SlzIA6sEo2rHCpdyhjAGxr0A6MHezetG0RZXdTA/"
   },
   {
      "market_hash_name":"Shadow Case Key 3",
      "assetid":"1293611210722_143865972",
      "amount":1,
      "image":"https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOiePrKF4wi6aaIGwStN_jl4bSzvXwMO6AwDlSvsYoiOiZ8dij3QbtqkU9ZnezetFWWxusZg/"
   },
   {
      "market_hash_name":"Shadow Case Key 4",
      "assetid":"123393510722_143861972",
      "amount":1,
      "image":"https://steamcommunity-a.akamaihd.net/economy/image/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXX7gNTPcUxuxpJSXPbQv2S1MDeXkh6LBBOiePrKF4wi6aaIGwStN_jl4bSzvXwMO6AwDlSvsYoiOiZ8dij3QbtqkU9ZnezetFWWxusZg/"
   }
];

Here is my attempt of it, it adds to the queue but it doesn't check if the child object exists. How can I add that?

function addItemToSelectedQueue(assetId){
    var itemObj = findItemById(assetId);
    var queueHasItem = false;
    for (var i = selectedItemQueue.length - 1; i >= 0; i--) {
        if (selectedItemQueue[i] === itemObj) {
            queueHasItem == true;
        };
    };
    if (queueHasItem == false) {
        selectedItemQueue.push(itemObj);
        updateSelecteditems();
    };
}
kinx
  • 463
  • 5
  • 12
  • 31
  • you can't check match by "selectedItemQueue[i] === itemObj", you should compare the properties one by one (or the key value if it has), and only all properties match, queueHasItem updated to true. – Surely Jan 19 '16 at 15:39
  • 1
    Minor note: there's no reason to end your `if` blocks with a semicolon. In fact, I'd recommend against it. – Mike Cluck Jan 19 '16 at 15:40
  • 1
    Another minor note, you can use `!condition` instead of `condition == false` for brevity – nem035 Jan 19 '16 at 15:40
  • 4
    Using the equality operator to compare two objects checks to see if they are _the same object_ rather than if they have the same values. See this question for more information: http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects – Michael L. Jan 19 '16 at 15:40
  • First, pick an id property to compare equality by, it's probably either assetid, or market_hash_name or maybe a combination of the two. Also, this kind of thing is a lot cleaner using a library like Underscore or Lodash. – Shashank Jan 19 '16 at 15:41
  • https://jsfiddle.net/mplungjan/gb793m5d/ – mplungjan Jan 19 '16 at 15:57

2 Answers2

0

What about something like this @JsBIN? I assume market_hash_name is used to differential between each object.

var selecteditemObj = [
   {
      "market_hash_name":"Chroma 2 Case Key 1",
   }
];

var selectedItemQueue = [
   {
      "market_hash_name":"Chroma 2 Case Key 2",
   },
  {
      "market_hash_name":"Chroma 2 Case Key 3",
   }
];

function addItemObjIfNotExist(obj1, obj2, id) {
  for(var i = 0, len = obj2.length; i < len; i++) {
    if(obj1[1][id] !== obj2[i][id]) {
      obj2.push(obj1);
    }
  }
  return obj2;
}

console.log(addItemObjIfNotExist(selectedItemQueue,selecteditemObj, 'market_hash_name'));

Output:

[[object Object] {
  market_hash_name: "Chroma 2 Case Key 1"
}, [[object Object] {
  market_hash_name: "Chroma 2 Case Key 2"
}, [object Object] {
  market_hash_name: "Chroma 2 Case Key 3"
}]]
Bun
  • 3,037
  • 3
  • 19
  • 29
0

I would do it this way:

function addItemToSelectedQueue(fromobj, toobj){
    var found = false;
    for(var x=0; x<toobj.length; x++){
      if(toobj[x].market_hash_name == fromobj[0].market_hash_name){
        found = true;
      }
    }

    if(found == false){
      toobj.push(fromobj[0]);
    }
}

Usage:

addItemToSelectedQueue(selecteditemObj, selectedItemQueue);

Works perfectly fine for me!

Tewdyn
  • 687
  • 3
  • 16