最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the array index in Lodash _.each - Stack Overflow

programmeradmin3浏览0评论

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
})

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
})
Share Improve this question edited Feb 2, 2019 at 20:01 Itay Grudev 7,4344 gold badges57 silver badges93 bronze badges asked Jan 31, 2019 at 22:46 SpiritSpirit 3072 gold badges4 silver badges13 bronze badges 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]; – user5734311 Commented Jan 31, 2019 at 23:10
Add a comment  | 

3 Answers 3

Reset to default 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.

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]);
        }
    }
});

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)
                  });
发布评论

评论列表(0)

  1. 暂无评论