我如何使用$ .each jquery函数获得两个不同数组的相同ID

时间:2018-08-20 06:15:45

标签: jquery

这是在$ .each中获取数组值的方式。我正在获取每个模型和数量的价值。 reserved_qty是我表中的值。这是我的查询。

$sql = $conn->prepare("SELECT *, COUNT(backup_sales.model_id) as ReservedQTY FROM backup_sales 
              LEFT JOIN product_model ON backup_sales.model_id = product_model.model_id 
              LEFT JOIN brand ON brand.brand_id = product_model.brand_id 
              LEFT JOIN client ON client.client_id =backup_sales.client_id 
              LEFT JOIN company ON company.company_id = client.company_id 
              WHERE backup_sales.po_ID = '".$_GET['po_ID']."'
              GROUP BY backup_sales.model_id, backup_sales.client_id");


//Reserved Qtys Function
$('input[name^="reserved_qty"]', '#tblEdit').each(function(){
  obj = {};
  if (this.className == "" && this.value == "0"){
    return this.value;
  }
  else
  {
    obj["model"] = this.className;
    obj["reserved_qty"] = this.value;
  }
  my_arr.push(obj);
});

//Input Qtys Function
$('input[name^="quantity"]', '#tblEdit').each(function(){
  obj = {};
  if (this.className == "" && this.value == "0"){
    return this.value;
  }
  else
  {
    obj["model_id"] = this.className;
    obj["input_quantity"] = this.value;
  }
  dataObs.push(obj);
});

这是我显示每个值并分配2个数组的方式。

   $.each(my_arr, function(index, value){
    $.each(dataObs, function(subIndex, val){
    console.log(value);
    console.log(val);
  });
});

我希望我的价值成为

{model: "1", reserved_qty: "4"} 
{model_id: "1", input_quantity: "4"} 
{model_id: "3", input_quantity: "3"} 
{model: "3", reserved_qty: "3"} 
{model: "4", reserved_qty: "3"} 
{model_id: "4", input_quantity: "3"} 

到目前为止,我的价值是继续获得这些价值

{model: "1", reserved_qty: "4"} 
{model_id: "3", input_quantity: "3"} 
{model: "1", reserved_qty: "4"} 
{model_id: "4", input_quantity: "3"} 
{model: "3", reserved_qty: "3"} 
{model_id: "1", input_quantity: "4"} 
{model: "3", reserved_qty: "3"} 
{model_id: "4", input_quantity: "3"}

1 个答案:

答案 0 :(得分:0)

不确定我是否正确理解了您的问题,但是由于您有嵌套循环,因此需要检查内部循环的每次迭代,以确保modelmodel_id匹配。否则,它将为表单中的每个条目输出每个数据库行(您的外部循环)。

类似这样的东西:

 $.each(my_arr, function(index, value){
    $.each(dataObs, function(subIndex, val){
    if (value.model == val.model_id) { // you need to check model vs model_id here
       console.log(value);
       console.log(val);
    }
  });