0

Ho everyone, I'm new to coding and I would like to know how do I find out whether an array has repeated objects or not regardless of their order without knowing what these objects are yet? For example:

I create an empty array:

var random_array = [];

Then I use one function to push objects in that array, so I end up with:

var random_array = [ball, ball, tree, ball, tree, bus, car];

In the array above we have:

3 balls, 2 trees, 1 bus, 1 car.

So how will I be able to know these repeated numbers? Should I use a for loop or what? Anyway, I'm new to coding so please bear with me. And thanks in advance!

Edit:

Ok, so here's my simple code:

function Product(name, price) {
    this.name = name;
    this.price = price;
}

var register = {
    total: 0,
    list: [],

    add: function(Object, quant) {
        this.total += Object.price * quant;
        for (x=0; x <quant; x++) {
        this.list.push(Object);
        }
    },

undo: function() {
    var last_item = this.list[this.list.length - 1];
    this.total -= last_item.price;
    this.list.pop(this.list[this.list.length - 1]);
    },

print: function() {
    console.log("Super Random Market");
    for (x = 0; x < this.list.length; x++) {
        console.log(this.list[x].name + ": " + this.list[x].price.toPrecision(3));
    }
    console.log("Total: " + this.total.toFixed(2));
    }
}

var icecream_1 = new Product("Chocolate Icecream", 2.30);
var cake_1 = new Product("Chocolate cake", 4.00);

register.add(icecream_1, 5);
register.add(cake_1, 3);

register.print();

I'm trying to build a cash register and I'm trying to find out how to only print the items once with its quantity, instead of multiple times.

8
  • An object can't be repeated, as no two objects are the same? You're mixing up arrays, objects, strings, and undefined variables, so it's hard to understand what you're really asking ? Commented Aug 4, 2014 at 19:25
  • This may be of interest to you: stackoverflow.com/questions/13486479/… Commented Aug 4, 2014 at 19:27
  • Do you need to check for objects of the same type with the same property values or just objects of the same type? Commented Aug 4, 2014 at 19:27
  • welcome to coding, and welcome to stackoverflow. Its very difficult to help you because you didn't try anything or show anything you've already done. Our community is designed to help you learn, not do things for you. Try a loop. See if it works and then post it here if you're having trouble. Commented Aug 4, 2014 at 19:28
  • @Luka, Objects of the same type with the same property values. Commented Aug 4, 2014 at 19:30

2 Answers 2

4
var randomArray = ["ball", "ball", "tree", "ball", "tree", "bus", "car"];
var itemCount = {};

randomArray.forEach(function(value){
    if(value in itemCount) itemCount[value] = itemCount[value] + 1;
    else itemCount[value] = 1;
});

Then you can reference the number of "ball" in the array like this: itemCount.ball this will return 3.

Fiddle: http://jsfiddle.net/3XpXn/2/

Made it smaller:

var randomArray = ["ball", "ball", "tree", "ball", "tree", "bus", "car"],
    itemCount = {};

randomArray.forEach(function(value){
    value in itemCount ? itemCount[value] = itemCount[value] + 1 : itemCount[value] = 1;
});
Sign up to request clarification or add additional context in comments.

9 Comments

A nice solution to creating a unique array, but it isn't clear to me from the OP's statements if he wants that, or if he wants a count of occurrences.
@ThomasWTupper: It looks like he'd like to count duplicates: "3 balls, 2 trees..." etc.
oh I see. 1 second then.
Thanks for the replies everyone, I'll try this thing that Kolby suggested. At any rate what I am trying to learn is how to build a simple cash register, so I'll update the OP with my code too. I wanna know why an element in the array repeats so I don't need to print it out X times, and I can instead, print it once with its quantity!
@Kolby Upvoted. I'd still suggest just using a normal for loop, but I'll leave that up to you. For OP: Please note that Array.forEach is not supported in Internet Explorer 8 and below. ECMAScript 5 compatibility table
|
0

The below code should work.

    var randomArray = ["ball", "ball", "tree", "ball", "tree", "bus", "car"];       
        Array.prototype.findUniqueCount = function(){
          var collection = {}, i;   
          for (i = 0; i < this.length; i = i+1 ) {    
             collection[this[i]] = (collection[this[i]] || 0) + 1;
            }  
          return collection;
        };
console.log(randomArray.findUniqueCount());//{ ball: 3, tree: 2, bus: 1, car: 1 } 

check this url https://coderpad.io/HEGY26FN

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.