简体   繁体   中英

Comparing arrays and adding?

I have two arrays

Data = [
"year": 2001, "count": 35,
"year": 2002, "count": 15,
"year": 2005, "count": 2
]

Years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]

What I want to do is: check if the Years exist in the Data , and if they don't, add "year" to the Data , with a "count" of 0.

All I can do so far is check that they're there.

 Data = [ "year": 2001, "count": 35, "year": 2002, "count": 15, "year": 2005, "count": 2 ] Years = [2000, 2001, 2002, 2003, 2004, 2005, 2006] for (var i = 0; i < Data.length; i++) { $(Years).each(function(value) { if (Data.indexOf(Years[value]) != -1) { console.log("missing one"); }; }) } 

If I assume your Data is an array of objects, then you can proceed like this:

 const Data = [ {"year": 2001, "count": 35}, {"year": 2002, "count": 15}, {"year": 2005, "count": 2} ]; const Years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]; Years.forEach(x => { if (!Data.some(({year}) => year === x)) Data.push({year: x, count: 0}); }) console.log(Data); // If you need to sort by year, then you could do this: Data.sort((a,b) => a.year - b.year); console.log(Data); 

If you are searching for performance, then you can first create a Set() of already existing years to improve the pushing algorithm later.

 const Data = [ {"year": 2001, "count": 35}, {"year": 2002, "count": 15}, {"year": 2005, "count": 2} ]; const Years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]; const dataYears = new Set(Data.map(({year}) => year)); Years.forEach(x => { if (!dataYears.has(x)) Data.push({year: x, count: 0}); }) console.log(Data); 

You may cycle on Years:

 Data = [ {"year": 2001, "count": 35}, {"year": 2002, "count": 15}, {"year": 2005, "count": 2} ]; Years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]; for (var i = 0; i < Years.length; i++) { if(!Data.find((e) => e.year == Years[i])) { Data.push({'year': Years[i], 'count': 0}) } } console.log(Data); 

Use includes and push it when includes returns false

 for (var i = 0; i < Data.length; i++) { if(!Years.includes(Data[i].year)) years.push(Data[i].year) } 

Data is invalid rn, it should be an array of objects:

data = [
{"year": 2001, "count": 35},
{"year": 2002, "count": 15},
{"year": 2005, "count": 2}
]

then you can compare:


for(let i = 0; i < data.length;  i++){
    if(years.includes(data[i].year){
       data[i].count++;
    }
}

PS: your variables should be camel cased to avoid confusion of being a class.

You could iterate the years and splice the missing years into the data array.

 var data = [{ year: 2001, count: 35 }, { year: 2002, count: 15 }, { year: 2005, count: 2 }], years = [2000, 2001, 2002, 2003, 2004, 2005, 2006]; years.forEach((i => year => { if (i >= data.length || year < data[i].year) { data.splice(i, 0, { year, count: 0 }); } i++; })(0)); console.log(data); 
 .as-console-wrapper { max-height: 100% !important; top: 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