0

I've have the function:

function getCotas() {
  var values = [];
  values['first'] = 10;
  $('.loop.scota').each(function(key, value) {
    var id = key + 1;
    values[$('.sdate.id' + id).text()] = parseInt($(this).text());
  });

  return values;
}

The output is the array with the keys:

[first: 160, 01/12/2017: 157, 04/12/2017: 153]

What I need is just the values of the array, without the keys:

[160,157,153]

I already tried to do a loop with the values, like this answer, but always return an empty [] or an error with the functions.

Someone knows how to resolve this problem?

João Mantovani
  • 2,230
  • 1
  • 21
  • 33
  • Possible duplicate of [How to append something to an array?](https://stackoverflow.com/questions/351409/how-to-append-something-to-an-array) – Cave Johnson Dec 04 '17 at 19:34

3 Answers3

1

Array push should be what you're looking for.

values.push(parseInt($(this).text());
pavel
  • 26,538
  • 10
  • 45
  • 61
0

See the below example code:

var values = [];
    values["key1"] = "value";
    values["key2"] = "value";
var result = [];
for (var key in values) {
    result = result.concat(values[key]);
}

alert(result);

https://jsfiddle.net/afje1Lhb/

bamberjp
  • 182
  • 4
  • Eh? Javascript has no `=>` operator for arrays (as I know) and you don't answer for question, the main point is how to put these values into array. – pavel Dec 04 '17 at 19:38
0

You could remove the loop if the keys are not important:

function getCotas() {
  var values = [];
  values[0] = 10; // first value
  var elArr = $('.loop.scota').toArray();
  values = values.concat(elArr.map(function (el) { return parseInt( $(el).text()); }));

  return values;
}

console.info(getCotas());