0

I have some script to read the values from html table but there is a problem to compare name of existing items in array with temporary item name.

$(document).ready(function () {
    var selling = new Array();
    $('table tr').each(function() {
        var name = $(this).find('td:eq(1)').html();
        var price = $(this).find('td:eq(3)').html();
        var amount = 1;
        var item = new Array(name,price,amount);
        var found = 0; //selling.find(name)? notworking :)
        if(found.length > 0) {
            alert('found'); //get amount and change it
        } else {
            selling.push(item); //push new item to array                       
        }
    });
    alert(selling);
});

I want to get an array

[cat, 10$, 150]
[cow, 120$, 7]
[bird, 500$, 1]
[horse, 400$, 2]

Can somebody tell me how to compare those names? or do this better way?

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
lucaste
  • 536
  • 2
  • 7
  • 21
  • this is duplicate of: https://stackoverflow.com/questions/19543514/check-whether-an-array-exists-in-an-array-of-arrays – lucaste Aug 07 '17 at 15:09

1 Answers1

0

Try this, using $.inArray():

$(document).ready(function () {
    var selling = new Array();
    $('table tr').each(function() {
        var name = $(this).find('td:eq(1)').html();
        var price = $(this).find('td:eq(3)').html();
        var amount = 1;
        var item = new Array(name,price,amount);
        if($.inArray(item,selling) > -1) {
            alert('found'); //get amount and change it
        } else {
            selling.push(item); //push new item to array                       
        }
    });
    alert(selling);
});
A. Wolff
  • 73,242
  • 9
  • 90
  • 149