我的意图是将结构数组值的所有输入加起来。我以前做过,但是使用普通数组:let total = array.reduce(0, +)
。
让我们说这是我的结构:
struct addDataStruct: Codable {
var driven: Int
var date: Date
var consumedL: Double
var pricePerLiter: Double
}
这是我的数组:
var addDataArray: [addDataStruct] = []
func createStructArray() {
let addData: addDataStruct = addDataStruct(driven: ..., date: Date(), consumedL: ..., pricePerLiter: ...)
addDataArray.append(addData)
}
如何将所有驱动值相加?
答案 0 :(得分:1)
您可以尝试
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Repeat DIV on button click</title>
<style>
.js-show-service{
margin:1rem auto;
border:1px solid red;
box-sizing:border-box;
padding:1rem;
}
.clone{
border:1px solid blue!important
}
input,button,select{padding:0.5rem}
button{min-width:10%;clear:none;float:none;margin:0.25rem;}
.clone:before{
content:attr(data-clone);
color:white;
background:black;
padding:0.5rem;
width:2rem;
height:2rem;
text-align:center;
margin:0;
left:-1rem;
top:-0.5rem;
position:relative;
}
</style>
<script>
document.addEventListener('DOMContentLoaded',e=>{
let parent=document.body;
let i=parent.querySelectorAll('div.js-show-service').length;
const clonediv=function( n ){
let clone=n.cloneNode( true );
clone.classList.add('clone');
clone.dataset.clone=i;
clone.querySelector( '.add_button' ).addEventListener('click',function(e){
clonediv( this );
}.bind( clone ) );
clone.querySelector( '.suppr_button' ).addEventListener('click',function(e){
removediv( this )
}.bind( clone ) );
clone.querySelectorAll('input').forEach( inp => {if( inp.value!=='' )inp.value='';} )
clone.querySelectorAll('*').forEach( el=>{ if( el.hasAttribute('id') )el.removeAttribute('id') })
parent.appendChild( document.createComment( 'Clone: '+i ) );
parent.appendChild( clone );
clone.scrollIntoView({behavior:'smooth',block:'end'})
i++;
return clone;
}
const removediv=function( n ){
parent.removeChild( n );
}
Array.prototype.slice.call( document.querySelectorAll( 'button' ) ).forEach( function(bttn){
bttn.addEventListener('click',function(event){
if( this.className=='add_button' )clonediv( this.parentNode );
});
});
});
</script>
</head>
<body>
<h1>Repeated content using javascript</h1>
<!-- repeat ALL of the following -->
<div class='w-full dis-none js-show-service'>
<div class='wrap-input100 input100-select bg1'>
<div class='wrap-input100 input100-select bg1'>
<span class='label-input100'>Techno Garentie d'Origine *</span>
<div>
<select class='js-select2' name='service'>
<option value='Solaire'>Solaire
<option value='Hydro'>Hydraulique
<option value='Eolien'>Eolienne
<option value='BioMasse'>BioMasse
</select>
<div class='dropDownSelect2'></div>
</div>
</div>
<div class='wrap-input100 input100-select bg1'>
<span class='label-input100'>Provenance Garentie d'Origine *</span>
<div>
<select class='js-select2' name='service'>
<option value='europe'>Europe
<option value='allemagne'>Allemagne
<option value='autriche'>Autriche
<option value='belgique'>Belgique
<option value='chypre'>Chypre
<option value='croatie'>Croatie
<option value='danemark'>Danemark
<option value='espagne'>Espagne
<option value='estonie'>Estonie
<option value='finlande'>Finlande
<option value='France'>France
<option value='irlande'>Irlande
<option value='islande'>Islande
<option value='italie'>Italie
<option value='lituanie'>Lituanie
<option value='luxembourg'>Luxembourg
<option value='norvege'>Norvege
<option value='pays-bas'>Pays-Bas
<option value='republique-tcheque'>Republique-Tcheque
<option value='slovenie'>Slovénie
<option value='suede'>Suède
<option value='suisse'>Suisse
</select>
<div class='dropDownSelect2'></div>
</div>
</div>
<div class='wrap-contact100-form-range'>
<span class='label-input100'>Pourcentage D'energie Verte *</span>
<div class='contact100-form-range-value'>
<span class='value-upper'>50</span>%
<input type='text' name='from-value' />
<input type='text' name='to-value' />
</div>
<div class='contact100-form-range-bar'>
<div class='filter-bar'></div>
</div>
</div>
<!--<button class='suppr_button'>-</button>-->
</div>
<button class='add_button'>+</button>
<button class='suppr_button'>-</button>
</div>
<!-- END repeated content -->
</body>
</html>
OR
let total = array.lazy.map{ $0.driven }.reduce(0, +)