Pass value of a variable in Array?

Asked

Viewed 703 times

0

I have the following variable:

var prodadicional = $(this).attr('data-adicionais');

It returns two string-like values that are: Burger and Bacon. Like I do to put them inside a array?

I tried to pass inside the array and rely on length but only returns the value 1, having two values.

var dadosarrayAdicional = [prodadicional];
var teste = prodadicional.length;
  • Does it no longer return you in an array? When you give one alert(prodadicional) or console.log(prodadicional) he returns to you what?

  • If I put var test = dadosarrayAdicional.length; it returns the value 1. But it has 2 values.

  • Give a console.log(additional output ) and provide the result please.

  • If I by prodadicional.length it returns (16)

  • 1

    You are placing an array within another array, so the Count of the first array is actually 1, but within that array has 2 elements..

  • tried using JSON.stringify()?

  • The question is not very clear Felipe, try to put some verifiable example and/ or clarify better the inputs and outputs - as is the most you will get are guesses.

  • If you receive a string, you have to transfer it in array before, if your string is even "Burger and Bacon", receive as var prodadicional = $(this). attr('additional date'). split("and");

  • If I give a console.log(additional prod) it returns the value in string. Burger and Bacon. I want him to put these variable values in an array and count the number of items right after to be able to do a for and list the data in each LI. Type Burger would stand on one LI and Bacon on another. Only if I give a dataArrayAdicional.length it returns 1.

  • It returns a string "Hambúrguer e Bacon" or [{"lanche":"Hambúrguer","lanche":"Bacon"}] or how?

  • String Hambúrger and Bacon.

  • Then do as @Felipeduarte said: separating the string with split ai will become array, from now on you can do what you want

Show 7 more comments

1 answer

1


Se está vindo como string você pode fazer de dois jeitos que são a mesma coisa:

$('.btn-comida').on('click',function(){
  var prodadicional = $(this).data('adicionais').split(' e ');
  $('#total').val(prodadicional.length);
  var ul = $('ul');
  ul.append('<li>'+prodadicional[0]+'</li>');
  ul.append('<li>'+prodadicional[1]+'</li>');
  
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

<div class="form-group">
 
  <button class="btn btn-primary btn-comida" data-adicionais="Hambúrguer e Bacon">Hambúrguer e Bacon</button>  
</div>

<div class="form-group">
  <label for="total">Total de itens</label>
  <input class="form-control col-lg-1" id="total"> 
</div>



<ul>
</ul>

Or

$('.btn-comida').on('click', function(){
  var prodadicional = $(this).data('adicionais').split(' e ');
  $('#total').val(prodadicional.length);
  $.each(prodadicional, function(i, prod){
     
      var li = "<li>"+ prodadicional[i] + '</li>';
      $('ul').append(li);
  })
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

<div class="form-group">
 
  <button class="btn btn-primary btn-comida" data-adicionais="Hambúrguer e Bacon">Hambúrguer e Bacon</button>
</div>
<div class="form-group">
  <label for="total">Total de itens</label>
  <input class="form-control col-lg-1" id="total"> 
</div>
<ul>
</ul>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.