13

I'm newbie in JavaScript. I have a question

My code in Java:

public void checkArray(int a, int b) {
    int[]days = new int[]{5, 15, 25};
    int[]hours = new int[]{6, 8, 7};
    ArrayList<Interger> result = new ArrayList<>();
    for (int i = 0; i < days.length-1;  i++) {
        if (days[i] < a && b < days[i+1]) {
            result.add(hours[i]);
        } else if (days[i] > a && days[i] < b) {
            result.add(hours[i]);
            if (i > 0) {
                result.add(hours[i-1]);
            }
        }

How can I write this code in JavaScript with Lodash _.each? I can't find a variable like [i] in JavaScript code, so I can't check the condition [if (days[i] < a && b < days[i+1])]

My code in JavaScript:

_.each(days, day => {
    //TODO something
})
1
  • Javascript uses the same syntax days[i] to access an array member. To declare the array, you'd use const days = [5, 15, 25]; Commented Jan 31, 2019 at 23:10

3 Answers 3

11

From the Lodash documentation:

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

This means that you can simply do:

_.each( days, function( day, i ){

});

So your whole code becomes:

var days = [5, 15, 25];
var hours = [6, 8, 7];
var result = [];

_.each( days, function( day, i ){
    if( days[i] < a && b < days[i+1] ){ // days[i] == day
        result.push( hours[i] );
    } else if( days[i] > a && days[i] < b ){
        result.push( hours[i] );
        if( i > 0 ){
            result.push( hours[i-1] );
        }
    }
})

Here is a jsFiddle to experiment.

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

Comments

1

From Lodash Documentation:

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

_.each gets only one Collection to iterate as the first argument, and a function "iteratee" for the second argument. So, for is a good option for you.

However, if you still want to use lodash you can get help from _range to create an array of indices, and then you can do something like that:

(in javascript of course)

let days = [5, 15, 25];
let hours = [6, 8, 7];
let result = [];
_.each(_.range(days.length),function(i) {
    if (days[i] < a && b < days[i+1]) {
        result.push(hours[i]);
    } else if (days[i] > a && days[i] < b) {
        result.push(hours[i]);
        if (i > 0) {
            result.push(hours[i-1]);
        }
    }
});

Comments

-1

You can use javascript forEach in the array (no need to use Lodash) and it will be easier in ReactJS:

var days = [5, 15, 25];
days.forEach((day) => {
                  //do things with the day
                  window.alert(day)
                  });

1 Comment

Sorry mate, I must use Lodash :)

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.