简体   繁体   中英

iterating through multiple array in javascript

I have data array as follows:

 totalDays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
 saleDays =  ["tue", "thu"],
 sale = [314,240].

I need to modify 'sale' array in a such a way that, sale array should compare with totalDays array with respective saleDays,if sale are present for that day include sale array amount else include 0 in sale array.

for above example i need sale array output as follows:

sale  = [0,314,0,240,0,0,0].

code:

 var arr = _.zip(saleDays, sale);
    var newSale = [];
    var saleAmt = 0;
    _.forEach(arr, function(element) {
        saleAmt = totalDays.indexOf(element[0]) > -1 ? element[1] : 0;
        newSale.push(saleAmt); 
    });

but the problem is in this case the indexOf condition is always returns true, So i have tried to use nested loop like:

    _.forEach(totalDays, function(dt) {
        _.forEach(arr, function(element) {
        saleAmt = dt === element[0] ? element[1] : 0;
        newSale.push(saleAmt); 
        });
    });

But in this case the loop is running 'm * n'times. where m is length of 'totalDays' and 'n' is length of 'arr'. Which i want to avoid and loop should run for totalDays.length times and produced result. Please help me to achieve desire output.

As per @Tao PR suggestion, same thing I am trying to use with array of month sale with week startdate.

for eg:

totalSale = ['Wed Jun 10 2015','Tue Jun 16 2015','Mon Jun 22 2015','Sun Jun 28 2015','Sat Jul 04 2015','Fri Jul 10 2015']
saleDays: [ 'Mon Jun 15 2015', 'Tue Jul 07 2015' ]
sale: [134,245]

Now I have to check in which week of totalSale, sale has been done. For above example I need saleData = [134,0,0,0,245,0] I have tried this by creating array of array for totalSale,

var mappedSale = _.map(totalDays, function(d){ 
    for(var j = 0; j < saleDays.length; j ++ ){
        var i = d.indexOf(saleDays[j]);
            if (i<0){ return 0 }
            else return sale[i];
    }
});

where,

totalDays = [['Wed Jun 10 2015',..'Mon Jun 15 2015'],['Tue Jun 16 2015',..'Sun Jun 21 2015'],...]

Will you please tell me what is wrong in this?

You can use Array.prototype.map to map totalDays array with the corresponding value of sale like this:

 totalDays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"],
 saleDays =  ["tue", "thu"],
 sale = [314,240];

// Output
var mappedSale = totalDays.map(function(d){
    var i = saleDays.indexOf(d);
    if (i<0){ return 0 }
    else return sale[i];
});

alert(mappedSale); // <-- [0,314,0,240,0,0,0]

Equivalent logic with underscore :

var mappedSale = _.map(totalDays, function(d){
    var i = saleDays.indexOf(d);
    if (i<0){ return 0 }
    else return sale[i];
});

You may try this without any external library:

 var totalDays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"], saleDays = ["tue", "thu"], sale = [314, 240], newSales = [], i = -1, l = totalDays.length, j; while (l > ++i) { j = saleDays.indexOf(totalDays[i]); newSales.push((-1 === j) ? 0 : sale[j]); } console.log(newSales); 

With .map() :

 var totalDays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"], saleDays = ["tue", "thu"], sale = [314, 240]; var s; var newSales = totalDays.map(function(e) { s = saleDays.indexOf(e); return (-1 === s) ? 0 : sale[s]; }); console.log(newSales); 

There is nothing wrong with the other answers, but the logic might be clearer if you did a bit of preparation, creating a saleData object as follows:

var saleData = {};
for (var i = 0; i < sale.length; i++) { saleData[saleDays[i]] = sale[i]; }

This will give you an object

{ tue: 314, thu: 240 }

If you're using Underscore, you can just say

saleData = _.object(saleDays, saleData);

In either case, now the map is a bit easier:

totalDays . map(function(day) { return saleData[day] || 0; })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM