0

I am using javascript in my app which is a rhino javascript engine. I like to use array for an array.

The following code is my actual working code.

    while (input.hasNext()) {
        var data = input.next();
        var new_data = {};
        var i = 0;
        while(i<data.get('total_entries')){
        new_data.entries = data.get('total_entries');
        new_data.id = data.get('out').get(i).get('id');
        output.write(new_data);
        i++;
        }
    }

I need to create array for new_data[].

    while (input.hasNext()) {
        var data = input.next();
        var new_data[] = {};
        var i = 0;
        while(i<data.get('total_entries')){
        new_data[i].entries = data.get('total_entries');
        new_data[i].id = data.get('out').get(i).get('id');
        output.write(new_data[i]);
        i++;
        }
    }

But its not working. So, help me to create array variable in it.

Thanks.

2 Answers 2

1

You mean something like this:

while (input.hasNext()) {
    var data = input.next();
    var new_data = [];
    var i = 0;
    while(i<data.get('total_entries')){
    new_data.push({entries: data.get('total_entries'), id: data.get('out').get(i).get('id')});
    output.write(new_data[i]);
    i++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
var new_data[] = {};

First of all, that's invalid JavaScript.

new_data[i]

Looks like you want new_data to be an array of objects. Why not declare it as one and add objects on every iteration of the while loop:

var new_data = [];

while(...){
  new_data.push({
    entries: ...
    id: ...
  });
}

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.