0
var link = [];

for(var x = 0; x < id_ion['ions'].length; x++) {
    var source,
    target;

    if(id_ion['ions'][x]['ID1'] == data.main[0].ID1) {
        source = id_ion['ions'][x]['ID1'];
        target = id_ion['ions'][x]['ID2'];
    } else {
        source = id_ion['ions'][x]['ID2'];
        target = id_ion['ions'][x]['ID1'];
    }
    var_edges.push({data:{
            source: source,      
            target: target,
            exp: id_ion['ions'][x]['exp'],
            pub: id_ion['ions'][x]['pub'],
            name: id_ion['ions'][x]['name'],
            age: id_ion['ions'][x]['age']
    }});

Hello, I am a beginner and I am trying hard to understand coding. I have looked everywhere and I was not able to understand this code completely.

6
  • 2
    what is the problem? what is it that you don't understand? don't post too broad questions... Commented Mar 15, 2016 at 15:06
  • I know there's an array but I can't understand the logic of the other variables and their functions. Commented Mar 15, 2016 at 15:09
  • Please remove the jquery tag. Commented Mar 15, 2016 at 15:16
  • If you don't understand the basic javascript syntax, you should take a general course in the language (or read a book, study online etc.) before being expected to work on existing code. Commented Mar 15, 2016 at 15:17
  • 1
    also you should know that in Javascript x['y'] is the same as x.y (assuming y is a word, not something that can't be a variable). So id_ion['ions'][x]['age] is the same as id_ion.ions[x].age Commented Mar 15, 2016 at 15:21

2 Answers 2

3

This is pretty basic code, so you should look into some basic online tools to boost your understanding. codecademy teaches basic javascript and would be a good spring board. I'll try to cover each chunk of code with the basics.

for(var x = 0; x < id_ion['ions'].length; x++) {

This is a for loop. Its going to iterate through each element of the array id_ion['ions'] so that you can evaluate them one by one. Each time the for loop goes through an iteration, x increments. So the first time we look at id_ion['ions'][0] which is the first element, next we look at id_ion['ions'][1] and so on.

    if(id_ion['ions'][x]['ID1'] == data.main[0].ID1) {
        source = id_ion['ions'][x]['ID1'];
        target = id_ion['ions'][x]['ID2'];
    } else {
        source = id_ion['ions'][x]['ID2'];
        target = id_ion['ions'][x]['ID1'];
    }

This is an if/else block. In this conditional, we see if the element that our for loop is currently evaluating is equal to data.main[0].ID1. If it is, we set the source and target one way, if not, we run the else and set them the other way.

    var_edges.push({data:{
            source: source,      
            target: target,
            exp: id_ion['ions'][x]['exp'],
            pub: id_ion['ions'][x]['pub'],
            name: id_ion['ions'][x]['name'],
            age: id_ion['ions'][x]['age']
    }});

This creates an array called 'data' which you fill with data that you collect for each iteration of your loop and pushes it into another array called var_edges. Do a little research and ask around for some good books/tutorials that can help you learn. You'll start to understand javascript in no time.

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

4 Comments

Thank you a lot Zach L for your answer and valuable time. It is much clearer for me now.
No problem. When you're learning, you're going to get confused or stuck. Search stackoverflow and generally, someone has already had the same issue and gotten a solution that can help you figure out your error. Happy Coding. :)
Thanks again for the advice.
If you feel like this answered your question, feel free to accept it as the correct answer
0
 for(var x = 0; x < id_ion['ions'].length; x++) {}

is used to deal with all the elements of the table.

var source, target;
if(id_ion['ions'][x]['ID1'] == data.main[0].ID1) {
   source = id_ion['ions'][x]['ID1'];
   target = id_ion['ions'][x]['ID2'];
} else {
   source = id_ion['ions'][x]['ID2'];
   target = id_ion['ions'][x]['ID1'];
}

is used to put the table elements into variables with an if statement.

var_edges.push({data:{
   source: source,      
   target: target,
   exp: id_ion['ions'][x]['exp'],
   pub: id_ion['ions'][x]['pub'],
   name: id_ion['ions'][x]['name'],
   age: id_ion['ions'][x]['age']
}});

is used to create an other array thanks to the push function.

1 Comment

Thank you a lot @ahbon for your answer and valuable time. It is getting clearer for me to understand now

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.