0

So here is my code, it is in NodeJS and not javascript :

       while (otn !== -1) {
     var li= inventory.length - 1;
     while (li !== -1){
       li = li- 1;
       if (inventory[li].market_name === orderitemname[otn]){
         console.log("Add item to trade " + orderitemname[otn]);
         offer.addMyItem(inventory[li]);
         console.log(inventory[li])
         delete inventory[li];
         //Close everything

So everything is working fine, except delete inventory[li], what it does is it's deleting the value from the json, but when the while loop runs again, the program tries to read the value from the same and crashes, so is there a way we could like completely delete it(The following bunch of code) and then not even have it listed ? Btw here is my JSON :

CEconItem {
appid: 730,
contextid: '2',
assetid: '13799167798',
classid: '506854340',
instanceid: '188530139',
amount: 1,
pos: 1,
id: '13799167798',
background_color: '',
icon_url: blah,
icon_url_large: blah,
descriptions: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
tradable: true,
actions: [ [Object] ],
name: 'P90 | Module',
name_color: 'D2D2D2',
type: 'Mil-Spec Grade SMG',
market_name: 'P90 | Module (Factory New)',
market_hash_name: 'P90 | Module (Factory New)',
market_actions: [ [Object] ],
commodity: false,
market_tradable_restriction: 7,
marketable: true,
tags: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
is_currency: false,
market_marketable_restriction: 0,
fraudwarnings: [] },
And a lot of these...

Any help would be greatly appreciated :) Thanks!

PS: I'm a complete noobie, if I'm missing something as simple as cake, please don't roast me :)

DentFuse
  • 21
  • 1
  • 7
  • please use this function inventory.splice(li, 1) instead of delete inventory[li]. Note: delete is used to delete the memory not removing one from array. delete can be used to remove field from object. – Lead Developer Mar 13 '18 at 15:00
  • @hkg328: `delete` has little to do with memory allocation in JS. Its job is to undefine a property on an object. (You're right that it's probably not the right tool for this job, though.) – cHao Mar 13 '18 at 15:03
  • @hkg328 — The accepted answer on the duplicate question shows how to use splice already. There's no need to repeat it. – Quentin Mar 13 '18 at 15:03
  • @hkg328 Thank you for ur answer, it works, but I still don't understand the difference b/w `splice` and `delete`, is `splice` used when we want to like remove a value from an array? If so why didn't `delete` work for deleting the above JSON code? Did `delete` just empty the value and so node wasn't able to read from it and threw undefined? Sorry if I'm asking a lot of questions, just wanna know and learn :) – DentFuse Mar 13 '18 at 15:13
  • @DentFuse: For arrays, the properties corresponding to indexes are a bit magical. Setting them can affect the `length` property. But deleting them doesn't do the reverse...so you basically end up not with an empty array, but with an array full of `undefined`s. If your code is assuming those entries are objects, it's likely to choke on them. – cHao Mar 13 '18 at 21:27

0 Answers0