按照你的要求,求满足多个条件的相同属性的对象之和的Javascript程序如下
<script type=text/javascript>
var obj = [{source: "HTML",target: "JAVASCRIPT",value: 11},
{source: "HTML",target: "CSS",value: 22},
{source: "HTML",target: "JAVA",value: 33},
{source: "HTML",target: "JAVASCRIPT",value: 44}]
var result=[]
var flag=true
result.push(obj[0])
for(var i=1i<obj.lengthi++){
flag=true
for(var j=0j<result.lengthj++){
if(obj[i].source==result[j].source &&obj[i].target==result[j].target){
flag=false
result[j].value=obj[i].value+result[j].value
}
}
if(flag==true)
result.push(obj[i])
}
document.write(JSON.stringify(result))
</script>
源代码
你好,实现代码如下:
var arry = [{'id': 80, 'price': 50}, {'id': 20, 'price': 30}, {'id': 20, 'price': 300}]var strarr = []
for (let i in arry) {
strarr.push(arry[i]['price'])
}
console.log(eval(strarr.join('+'))) //结果
以上是实现的代码,希望采纳!
var shaanxi = [...略...]var sichuan = [...略...]
function leijia(a,b){
return a+b.value
}
var province = [
{name:'shaanxi', value: shaanxi.reduce(leijia,0)},
{name:'sichuan', value: sichuan.reduce(leijia,0)}
]