1

I have the following array:

 var elements = [
        { "a": [ "1", "2", ] }, 
        { "b": [ "1", "2", "3", "4" ] }, 
        { "c": [ "1", "2", "3", "4", "5", "6" ] } 
    ];

How do I iterate the array in order to output the following:

a-1,a-2,b-1,b-2,b-3,b-4,c-1, c2 etc

1
  • 1
    Have you tried anything so far? Commented Aug 12, 2019 at 9:08

2 Answers 2

2

Try with multi-pal nested loop like below.

var elements = [{
  "a": ["1", "2", ]
}, {
  "b": ["1", "2", "3", "4"]
}, {
  "c": ["1", "2", "3", "4", "5", "6"]
}];


$.each(elements, function(i, data) {
  $.each(data, function(key, val) {
    var value = val;
    $.each(value, function(i) {
      console.log(key + '-' + value[i]);
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this?

var elements = [
    { "a": [ "1", "2", ] }, 
    { "b": [ "1", "2", "3", "4" ] }, 
    { "c": [ "1", "2", "3", "4", "5", "6" ] } 
];
var result = '';
$.each(elements, function(i,e){
    $.each(e, function(ind, elem){
        $.each(elem, function(index, element){
            result += ind + '-' + element + ',';
        })
    })
})
result = result.substring(0, result.length-1);
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.