1

var tags = ["abcd", "aaacd", "ade"];

I'm trying to loop through each string in the array and find its index. Again loop through characters in each string and find its index (eg.)

$.each(arr, function( index, value ) {
    $.each(value.split(""), function( i, v ) {
        alert( i + ": " + v );
    });
});
3
  • Use value.split("") in second loop Commented Sep 9, 2018 at 7:34
  • What's your question, exactly, about this? Commented Sep 9, 2018 at 7:35
  • @Mohammad -- I tried split. It didin't work$.each(arr, function( index, value ) { $.each(value.split(""), function( i, v ) { alert( i + ": " + v ); }); }); Commented Sep 9, 2018 at 14:38

4 Answers 4

1

You have to split() the value with empty string so that it becomes an array and you can implement .each():

value.split('')

var tags = ["abcd", "aaacd", "ade"];
$.each(tags, function( index, value ) {
  $.each(value.split(''), function( i, v ) {
    alert( i + ": " + v );
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

1

First you have to convert string to array inside first each

var tags = ["abcd", "aaacd", "ade"];
$.each(tags, function( index, value ) {
     var va=value.split('');
    $.each(va, function( i, v ) {
        alert( i + ": " + v );
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

You could use a nested Array.from and map at the end the index and value.

var tags = ["abcd", "aaacd", "ade"],
    result = Array.from(tags, (s, i) => Array.from(s, (c, i) => [i, c]));
    
console.log(result);

Comments

0

There are numerous way of handling this problem. You could also use the .entries() in a for of loop:

var tags = ["abcd", "aaacd", "ade"];
for (var val of tags) {
  for (var [idx, char] of val.split('').entries()) {
    console.log(idx + " : " + char);
  }
};

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.