1

I am trying to make an inventory system where when an item is obtained it will only put the item into the array if it does not already exist in the array. Then regardless of whether it was put into the array or not, find the item in the array and increase its amount by 1. This is what I have at the moment:

function newItem(){
    apple = new uniqueItem("apple", "resources/apple.png")

    if (inventory.indexOf(apple) != null){
        inventory.push(apple)
    }
    inventory[inventory.indexOf(apple)].amount += 1
}

Im still pretty new to javascript and I would appreciate it if someone could point me in the right direction

4
  • 2
    indexOf(object) ? I guess you should deal with the keys of the object.. Commented Mar 31, 2016 at 5:09
  • var arr=[{a:10},{b:20}]; var a={a:10}; arr.indexOf(a); will be -1 Commented Mar 31, 2016 at 5:10
  • ah ok, but just out of interest would something like 'arr.indexOf({name:"apple", imgLink:"resources/apple.png"})' work? Commented Mar 31, 2016 at 5:42
  • 2 objects can be equal if all the keys and values of the object are equal..And that is why you need to iterate and compare.. Commented Mar 31, 2016 at 5:46

4 Answers 4

1

There is this Array.prototype.includes() method at your service just for this job.

var o1 = {a:1,b:2},
   arr = [];
arr.push(o1);
arr.includes(o1); // <- true so don't add again

For more on this check Array.prototype.includes() out.

Sign up to request clarification or add additional context in comments.

5 Comments

var arr=[{a:10},{b:20}]; and arr.includes({a:10}); is evaluated as false
@RayonDabre so do arr.indexOf({a:10}); returns -1. Plus in my above example arr.indexOf(o1) would return 0 and this might yield false if you don't check properly like ~arr.indexOf(o1) which makes your code unreadable. Best is to implement Array.prototype.includes() since it's already there.
The reason is you are passing reference not the value..Reference can point to the same object
IE doesn't support includes function. you need latest chrome and firefox. Better go with indexOf or loop and match .
if you are using lodash you can check .findIndex(array, [predicate=.identity]) this function which will find index based on your property
0

You should not use indexOf here. Use something like below which iterates over the array and compares with each object in it. indexOf takes a string as value rather than a object.

function containsObject(obj, list) {
   var i;
   for (i = 0; i < list.length; i++) { 
     if (list[i] === obj) { return true; } 
   } 
   return false;
 }

Comments

0

You can do it by your own! Just loop through all inventory and check whether the item you are trying to insert is already exist. If it doesn't exist then insert it.

Comments

0

I think this may help you too

    <p> Year : <select style="width: 150px;">
        <option value ="2014">2014</option>
        <option value ="2015">2015</option>
        <option value =" '2015">2015</option>

    </select>
    </p>

    <input  type="submit" value="Submit">
            <input type="submit" value="cancel">

</center>
</body>

1 Comment

Aaah! What ? How ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.