1

I am working on the demo below. How can I load all of data in the age attributes into one array, so it looks like this?

 out = [48,14,139,49,15,135,51,15,140,49,15,135,51,15,140,52,16,141] 

let agesindx =[];
$(".box").each(function(){
    //agesindx.push($(this).data('ages').split(','));
    agesindx.push($(this).data('ages'));
  });
 console.log(agesindx);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>
halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

6 Answers6

2

data-ages should be an array of arrays like data-ages="[[48, 14, 139]]"

Use flat() to get a concatenated sub-array elements

let agesindx = [];
$(".box").each(function() {
  agesindx.push($(this).data('ages'));
});
console.log(agesindx.flat());
console.log(agesindx.flat(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[[48, 14, 139]]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140]]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140],[52,16,141]]"></div>

OR

using JSON.parse()

let agesindx = [];
$(".box").each(function() {
  agesindx.push(JSON.parse('[' + $(this).data('ages').toString() + ']'));
});
console.log(agesindx);
console.log(agesindx.flat(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>
User863
  • 19,346
  • 2
  • 17
  • 41
2

You can use the JSON.parse to parse your array from the string. Then you'll get an array inside a array and so on. So at end you can do join and split that will give you a single array.

let agesindx =[];
$(".box").each(function(){
    //agesindx.push($(this).data('ages').split(','));
    let x = JSON.parse('['+$(this).data('ages')+']');
    // console.log(JSON.parse('['+$(this).data('ages')+']'));
    agesindx.push(x);
  });
  
 console.log(agesindx.join().split(',') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>
Kaushik
  • 2,072
  • 1
  • 23
  • 31
1

If you have proper arrays in your data-ages attribute like [[49,15,135],[51,15,140]] you can get an array of arrays using map() function on the box element list. Then you can use $.map to flatten it - see demo below:

let result = $.map($(".box").map(function() {
  return $(this).data('ages');
}).get(), function(e) {
  return e;
});
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140]]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140],[52,16,141]]"></div>

Shortened in ES6:

let result = $.map($(".box").map((idx, el) => $(el).data('ages')).get(), e => e);
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140]]"></div>
<div class="box" data-ages="[[49,15,135],[51,15,140],[52,16,141]]"></div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • 1
    @MonaCoder adding on to the other answers, see another option if you have proper arrays in your data attribute... – kukkuz Apr 15 '19 at 07:13
  • 1
    And, fwiw (I may be stating the obvious but...), the code snippet shown here *also* works when the data of the first element is also structured as a 2-D array: `data-ages="[[48, 14, 139]]"` (i.e., double brackets on the end) – RichS Apr 15 '19 at 07:29
0

push was not adding the individual integers to the array because the latter two data ages were strings. You can use regex to extract just the digits and form an array with match, push to agesindx, then flatten the resulting nested array at the end.

let agesindx =[];
$(".box").each(function(){
    let ages = String($(this).data('ages')).match(/\d+/g);
    agesindx.push(ages);
});
console.log(agesindx.flat());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>
Auran Buckles
  • 31
  • 1
  • 5
0

Well You can use JSON.parse to get data as array and then to concat use concat like here : Merge/flatten an array of arrays

$(document).ready(function(){
  var dataList = $(".box").map(function() {
    return JSON.parse('['+ $(this).data("ages")+']');
  }).get();
  
  var merged = [].concat.apply([], dataList);
  console.log(merged);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>
Kamalesh M. Talaviya
  • 1,422
  • 1
  • 12
  • 26
0

This code solves the OP's question exactly as asked, in the output format shown, without requiring modification to the html data attributes:

let agesindx =[];
$(".box").each(function(){
    var tmpArr = $(this).data('ages')
    if( typeof tmpArr == 'string' )
     tmpArr = JSON.parse(tmpArr.replace(/\]\,\[/g, ','));
    agesindx = agesindx.concat(tmpArr); 
  });
 console.log(agesindx)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box" data-ages="[48, 14, 139]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140]"></div>
<div class="box" data-ages="[49,15,135],[51,15,140],[52,16,141]"></div>

If, however, the data type consistency could be ensured to the same for each element, the code could probably be better at flattening it (re: kukkuz's answer).

RichS
  • 913
  • 4
  • 12